51
51
52
52
import org .apache .commons .lang3 .StringUtils ;
53
53
import org .apache .maven .artifact .Artifact ;
54
- import org .apache .maven .artifact .resolver .ArtifactResolutionRequest ;
55
- import org .apache .maven .artifact .resolver .ArtifactResolutionResult ;
54
+ import org .apache .maven .artifact .DefaultArtifact ;
55
+ import org .apache .maven .artifact .handler .ArtifactHandler ;
56
+ import org .apache .maven .artifact .handler .manager .ArtifactHandlerManager ;
57
+ import org .apache .maven .artifact .resolver .filter .ExcludesArtifactFilter ;
58
+ import org .apache .maven .artifact .versioning .InvalidVersionSpecificationException ;
59
+ import org .apache .maven .artifact .versioning .VersionRange ;
56
60
import org .apache .maven .buildcache .CacheUtils ;
57
61
import org .apache .maven .buildcache .MultiModuleSupport ;
58
62
import org .apache .maven .buildcache .NormalizedModelProvider ;
71
75
import org .apache .maven .buildcache .xml .config .Include ;
72
76
import org .apache .maven .execution .MavenSession ;
73
77
import org .apache .maven .model .Dependency ;
78
+ import org .apache .maven .model .Exclusion ;
74
79
import org .apache .maven .model .Model ;
75
80
import org .apache .maven .model .Plugin ;
76
81
import org .apache .maven .model .PluginExecution ;
77
82
import org .apache .maven .model .Resource ;
78
83
import org .apache .maven .model .io .xpp3 .MavenXpp3Writer ;
79
84
import org .apache .maven .project .MavenProject ;
80
- import org .apache .maven .repository .RepositorySystem ;
81
85
import org .codehaus .plexus .util .IOUtil ;
82
86
import org .codehaus .plexus .util .WriterFactory ;
87
+ import org .eclipse .aether .RepositorySystem ;
88
+ import org .eclipse .aether .artifact .DefaultArtifactType ;
89
+ import org .eclipse .aether .resolution .ArtifactRequest ;
90
+ import org .eclipse .aether .resolution .ArtifactResolutionException ;
91
+ import org .eclipse .aether .resolution .ArtifactResult ;
83
92
import org .slf4j .Logger ;
84
93
import org .slf4j .LoggerFactory ;
85
94
@@ -133,6 +142,8 @@ public class MavenProjectInput {
133
142
private final MultiModuleSupport multiModuleSupport ;
134
143
private final ProjectInputCalculator projectInputCalculator ;
135
144
private final Path baseDirPath ;
145
+ private final ArtifactHandlerManager artifactHandlerManager ;
146
+
136
147
/**
137
148
* The project glob to use every time there is no override
138
149
*/
@@ -152,7 +163,8 @@ public MavenProjectInput(
152
163
MavenSession session ,
153
164
CacheConfig config ,
154
165
RepositorySystem repoSystem ,
155
- RemoteCacheRepository remoteCache ) {
166
+ RemoteCacheRepository remoteCache ,
167
+ ArtifactHandlerManager artifactHandlerManager ) {
156
168
this .project = project ;
157
169
this .normalizedModelProvider = normalizedModelProvider ;
158
170
this .multiModuleSupport = multiModuleSupport ;
@@ -171,6 +183,7 @@ public MavenProjectInput(
171
183
this .exclusionResolver = new ExclusionResolver (project , config );
172
184
173
185
this .fileComparator = new PathIgnoringCaseComparator ();
186
+ this .artifactHandlerManager = artifactHandlerManager ;
174
187
}
175
188
176
189
public ProjectsInputInfo calculateChecksum () throws IOException {
@@ -644,7 +657,7 @@ private SortedMap<String, String> getMutablePluginDependencies() throws IOExcept
644
657
continue ;
645
658
}
646
659
647
- String rawKeyPrefix = KeyUtils .getVersionlessArtifactKey (repoSystem . createPluginArtifact (plugin ));
660
+ String rawKeyPrefix = KeyUtils .getVersionlessArtifactKey (createPluginArtifact (plugin ));
648
661
int occurrenceIndex = keyPrefixOccurrenceIndex
649
662
.computeIfAbsent (rawKeyPrefix , k -> new AtomicInteger ())
650
663
.getAndIncrement ();
@@ -654,6 +667,110 @@ private SortedMap<String, String> getMutablePluginDependencies() throws IOExcept
654
667
return fullMap ;
655
668
}
656
669
670
+ public Artifact createPluginArtifact (Plugin plugin ) {
671
+
672
+ VersionRange versionRange ;
673
+ try {
674
+ versionRange = VersionRange .createFromVersionSpec (plugin .getVersion ());
675
+ } catch (InvalidVersionSpecificationException e ) {
676
+ LOGGER .error (
677
+ String .format (
678
+ "Invalid version specification '%s' creating plugin artifact '%s'." ,
679
+ plugin .getVersion (), plugin ),
680
+ e );
681
+ // should not happen here
682
+ throw new RuntimeException (e );
683
+ }
684
+
685
+ return createArtifact (
686
+ plugin .getGroupId (),
687
+ plugin .getArtifactId (),
688
+ versionRange ,
689
+ "maven-plugin" ,
690
+ null ,
691
+ Artifact .SCOPE_RUNTIME ,
692
+ null ,
693
+ false );
694
+ }
695
+
696
+ private Artifact createArtifact (
697
+ String groupId ,
698
+ String artifactId ,
699
+ VersionRange versionRange ,
700
+ String type ,
701
+ String classifier ,
702
+ String scope ,
703
+ String inheritedScope ,
704
+ boolean optional ) {
705
+ String desiredScope = Artifact .SCOPE_RUNTIME ;
706
+
707
+ if (inheritedScope == null ) {
708
+ desiredScope = scope ;
709
+ } else if (Artifact .SCOPE_TEST .equals (scope ) || Artifact .SCOPE_PROVIDED .equals (scope )) {
710
+ return null ;
711
+ } else if (Artifact .SCOPE_COMPILE .equals (scope ) && Artifact .SCOPE_COMPILE .equals (inheritedScope )) {
712
+ // added to retain compile artifactScope. Remove if you want compile inherited as runtime
713
+ desiredScope = Artifact .SCOPE_COMPILE ;
714
+ }
715
+
716
+ if (Artifact .SCOPE_TEST .equals (inheritedScope )) {
717
+ desiredScope = Artifact .SCOPE_TEST ;
718
+ }
719
+
720
+ if (Artifact .SCOPE_PROVIDED .equals (inheritedScope )) {
721
+ desiredScope = Artifact .SCOPE_PROVIDED ;
722
+ }
723
+
724
+ if (Artifact .SCOPE_SYSTEM .equals (scope )) {
725
+ // system scopes come through unchanged...
726
+ desiredScope = Artifact .SCOPE_SYSTEM ;
727
+ }
728
+ ArtifactHandler handler = artifactHandlerManager .getArtifactHandler (type );
729
+
730
+ return new DefaultArtifact (
731
+ groupId , artifactId , versionRange , desiredScope , type , classifier , handler , optional );
732
+ }
733
+
734
+ public Artifact createDependencyArtifact (Dependency d ) {
735
+ VersionRange versionRange ;
736
+ try {
737
+ versionRange = VersionRange .createFromVersionSpec (d .getVersion ());
738
+ } catch (InvalidVersionSpecificationException e ) {
739
+ LOGGER .error (
740
+ String .format (
741
+ "Invalid version specification '%s' creating dependency artifact '%s'." , d .getVersion (), d ),
742
+ e );
743
+ // should not happen here ?
744
+ throw new RuntimeException (e );
745
+ }
746
+
747
+ Artifact artifact = createArtifact (
748
+ d .getGroupId (),
749
+ d .getArtifactId (),
750
+ versionRange ,
751
+ d .getType (),
752
+ d .getClassifier (),
753
+ d .getScope (),
754
+ null ,
755
+ d .isOptional ());
756
+
757
+ if (Artifact .SCOPE_SYSTEM .equals (d .getScope ()) && d .getSystemPath () != null ) {
758
+ artifact .setFile (new File (d .getSystemPath ()));
759
+ }
760
+
761
+ if (!d .getExclusions ().isEmpty ()) {
762
+ List <String > exclusions = new ArrayList <>();
763
+
764
+ for (Exclusion exclusion : d .getExclusions ()) {
765
+ exclusions .add (exclusion .getGroupId () + ':' + exclusion .getArtifactId ());
766
+ }
767
+
768
+ artifact .setDependencyFilter (new ExcludesArtifactFilter (exclusions ));
769
+ }
770
+
771
+ return artifact ;
772
+ }
773
+
657
774
private SortedMap <String , String > getMutableDependenciesHashes (String keyPrefix , List <Dependency > dependencies )
658
775
throws IOException {
659
776
SortedMap <String , String > result = new TreeMap <>();
@@ -684,51 +801,58 @@ private SortedMap<String, String> getMutableDependenciesHashes(String keyPrefix,
684
801
projectInputCalculator .calculateInput (dependencyProject ).getChecksum ();
685
802
} else // this is a snapshot dependency
686
803
{
687
- DigestItem resolved = resolveArtifact (repoSystem .createDependencyArtifact (dependency ), false );
804
+ DigestItem resolved = null ;
805
+ try {
806
+ resolved = resolveArtifact (dependency );
807
+ } catch (ArtifactResolutionException | InvalidVersionSpecificationException e ) {
808
+ throw new IOException (e );
809
+ }
688
810
projectHash = resolved .getHash ();
689
811
}
690
812
result .put (
691
- keyPrefix + KeyUtils .getVersionlessArtifactKey (repoSystem .createDependencyArtifact (dependency )),
692
- projectHash );
813
+ keyPrefix + KeyUtils .getVersionlessArtifactKey (createDependencyArtifact (dependency )), projectHash );
693
814
}
694
815
return result ;
695
816
}
696
817
697
818
@ Nonnull
698
- private DigestItem resolveArtifact (final Artifact dependencyArtifact , boolean isOffline ) throws IOException {
699
- ArtifactResolutionRequest request = new ArtifactResolutionRequest ()
700
- .setArtifact (dependencyArtifact )
701
- .setResolveRoot (true )
702
- .setResolveTransitively (false )
703
- .setLocalRepository (session .getLocalRepository ())
704
- .setRemoteRepositories (project .getRemoteArtifactRepositories ())
705
- .setOffline (session .isOffline () || isOffline )
706
- .setForceUpdate (session .getRequest ().isUpdateSnapshots ())
707
- .setServers (session .getRequest ().getServers ())
708
- .setMirrors (session .getRequest ().getMirrors ())
709
- .setProxies (session .getRequest ().getProxies ());
710
-
711
- final ArtifactResolutionResult result = repoSystem .resolve (request );
712
-
713
- if (!result .isSuccess ()) {
819
+ private DigestItem resolveArtifact (final Dependency dependency )
820
+ throws IOException , ArtifactResolutionException , InvalidVersionSpecificationException {
821
+
822
+ org .eclipse .aether .artifact .Artifact dependencyArtifact = new org .eclipse .aether .artifact .DefaultArtifact (
823
+ dependency .getGroupId (),
824
+ dependency .getArtifactId (),
825
+ dependency .getClassifier (),
826
+ null ,
827
+ dependency .getVersion (),
828
+ new DefaultArtifactType (dependency .getType ()));
829
+ ArtifactRequest artifactRequest = new ArtifactRequest ().setArtifact (dependencyArtifact );
830
+
831
+ ArtifactResult result = repoSystem .resolveArtifact (session .getRepositorySession (), artifactRequest );
832
+
833
+ if (!result .isResolved ()) {
714
834
throw new DependencyNotResolvedException ("Cannot resolve in-project dependency: " + dependencyArtifact );
715
835
}
716
836
717
- if (!result .getMissingArtifacts ().isEmpty ()) {
718
- throw new DependencyNotResolvedException (
719
- "Cannot resolve artifact: " + dependencyArtifact + ", missing: " + result .getMissingArtifacts ());
837
+ if (result .isMissing ()) {
838
+ throw new DependencyNotResolvedException ("Cannot resolve missing artifact: " + dependencyArtifact );
720
839
}
721
840
722
- if (result .getArtifacts ().size () != 1 ) {
723
- throw new IllegalStateException ("Unexpected number of artifacts returned. Requested: " + dependencyArtifact
724
- + ", expected: 1, actual: " + result .getArtifacts ());
725
- }
841
+ org .eclipse .aether .artifact .Artifact resolved = result .getArtifact ();
726
842
727
- final Artifact resolved = result .getArtifacts ().iterator ().next ();
843
+ Artifact artifact = createArtifact (
844
+ resolved .getGroupId (),
845
+ resolved .getArtifactId (),
846
+ VersionRange .createFromVersionSpec (resolved .getVersion ()),
847
+ dependency .getType (),
848
+ resolved .getClassifier (),
849
+ dependency .getType (),
850
+ dependency .getScope (),
851
+ false );
728
852
729
853
final HashAlgorithm algorithm = config .getHashFactory ().createAlgorithm ();
730
854
final String hash = algorithm .hash (resolved .getFile ().toPath ());
731
- return DtoUtils .createDigestedFile (resolved , hash );
855
+ return DtoUtils .createDigestedFile (artifact , hash );
732
856
}
733
857
734
858
/**
0 commit comments