diff --git a/provider/internal/java/dependency.go b/provider/internal/java/dependency.go index d9f5fb8f..5d75492f 100644 --- a/provider/internal/java/dependency.go +++ b/provider/internal/java/dependency.go @@ -209,13 +209,17 @@ func (p *javaServiceClient) GetDependenciesDAG(ctx context.Context) (map[uri.URI func (p *javaServiceClient) discoverDepsFromJars(path string, ll map[uri.URI][]konveyor.DepDAGItem) { // for binaries we only find JARs embedded in archive w := walker{ - deps: ll, + deps: ll, + depToLabels: p.depToLabels, + m2RepoPath: getMavenLocalRepoPath(p.mvnSettingsFile), } filepath.WalkDir(path, w.walkDirForJar) } type walker struct { - deps map[uri.URI][]provider.DepDAGItem + deps map[uri.URI][]provider.DepDAGItem + depToLabels map[string]*depLabelItem + m2RepoPath string } func (w *walker) walkDirForJar(path string, info fs.DirEntry, err error) error { @@ -229,6 +233,20 @@ func (w *walker) walkDirForJar(path string, info fs.DirEntry, err error) error { d := provider.Dep{ Name: info.Name(), } + artifact, _ := toDependency(context.TODO(), path) + if (artifact != javaArtifact{}) { + d.Name = fmt.Sprintf("%s.%s", artifact.GroupId, artifact.ArtifactId) + d.Version = artifact.Version + d.Labels = addDepLabels(w.depToLabels, d.Name) + d.ResolvedIdentifier = artifact.sha1 + // when we can successfully get javaArtifact from a jar + // we added it to the pom and it should be in m2Repo path + if w.m2RepoPath != "" { + d.FileURIPrefix = filepath.Join(w.m2RepoPath, + strings.Replace(artifact.GroupId, ".", "/", -1), artifact.ArtifactId, artifact.Version) + } + } + w.deps[uri.URI(filepath.Join(path, info.Name()))] = []provider.DepDAGItem{ { Dep: d, @@ -271,15 +289,15 @@ func (p *javaServiceClient) parseDepString(dep, localRepoPath string) (provider. d.ResolvedIdentifier = string(b) } - d.Labels = p.addDepLabels(d.Name) + d.Labels = addDepLabels(p.depToLabels, d.Name) d.FileURIPrefix = fmt.Sprintf("file://%v", filepath.Dir(fp)) return d, nil } -func (p *javaServiceClient) addDepLabels(depName string) []string { +func addDepLabels(depToLabels map[string]*depLabelItem, depName string) []string { m := map[string]interface{}{} - for _, d := range p.depToLabels { + for _, d := range depToLabels { if d.r.Match([]byte(depName)) { for label, _ := range d.labels { m[label] = nil diff --git a/provider/internal/java/util.go b/provider/internal/java/util.go index fe973fc0..40d2f5af 100644 --- a/provider/internal/java/util.go +++ b/provider/internal/java/util.go @@ -61,6 +61,7 @@ type javaArtifact struct { GroupId string ArtifactId string Version string + sha1 string } type decompileFilter interface { @@ -177,14 +178,14 @@ func decompileJava(ctx context.Context, log logr.Logger, archivePath string) (ex return "", "", err } - err = createJavaProject(ctx, projectPath, deps) + err = createJavaProject(ctx, projectPath, deduplicateJavaArtifacts(deps)) if err != nil { log.Error(err, "failed to create java project", "path", projectPath) return "", "", err } log.V(5).Info("created java project", "path", projectPath) - err = decompile(context.TODO(), log, decompFilter, 10, decompJobs, projectPath) + err = decompile(ctx, log, decompFilter, 10, decompJobs, projectPath) if err != nil { log.Error(err, "failed to decompile", "path", archivePath) return "", "", err @@ -193,6 +194,20 @@ func decompileJava(ctx context.Context, log logr.Logger, archivePath string) (ex return explodedPath, projectPath, err } +func deduplicateJavaArtifacts(artifacts []javaArtifact) []javaArtifact { + uniq := []javaArtifact{} + seen := map[string]bool{} + for _, a := range artifacts { + key := fmt.Sprintf("%s-%s-%s%s", + a.ArtifactId, a.GroupId, a.Version, a.packaging) + if _, ok := seen[key]; !ok { + seen[key] = true + uniq = append(uniq, a) + } + } + return uniq +} + // explode explodes the given JAR, WAR or EAR archive, generates javaArtifact struct for given archive // and identifies all .class found recursively. returns output path, a list of decompileJob for .class files // it also returns a list of any javaArtifact we could interpret from jars @@ -204,7 +219,7 @@ func explode(ctx context.Context, log logr.Logger, archivePath, projectPath stri } // Create the destDir directory using the same permissions as the Java archive file - // java.jar should become java-jar-decompiled + // java.jar should become java-jar-exploded destDir := filepath.Join(path.Dir(archivePath), strings.Replace(path.Base(archivePath), ".", "-", -1)+"-exploded") // make sure execute bits are set so that fernflower can decompile err = os.MkdirAll(destDir, fileInfo.Mode()|0111) @@ -468,6 +483,7 @@ func toDependency(ctx context.Context, jarFile string) (javaArtifact, error) { dep.GroupId = jarInfo["g"].(string) dep.ArtifactId = jarInfo["a"].(string) dep.Version = jarInfo["v"].(string) + dep.sha1 = sha1sum return dep, nil } return dep, fmt.Errorf("failed to construct artifact from jar")