Skip to content

Commit 4c0e754

Browse files
authored
[MBUILDCACHE-96] Remove use of maven-compat (#171)
* [MBUILDCACHE-96] Remove use of maven-compat --------- Signed-off-by: Olivier Lamy <[email protected]>
1 parent c6bc4bc commit 4c0e754

File tree

4 files changed

+167
-44
lines changed

4 files changed

+167
-44
lines changed

pom.xml

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -114,12 +114,6 @@ under the License.
114114
<version>${mavenVersion}</version>
115115
<scope>provided</scope>
116116
</dependency>
117-
<dependency>
118-
<groupId>org.apache.maven</groupId>
119-
<artifactId>maven-compat</artifactId>
120-
<version>${mavenVersion}</version>
121-
<scope>provided</scope>
122-
</dependency>
123117
<dependency>
124118
<groupId>org.apache.maven.resolver</groupId>
125119
<artifactId>maven-resolver-transport-http</artifactId>
@@ -456,7 +450,7 @@ under the License.
456450
<profile>
457451
<id>maven3</id>
458452
<properties>
459-
<mavenVersion>3.9.6</mavenVersion>
453+
<mavenVersion>3.9.8</mavenVersion>
460454
<maven.dir>maven3</maven.dir>
461455
<maven.basedir>${project.build.directory}/${maven.dir}</maven.basedir>
462456
</properties>

src/main/java/org/apache/maven/buildcache/CacheControllerImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@
8888
import org.apache.maven.plugin.descriptor.Parameter;
8989
import org.apache.maven.project.MavenProject;
9090
import org.apache.maven.project.MavenProjectHelper;
91-
import org.apache.maven.repository.RepositorySystem;
9291
import org.codehaus.plexus.util.ReflectionUtils;
92+
import org.eclipse.aether.RepositorySystem;
9393
import org.slf4j.Logger;
9494
import org.slf4j.LoggerFactory;
9595

src/main/java/org/apache/maven/buildcache/DefaultProjectInputCalculator.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,14 @@
2727
import java.util.concurrent.ConcurrentMap;
2828

2929
import org.apache.maven.SessionScoped;
30+
import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager;
3031
import org.apache.maven.buildcache.checksum.MavenProjectInput;
3132
import org.apache.maven.buildcache.xml.CacheConfig;
3233
import org.apache.maven.buildcache.xml.build.ProjectsInputInfo;
3334
import org.apache.maven.execution.MavenSession;
3435
import org.apache.maven.lifecycle.internal.builder.BuilderCommon;
3536
import org.apache.maven.project.MavenProject;
36-
import org.apache.maven.repository.RepositorySystem;
37+
import org.eclipse.aether.RepositorySystem;
3738
import org.slf4j.Logger;
3839
import org.slf4j.LoggerFactory;
3940

@@ -49,6 +50,7 @@ public class DefaultProjectInputCalculator implements ProjectInputCalculator {
4950
private final RepositorySystem repoSystem;
5051
private final NormalizedModelProvider normalizedModelProvider;
5152
private final MultiModuleSupport multiModuleSupport;
53+
private final ArtifactHandlerManager artifactHandlerManager;
5254

5355
private final ConcurrentMap<String, ProjectsInputInfo> checkSumMap = new ConcurrentHashMap<>();
5456

@@ -61,13 +63,15 @@ public DefaultProjectInputCalculator(
6163
CacheConfig cacheConfig,
6264
RepositorySystem repoSystem,
6365
NormalizedModelProvider rawModelProvider,
64-
MultiModuleSupport multiModuleSupport) {
66+
MultiModuleSupport multiModuleSupport,
67+
ArtifactHandlerManager artifactHandlerManager) {
6568
this.mavenSession = mavenSession;
6669
this.remoteCache = remoteCache;
6770
this.cacheConfig = cacheConfig;
6871
this.repoSystem = repoSystem;
6972
this.normalizedModelProvider = rawModelProvider;
7073
this.multiModuleSupport = multiModuleSupport;
74+
this.artifactHandlerManager = artifactHandlerManager;
7175
}
7276

7377
@Override
@@ -108,7 +112,8 @@ private ProjectsInputInfo calculateInputInternal(String key, MavenProject projec
108112
mavenSession,
109113
cacheConfig,
110114
repoSystem,
111-
remoteCache);
115+
remoteCache,
116+
artifactHandlerManager);
112117
return input.calculateChecksum();
113118
} catch (Exception e) {
114119
throw new RuntimeException("Failed to calculate checksums for " + project.getArtifactId(), e);

src/main/java/org/apache/maven/buildcache/checksum/MavenProjectInput.java

Lines changed: 157 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,12 @@
5151

5252
import org.apache.commons.lang3.StringUtils;
5353
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;
5660
import org.apache.maven.buildcache.CacheUtils;
5761
import org.apache.maven.buildcache.MultiModuleSupport;
5862
import org.apache.maven.buildcache.NormalizedModelProvider;
@@ -71,15 +75,20 @@
7175
import org.apache.maven.buildcache.xml.config.Include;
7276
import org.apache.maven.execution.MavenSession;
7377
import org.apache.maven.model.Dependency;
78+
import org.apache.maven.model.Exclusion;
7479
import org.apache.maven.model.Model;
7580
import org.apache.maven.model.Plugin;
7681
import org.apache.maven.model.PluginExecution;
7782
import org.apache.maven.model.Resource;
7883
import org.apache.maven.model.io.xpp3.MavenXpp3Writer;
7984
import org.apache.maven.project.MavenProject;
80-
import org.apache.maven.repository.RepositorySystem;
8185
import org.codehaus.plexus.util.IOUtil;
8286
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;
8392
import org.slf4j.Logger;
8493
import org.slf4j.LoggerFactory;
8594

@@ -133,6 +142,8 @@ public class MavenProjectInput {
133142
private final MultiModuleSupport multiModuleSupport;
134143
private final ProjectInputCalculator projectInputCalculator;
135144
private final Path baseDirPath;
145+
private final ArtifactHandlerManager artifactHandlerManager;
146+
136147
/**
137148
* The project glob to use every time there is no override
138149
*/
@@ -152,7 +163,8 @@ public MavenProjectInput(
152163
MavenSession session,
153164
CacheConfig config,
154165
RepositorySystem repoSystem,
155-
RemoteCacheRepository remoteCache) {
166+
RemoteCacheRepository remoteCache,
167+
ArtifactHandlerManager artifactHandlerManager) {
156168
this.project = project;
157169
this.normalizedModelProvider = normalizedModelProvider;
158170
this.multiModuleSupport = multiModuleSupport;
@@ -171,6 +183,7 @@ public MavenProjectInput(
171183
this.exclusionResolver = new ExclusionResolver(project, config);
172184

173185
this.fileComparator = new PathIgnoringCaseComparator();
186+
this.artifactHandlerManager = artifactHandlerManager;
174187
}
175188

176189
public ProjectsInputInfo calculateChecksum() throws IOException {
@@ -644,7 +657,7 @@ private SortedMap<String, String> getMutablePluginDependencies() throws IOExcept
644657
continue;
645658
}
646659

647-
String rawKeyPrefix = KeyUtils.getVersionlessArtifactKey(repoSystem.createPluginArtifact(plugin));
660+
String rawKeyPrefix = KeyUtils.getVersionlessArtifactKey(createPluginArtifact(plugin));
648661
int occurrenceIndex = keyPrefixOccurrenceIndex
649662
.computeIfAbsent(rawKeyPrefix, k -> new AtomicInteger())
650663
.getAndIncrement();
@@ -654,6 +667,110 @@ private SortedMap<String, String> getMutablePluginDependencies() throws IOExcept
654667
return fullMap;
655668
}
656669

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+
657774
private SortedMap<String, String> getMutableDependenciesHashes(String keyPrefix, List<Dependency> dependencies)
658775
throws IOException {
659776
SortedMap<String, String> result = new TreeMap<>();
@@ -684,51 +801,58 @@ private SortedMap<String, String> getMutableDependenciesHashes(String keyPrefix,
684801
projectInputCalculator.calculateInput(dependencyProject).getChecksum();
685802
} else // this is a snapshot dependency
686803
{
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+
}
688810
projectHash = resolved.getHash();
689811
}
690812
result.put(
691-
keyPrefix + KeyUtils.getVersionlessArtifactKey(repoSystem.createDependencyArtifact(dependency)),
692-
projectHash);
813+
keyPrefix + KeyUtils.getVersionlessArtifactKey(createDependencyArtifact(dependency)), projectHash);
693814
}
694815
return result;
695816
}
696817

697818
@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()) {
714834
throw new DependencyNotResolvedException("Cannot resolve in-project dependency: " + dependencyArtifact);
715835
}
716836

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);
720839
}
721840

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();
726842

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);
728852

729853
final HashAlgorithm algorithm = config.getHashFactory().createAlgorithm();
730854
final String hash = algorithm.hash(resolved.getFile().toPath());
731-
return DtoUtils.createDigestedFile(resolved, hash);
855+
return DtoUtils.createDigestedFile(artifact, hash);
732856
}
733857

734858
/**

0 commit comments

Comments
 (0)