1919import java .util .List ;
2020import java .util .Queue ;
2121import java .util .Set ;
22+ import java .util .stream .Stream ;
2223
2324import org .eclipse .aether .RepositoryException ;
2425import org .eclipse .aether .RepositorySystem ;
2526import org .eclipse .aether .RepositorySystemSession ;
2627import org .eclipse .aether .artifact .Artifact ;
28+ import org .eclipse .aether .artifact .DefaultArtifact ;
2729import org .eclipse .aether .graph .DefaultDependencyNode ;
2830import org .eclipse .aether .graph .Dependency ;
2931import org .eclipse .aether .graph .DependencyNode ;
3032import org .eclipse .aether .repository .RemoteRepository ;
33+ import org .eclipse .aether .repository .RepositoryPolicy ;
3134import org .eclipse .aether .resolution .ArtifactDescriptorRequest ;
3235import org .eclipse .aether .resolution .ArtifactDescriptorResult ;
3336import org .eclipse .aether .resolution .ArtifactRequest ;
3942 */
4043public class MavenDependencyCollector {
4144
42- private static final Set < String > VALID_EXTENSIONS = Set . of ( "jar" , " pom") ;
45+ private static final String TYPE_POM = " pom" ;
4346
44- private RepositorySystem repoSystem ;
45- private RepositorySystemSession repositorySession ;
46- private List <RemoteRepository > repositories ;
47- private DependencyDepth depth ;
48- private Collection <String > dependencyScopes ;
47+ private static final Set <String > VALID_EXTENSIONS = Set .of ("jar" , TYPE_POM );
48+
49+ private final RepositorySystem repoSystem ;
50+ private final RepositorySystemSession repositorySession ;
51+ private final List <RemoteRepository > repositories ;
52+ private final Collection <String > dependencyScopes ;
53+
54+ private DependencyDepth dependencyDepth ;
4955
5056 public MavenDependencyCollector (RepositorySystem repoSystem , RepositorySystemSession repositorySession ,
51- List <RemoteRepository > repositories , DependencyDepth depth , Collection <String > dependencyScopes ) {
57+ List <RemoteRepository > defaultRepositories , Collection <AdditionalRepository > additionalRepositories ,
58+ DependencyDepth depth , Collection <String > dependencyScopes ) {
5259 this .repoSystem = repoSystem ;
5360 this .repositorySession = repositorySession ;
54- this .repositories = repositories ;
55- this .depth = depth ;
61+ if (additionalRepositories == null || additionalRepositories .isEmpty ()) {
62+ this .repositories = repoSystem .newResolutionRepositories (repositorySession , defaultRepositories );
63+ } else {
64+ List <RemoteRepository > combined = Stream
65+ .concat (defaultRepositories .stream (), additionalRepositories .stream ().map (additional -> {
66+ return new RemoteRepository .Builder (additional .id (), "default" , additional .url ())
67+ .setReleasePolicy (new RepositoryPolicy (true , RepositoryPolicy .UPDATE_POLICY_ALWAYS ,
68+ RepositoryPolicy .CHECKSUM_POLICY_WARN ))
69+ .setSnapshotPolicy (new RepositoryPolicy (true , RepositoryPolicy .UPDATE_POLICY_ALWAYS ,
70+ RepositoryPolicy .CHECKSUM_POLICY_WARN ))
71+ .build ();
72+ })).distinct ().toList ();
73+ this .repositories = repoSystem .newResolutionRepositories (repositorySession , combined );
74+ }
75+ this .dependencyDepth = depth ;
5676 this .dependencyScopes = dependencyScopes ;
5777 }
5878
59- public DependencyResult collect (Dependency root ) throws RepositoryException {
60- if (!isValidDependency (root )) {
79+ public List <RemoteRepository > getEffectiveRepositories () {
80+ return repositories ;
81+ }
82+
83+ public DependencyResult collect (MavenRootDependency root ) throws RepositoryException {
84+ if (!VALID_EXTENSIONS .contains (root .getType ())) {
6185 throw new RepositoryException (
62- "Invalid root dependency: " + root + " allowed extensions are " + VALID_EXTENSIONS );
86+ "Invalid root dependency: " + root + " allowed types are " + VALID_EXTENSIONS );
6387 }
64- List <Artifact > artifacts = new ArrayList <>();
88+ DependencyDepth depth = getEffectiveDepth (root , dependencyDepth );
89+ List <RepositoryArtifact > artifacts = new ArrayList <>();
6590 List <DependencyNode > nodes = new ArrayList <>();
66- ArtifactDescriptor rootDescriptor = readArtifactDescriptor (root , null , artifacts , nodes );
91+ ArtifactDescriptor rootDescriptor = readArtifactDescriptor (new Dependency (
92+ new DefaultArtifact (root .groupId (), root .artifactId (), root .classifier (), root .type (), root .version ()),
93+ null ), null , artifacts , nodes );
6794 if (depth == DependencyDepth .NONE ) {
68- return new DependencyResult (artifacts , rootDescriptor .node (), nodes );
95+ return new DependencyResult (depth , artifacts , rootDescriptor .node (), nodes );
6996 }
7097 if (depth == DependencyDepth .DIRECT ) {
7198 for (Dependency dependency : rootDescriptor .dependencies ()) {
7299 readArtifactDescriptor (dependency , rootDescriptor .node (), artifacts , nodes );
73100 }
74- return new DependencyResult (artifacts , rootDescriptor .node (), nodes );
101+ return new DependencyResult (depth , artifacts , rootDescriptor .node (), nodes );
75102 }
76103 // Add all dependencies with BFS method
77104 Set <String > collected = new HashSet <>();
@@ -90,15 +117,7 @@ public DependencyResult collect(Dependency root) throws RepositoryException {
90117 }
91118 }
92119 }
93- return new DependencyResult (artifacts , rootDescriptor .node (), nodes );
94- }
95-
96- private String getId (Dependency dependency ) {
97- Artifact artifact = dependency .getArtifact ();
98- // This does not include the version so we always ever only collect one version
99- // of an (transitive) artifact
100- return artifact .getGroupId () + ":" + artifact .getArtifactId () + ":"
101- + artifact .getClassifier ();
120+ return new DependencyResult (depth , artifacts , rootDescriptor .node (), nodes );
102121 }
103122
104123 /**
@@ -111,8 +130,7 @@ private String getId(Dependency dependency) {
111130 * @throws RepositoryException
112131 */
113132 private ArtifactDescriptor readArtifactDescriptor (Dependency dependency , DependencyNode parent ,
114- Collection <Artifact > artifacts ,
115- List <DependencyNode > nodes ) throws RepositoryException {
133+ Collection <RepositoryArtifact > artifacts , List <DependencyNode > nodes ) throws RepositoryException {
116134 if (isValidScope (dependency ) && isValidDependency (dependency )) {
117135 ArtifactDescriptorRequest descriptorRequest = new ArtifactDescriptorRequest ();
118136 descriptorRequest .setArtifact (dependency .getArtifact ());
@@ -123,7 +141,7 @@ private ArtifactDescriptor readArtifactDescriptor(Dependency dependency, Depende
123141 artifactRequest .setRepositories (repositories );
124142 ArtifactResult artifactResult = repoSystem .resolveArtifact (repositorySession , artifactRequest );
125143 Artifact resolved = artifactResult .getArtifact ();
126- artifacts .add (resolved );
144+ artifacts .add (new RepositoryArtifact ( resolved , artifactResult . getRepository ()) );
127145 DefaultDependencyNode dependencyNode = new DefaultDependencyNode (
128146 new Dependency (resolved , dependency .getScope ()));
129147 nodes .add (dependencyNode );
@@ -153,4 +171,32 @@ private boolean isValidScope(Dependency dependency) {
153171 return dependencyScopes .contains (scope );
154172 }
155173
174+ public static DependencyDepth getEffectiveDepth (MavenRootDependency root , DependencyDepth dependencyDepth ) {
175+ DependencyDepth depth ;
176+ if (isClassified (root )) {
177+ // a classified artifact can not have any dependencies and will actually include
178+ // the ones from the main artifact.
179+ // if the user really wants this it is possible to include the pom typed
180+ // artifact or the main artifact in the list
181+ depth = DependencyDepth .NONE ;
182+ } else if (dependencyDepth == DependencyDepth .NONE && TYPE_POM .equalsIgnoreCase (root .type ())) {
183+ depth = DependencyDepth .DIRECT ;
184+ } else {
185+ depth = dependencyDepth ;
186+ }
187+ return depth ;
188+ }
189+
190+ private static String getId (Dependency dependency ) {
191+ Artifact artifact = dependency .getArtifact ();
192+ // This does not include the version so we always ever only collect one version
193+ // of an (transitive) artifact
194+ return artifact .getGroupId () + ":" + artifact .getArtifactId () + ":" + artifact .getClassifier ();
195+ }
196+
197+ private static boolean isClassified (MavenRootDependency root ) {
198+ String classifier = root .classifier ();
199+ return classifier != null && !classifier .isBlank ();
200+ }
201+
156202}
0 commit comments