From f419b0a54056ca986f4b2faafa35dbcba6de9859 Mon Sep 17 00:00:00 2001 From: Emily McMullan Date: Wed, 28 Aug 2024 12:02:38 -0400 Subject: [PATCH 1/2] find pom for .war and .ear files Signed-off-by: Emily McMullan --- .../pkg/java_external_provider/dependency.go | 36 ++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/external-providers/java-external-provider/pkg/java_external_provider/dependency.go b/external-providers/java-external-provider/pkg/java_external_provider/dependency.go index b891b6d7..115ef780 100644 --- a/external-providers/java-external-provider/pkg/java_external_provider/dependency.go +++ b/external-providers/java-external-provider/pkg/java_external_provider/dependency.go @@ -120,6 +120,11 @@ func (p *javaServiceClient) GetDependencies(ctx context.Context) (map[uri.URI][] ll = make(map[uri.URI][]konveyor.DepDAGItem, 0) // for binaries we only find JARs embedded in archive p.discoverDepsFromJars(p.config.DependencyPath, ll) + if len(ll) == 0 { + p.log.Info("unable to get dependencies from jars, looking for pom") + pomPath := p.discoverPom(p.config.DependencyPath, ll) + return p.GetDependenciesFallback(ctx, pomPath) + } } else { ll, err = p.GetDependenciesDAG(ctx) if err != nil { @@ -215,7 +220,7 @@ func (p *javaServiceClient) GetDependenciesFallback(ctx context.Context, locatio artifactIdKey: *d.ArtifactID, pomPathKey: path, } - if *d.Version != "" { + if d.Version != nil { if strings.Contains(*d.Version, "$") { version := strings.TrimSuffix(strings.TrimPrefix(*d.Version, "${"), "}") p.log.V(10).Info("Searching for property in properties", @@ -562,6 +567,7 @@ type walker struct { m2RepoPath string initialPath string seen map[string]bool + pomPath string } func (w *walker) walkDirForJar(path string, info fs.DirEntry, err error) error { @@ -634,6 +640,34 @@ func (w *walker) walkDirForJar(path string, info fs.DirEntry, err error) error { return nil } +func (p *javaServiceClient) discoverPom(path string, ll map[uri.URI][]konveyor.DepDAGItem) string { + pathMeta := filepath.Join(path, "META-INF") + w := walker{ + deps: ll, + depToLabels: p.depToLabels, + m2RepoPath: "", + seen: map[string]bool{}, + initialPath: pathMeta, + pomPath: "", + } + filepath.WalkDir(pathMeta, w.walkDirForPom) + return w.pomPath +} + +func (w *walker) walkDirForPom(path string, info fs.DirEntry, err error) error { + if info == nil { + return nil + } + if info.IsDir() { + return filepath.WalkDir(filepath.Join(path, info.Name()), w.walkDirForPom) + } + if strings.Contains(info.Name(), "pom.xml") { + w.pomPath = path + return nil + } + return nil +} + // parseDepString parses a java dependency string func (p *javaServiceClient) parseDepString(dep, localRepoPath, pomPath string) (provider.Dep, error) { d := provider.Dep{} From 3a6032c73d20782ae55f6e465d346d3687f1fe03 Mon Sep 17 00:00:00 2001 From: Emily McMullan Date: Thu, 12 Sep 2024 09:57:18 -0400 Subject: [PATCH 2/2] find all poms Signed-off-by: Emily McMullan --- .../pkg/java_external_provider/dependency.go | 34 +++++++++++++------ 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/external-providers/java-external-provider/pkg/java_external_provider/dependency.go b/external-providers/java-external-provider/pkg/java_external_provider/dependency.go index 115ef780..3569d6d8 100644 --- a/external-providers/java-external-provider/pkg/java_external_provider/dependency.go +++ b/external-providers/java-external-provider/pkg/java_external_provider/dependency.go @@ -8,6 +8,7 @@ import ( "fmt" "io" "io/fs" + "maps" "os" "os/exec" "path/filepath" @@ -90,6 +91,8 @@ func (p *javaServiceClient) findGradleBuild() string { } func (p *javaServiceClient) GetDependencies(ctx context.Context) (map[uri.URI][]*provider.Dep, error) { + p.log.V(4).Info("running dependency analysis") + if p.GetBuildTool() == gradle { p.log.V(2).Info("gradle found - retrieving dependencies") m := map[uri.URI][]*provider.Dep{} @@ -122,8 +125,15 @@ func (p *javaServiceClient) GetDependencies(ctx context.Context) (map[uri.URI][] p.discoverDepsFromJars(p.config.DependencyPath, ll) if len(ll) == 0 { p.log.Info("unable to get dependencies from jars, looking for pom") - pomPath := p.discoverPom(p.config.DependencyPath, ll) - return p.GetDependenciesFallback(ctx, pomPath) + pomPaths := p.discoverPoms(p.config.DependencyPath, ll) + for _, path := range pomPaths { + dep, err := p.GetDependenciesFallback(ctx, path) + if err != nil { + return m, err + } + maps.Copy(m, dep) + } + return m, nil } } else { ll, err = p.GetDependenciesDAG(ctx) @@ -248,6 +258,10 @@ func (p *javaServiceClient) GetDependenciesFallback(ctx context.Context, locatio } deps = append(deps, &dep) } + if len(deps) == 0 { + p.log.V(1).Info("unable to get dependencies from pom.xml in fallback", "pom", path) + return nil, nil + } m := map[uri.URI][]*provider.Dep{} m[uri.File(path)] = deps @@ -567,7 +581,7 @@ type walker struct { m2RepoPath string initialPath string seen map[string]bool - pomPath string + pomPaths []string } func (w *walker) walkDirForJar(path string, info fs.DirEntry, err error) error { @@ -640,18 +654,17 @@ func (w *walker) walkDirForJar(path string, info fs.DirEntry, err error) error { return nil } -func (p *javaServiceClient) discoverPom(path string, ll map[uri.URI][]konveyor.DepDAGItem) string { - pathMeta := filepath.Join(path, "META-INF") +func (p *javaServiceClient) discoverPoms(pathStart string, ll map[uri.URI][]konveyor.DepDAGItem) []string { w := walker{ deps: ll, depToLabels: p.depToLabels, m2RepoPath: "", seen: map[string]bool{}, - initialPath: pathMeta, - pomPath: "", + initialPath: pathStart, + pomPaths: []string{}, } - filepath.WalkDir(pathMeta, w.walkDirForPom) - return w.pomPath + filepath.WalkDir(pathStart, w.walkDirForPom) + return w.pomPaths } func (w *walker) walkDirForPom(path string, info fs.DirEntry, err error) error { @@ -662,8 +675,7 @@ func (w *walker) walkDirForPom(path string, info fs.DirEntry, err error) error { return filepath.WalkDir(filepath.Join(path, info.Name()), w.walkDirForPom) } if strings.Contains(info.Name(), "pom.xml") { - w.pomPath = path - return nil + w.pomPaths = append(w.pomPaths, path) } return nil }