@@ -136,9 +136,8 @@ func (p *Parser) Parse(ctx context.Context, r xio.ReadSeekerAt) ([]ftypes.Packag
136136 // Cache root POM
137137 p .cache .put (result .artifact , result )
138138
139- rootArt := root .artifact ()
139+ rootArt := result .artifact
140140 rootArt .Relationship = ftypes .RelationshipRoot
141- rootArt .RootFilePath = p .rootPath
142141
143142 return p .parseRoot (ctx , rootArt , set .New [string ]())
144143}
@@ -228,7 +227,7 @@ func (p *Parser) parseRoot(ctx context.Context, root artifact, uniqModules set.S
228227
229228 // Parse, cache, and enqueue modules.
230229 for _ , relativePath := range result .modules {
231- moduleArtifact , err := p .parseModule (ctx , result .filePath , relativePath )
230+ moduleArtifact , err := p .parseModule (ctx , result .filePath , relativePath , root . Repositories )
232231 if err != nil {
233232 p .logger .Debug ("Unable to parse the module" ,
234233 log .FilePath (result .filePath ), log .Err (err ))
@@ -307,7 +306,7 @@ func depVersion(depName string, uniqArtifacts map[string]artifact) string {
307306 return ""
308307}
309308
310- func (p * Parser ) parseModule (ctx context.Context , currentPath , relativePath string ) (artifact , error ) {
309+ func (p * Parser ) parseModule (ctx context.Context , currentPath , relativePath string , repos [] repository ) (artifact , error ) {
311310 // modulePath: "root/" + "module/" => "root/module"
312311 module , err := p .openRelativePom (currentPath , relativePath )
313312 if err != nil {
@@ -316,6 +315,7 @@ func (p *Parser) parseModule(ctx context.Context, currentPath, relativePath stri
316315
317316 result , err := p .analyze (ctx , module , analysisOptions {
318317 rootFilePath : module .filePath ,
318+ repositories : repos ,
319319 })
320320 if err != nil {
321321 return artifact {}, xerrors .Errorf ("analyze error: %w" , err )
@@ -347,17 +347,19 @@ func (p *Parser) resolve(ctx context.Context, art artifact, rootDepManagement []
347347
348348 p .logger .Debug ("Resolving..." , log .String ("group_id" , art .GroupID ),
349349 log .String ("artifact_id" , art .ArtifactID ), log .String ("version" , art .Version .String ()))
350- pomContent , err := p .tryRepository (ctx , art .GroupID , art .ArtifactID , art .Version .String ())
350+ pomContent , err := p .tryRepository (ctx , art .GroupID , art .ArtifactID , art .Version .String (), art . Repositories )
351351 if err != nil {
352352 if shouldReturnError (err ) {
353353 return analysisResult {}, err
354354 }
355355 p .logger .Debug ("Repository error" , log .Err (err ))
356356 }
357+
357358 result , err := p .analyze (ctx , pomContent , analysisOptions {
358359 exclusions : art .Exclusions ,
359360 depManagement : rootDepManagement ,
360361 rootFilePath : art .RootFilePath ,
362+ repositories : art .Repositories ,
361363 })
362364 if err != nil {
363365 return analysisResult {}, xerrors .Errorf ("analyze error: %w" , err )
@@ -380,6 +382,7 @@ type analysisOptions struct {
380382 exclusions set.Set [string ]
381383 depManagement []pomDependency // from the root POM
382384 rootFilePath string // File path of the root POM or module POM
385+ repositories []repository // Repositories inherited from parent
383386}
384387
385388func (p * Parser ) analyze (ctx context.Context , pom * pom , opts analysisOptions ) (analysisResult , error ) {
@@ -389,14 +392,16 @@ func (p *Parser) analyze(ctx context.Context, pom *pom, opts analysisOptions) (a
389392 if opts .exclusions == nil {
390393 opts .exclusions = set .New [string ]()
391394 }
392- // Update remoteRepositories
395+
396+ // Get repositories from current POM and merge with inherited ones
393397 pomRepos := pom .repositories (p .servers )
394- p . remoteRepos . pom = lo .UniqBy (append (pomRepos , p . remoteRepos . pom ... ), func (r repository ) url.URL {
398+ opts . repositories = lo .UniqBy (append (pomRepos , opts . repositories ... ), func (r repository ) url.URL {
395399 return r .url
396400 })
397401
398402 // Resolve parent POM
399- if err := p .resolveParent (ctx , pom ); err != nil {
403+ // TODO handle repos from parents
404+ if err := p .resolveParent (ctx , pom , opts .repositories ); err != nil {
400405 return analysisResult {}, xerrors .Errorf ("pom resolve error: %w" , err )
401406 }
402407
@@ -411,6 +416,7 @@ func (p *Parser) analyze(ctx context.Context, pom *pom, opts analysisOptions) (a
411416
412417 art := pom .artifact ()
413418 art .RootFilePath = opts .rootFilePath
419+ art .Repositories = opts .repositories
414420
415421 return analysisResult {
416422 filePath : pom .filePath ,
@@ -423,13 +429,13 @@ func (p *Parser) analyze(ctx context.Context, pom *pom, opts analysisOptions) (a
423429}
424430
425431// resolveParent resolves its parent POMs and inherits properties, dependencies, and dependencyManagement.
426- func (p * Parser ) resolveParent (ctx context.Context , pom * pom ) error {
432+ func (p * Parser ) resolveParent (ctx context.Context , pom * pom , pomRepos [] repository ) error {
427433 if pom .nil () {
428434 return nil
429435 }
430436
431437 // Parse parent POM
432- parent , err := p .parseParent (ctx , pom .filePath , pom .content .Parent )
438+ parent , err := p .parseParent (ctx , pom .filePath , pom .content .Parent , pomRepos )
433439 if err != nil {
434440 return xerrors .Errorf ("parent error: %w" , err )
435441 }
@@ -576,7 +582,7 @@ func excludeDep(exclusions set.Set[string], art artifact) bool {
576582 return false
577583}
578584
579- func (p * Parser ) parseParent (ctx context.Context , currentPath string , parent pomParent ) (* pom , error ) {
585+ func (p * Parser ) parseParent (ctx context.Context , currentPath string , parent pomParent , pomRepos [] repository ) (* pom , error ) {
580586 // Pass nil properties so that variables in <parent> are not evaluated.
581587 target := newArtifact (parent .GroupId , parent .ArtifactId , parent .Version , nil , nil )
582588 // if version is property (e.g. ${revision}) - we still need to parse this pom
@@ -588,7 +594,7 @@ func (p *Parser) parseParent(ctx context.Context, currentPath string, parent pom
588594 logger .Debug ("Start parent" )
589595 defer logger .Debug ("Exit parent" )
590596
591- parentPOM , err := p .retrieveParent (ctx , currentPath , parent .RelativePath , target )
597+ parentPOM , err := p .retrieveParent (ctx , currentPath , parent .RelativePath , target , pomRepos )
592598 if err != nil {
593599 if shouldReturnError (err ) {
594600 return nil , err
@@ -597,34 +603,34 @@ func (p *Parser) parseParent(ctx context.Context, currentPath string, parent pom
597603 return & pom {content : & pomXML {}}, nil
598604 }
599605
600- if err = p .resolveParent (ctx , parentPOM ); err != nil {
606+ if err = p .resolveParent (ctx , parentPOM , pomRepos ); err != nil {
601607 return nil , xerrors .Errorf ("parent pom resolve error: %w" , err )
602608 }
603609
604610 return parentPOM , nil
605611}
606612
607- func (p * Parser ) retrieveParent (ctx context.Context , currentPath , relativePath string , target artifact ) (* pom , error ) {
613+ func (p * Parser ) retrieveParent (ctx context.Context , currentPath , relativePath string , target artifact , pomRepos [] repository ) (* pom , error ) {
608614 var errs error
609615
610616 // Try relativePath
611617 if relativePath != "" {
612- pom , err := p .tryRelativePath (ctx , target , currentPath , relativePath )
618+ pom , err := p .tryRelativePath (ctx , target , currentPath , relativePath , pomRepos )
613619 if err == nil {
614620 return pom , nil
615621 }
616622 errs = multierror .Append (errs , err )
617623 }
618624
619625 // If not found, search the parent director
620- pom , err := p .tryRelativePath (ctx , target , currentPath , "../pom.xml" )
626+ pom , err := p .tryRelativePath (ctx , target , currentPath , "../pom.xml" , pomRepos )
621627 if err == nil {
622628 return pom , nil
623629 }
624630 errs = multierror .Append (errs , err )
625631
626632 // If not found, search local/remote remoteRepositories
627- pom , err = p .tryRepository (ctx , target .GroupID , target .ArtifactID , target .Version .String ())
633+ pom , err = p .tryRepository (ctx , target .GroupID , target .ArtifactID , target .Version .String (), pomRepos )
628634 if err == nil {
629635 return pom , nil
630636 }
@@ -634,7 +640,7 @@ func (p *Parser) retrieveParent(ctx context.Context, currentPath, relativePath s
634640 return nil , errs
635641}
636642
637- func (p * Parser ) tryRelativePath (ctx context.Context , parentArtifact artifact , currentPath , relativePath string ) (* pom , error ) {
643+ func (p * Parser ) tryRelativePath (ctx context.Context , parentArtifact artifact , currentPath , relativePath string , pomRepos [] repository ) (* pom , error ) {
638644 parsedPOM , err := p .openRelativePom (currentPath , relativePath )
639645 if err != nil {
640646 return nil , err
@@ -649,7 +655,7 @@ func (p *Parser) tryRelativePath(ctx context.Context, parentArtifact artifact, c
649655 if parsedPOM .artifact ().ArtifactID != parentArtifact .ArtifactID {
650656 return nil , xerrors .New ("'parent.relativePath' points at wrong local POM" )
651657 }
652- if err := p .resolveParent (ctx , parsedPOM ); err != nil {
658+ if err := p .resolveParent (ctx , parsedPOM , pomRepos ); err != nil {
653659 return nil , xerrors .Errorf ("analyze error: %w" , err )
654660 }
655661
@@ -699,7 +705,7 @@ func (p *Parser) openPom(filePath string) (*pom, error) {
699705 }, nil
700706}
701707
702- func (p * Parser ) tryRepository (ctx context.Context , groupID , artifactID , version string ) (* pom , error ) {
708+ func (p * Parser ) tryRepository (ctx context.Context , groupID , artifactID , version string , pomRepos [] repository ) (* pom , error ) {
703709 if version == "" {
704710 return nil , xerrors .Errorf ("Version missing for %s:%s" , groupID , artifactID )
705711 }
@@ -717,7 +723,7 @@ func (p *Parser) tryRepository(ctx context.Context, groupID, artifactID, version
717723 }
718724
719725 // Search remote remoteRepositories
720- loaded , err = p .fetchPOMFromRemoteRepositories (ctx , paths , isSnapshot (version ))
726+ loaded , err = p .fetchPOMFromRemoteRepositories (ctx , paths , isSnapshot (version ), pomRepos )
721727 if err == nil {
722728 return loaded , nil
723729 // We should return error if it's not "not found" error
@@ -735,7 +741,7 @@ func (p *Parser) loadPOMFromLocalRepository(paths []string) (*pom, error) {
735741 return p .openPom (localPath )
736742}
737743
738- func (p * Parser ) fetchPOMFromRemoteRepositories (ctx context.Context , paths []string , snapshot bool ) (* pom , error ) {
744+ func (p * Parser ) fetchPOMFromRemoteRepositories (ctx context.Context , paths []string , snapshot bool , pomRepos [] repository ) (* pom , error ) {
739745 // Do not try fetching pom.xml from remote repositories in offline mode
740746 if p .offline {
741747 p .logger .Debug ("Fetching the remote pom.xml is skipped" )
@@ -744,9 +750,9 @@ func (p *Parser) fetchPOMFromRemoteRepositories(ctx context.Context, paths []str
744750
745751 // Try all remoteRepositories by following order:
746752 // 1. remoteRepositories from settings.xml
747- // 2. remoteRepositories from pom.xml
753+ // 2. remoteRepositories from pom.xml (passed as parameter)
748754 // 3. default remoteRepository (Maven Central for Release repository)
749- for _ , repo := range slices .Concat (p .remoteRepos .settings , p . remoteRepos . pom , []repository {p .remoteRepos .defaultRepo }) {
755+ for _ , repo := range slices .Concat (p .remoteRepos .settings , pomRepos , []repository {p .remoteRepos .defaultRepo }) {
750756 // Skip Release only repositories for snapshot artifacts and vice versa
751757 if snapshot && ! repo .snapshotEnabled || ! snapshot && ! repo .releaseEnabled {
752758 continue
0 commit comments