Skip to content

Commit

Permalink
Get rid of usage of o.a.maven.repository.RepositorySystem
Browse files Browse the repository at this point in the history
  • Loading branch information
slawekjaranowski committed Jun 25, 2024
1 parent d2550ba commit 1a0e937
Show file tree
Hide file tree
Showing 72 changed files with 470 additions and 514 deletions.
6 changes: 6 additions & 0 deletions versions-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,12 @@
<artifactId>commons-io</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.codehaus.mojo.versions</groupId>
<artifactId>versions-test</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
*/

import java.io.BufferedInputStream;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
Expand Down Expand Up @@ -51,6 +52,8 @@
import org.apache.commons.lang3.tuple.Pair;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.ArtifactUtils;
import org.apache.maven.artifact.DefaultArtifact;
import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager;
import org.apache.maven.artifact.resolver.ArtifactResolutionException;
import org.apache.maven.artifact.versioning.ArtifactVersion;
import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
Expand All @@ -63,7 +66,6 @@
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.logging.Log;
import org.apache.maven.project.MavenProject;
import org.apache.maven.repository.RepositorySystem;
import org.apache.maven.wagon.Wagon;
import org.apache.maven.wagon.authentication.AuthenticationInfo;
import org.apache.maven.wagon.observers.Debug;
Expand All @@ -75,7 +77,6 @@
import org.codehaus.mojo.versions.ordering.VersionComparator;
import org.codehaus.mojo.versions.ordering.VersionComparators;
import org.codehaus.mojo.versions.utils.DefaultArtifactVersionCache;
import org.codehaus.mojo.versions.utils.DependencyBuilder;
import org.codehaus.mojo.versions.utils.DependencyComparator;
import org.codehaus.mojo.versions.utils.PluginComparator;
import org.codehaus.mojo.versions.utils.RegexUtils;
Expand All @@ -84,6 +85,7 @@
import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator;
import org.codehaus.plexus.util.StringUtils;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
import org.eclipse.aether.RepositorySystem;
import org.eclipse.aether.repository.AuthenticationContext;
import org.eclipse.aether.repository.RemoteRepository;
import org.eclipse.aether.repository.RepositoryPolicy;
Expand Down Expand Up @@ -122,9 +124,9 @@ RuleSet getRuleSet() {
*/
private RuleSet ruleSet;

private final RepositorySystem repositorySystem;
private final ArtifactHandlerManager artifactHandlerManager;

private final org.eclipse.aether.RepositorySystem aetherRepositorySystem;
private final RepositorySystem repositorySystem;

/**
* The {@link Log} to send log messages to.
Expand Down Expand Up @@ -160,13 +162,13 @@ RuleSet getRuleSet() {
* Private constructor used by the builder
*/
private DefaultVersionsHelper(
ArtifactHandlerManager artifactHandlerManager,
RepositorySystem repositorySystem,
org.eclipse.aether.RepositorySystem aetherRepositorySystem,
MavenSession mavenSession,
MojoExecution mojoExecution,
Log log) {
this.artifactHandlerManager = artifactHandlerManager;
this.repositorySystem = repositorySystem;
this.aetherRepositorySystem = aetherRepositorySystem;
this.mavenSession = mavenSession;
this.mojoExecution = mojoExecution;
this.log = log;
Expand Down Expand Up @@ -276,7 +278,7 @@ public ArtifactVersions lookupArtifactVersions(

return new ArtifactVersions(
artifact,
aetherRepositorySystem
repositorySystem
.resolveVersionRange(
mavenSession.getRepositorySession(),
new VersionRangeRequest(
Expand Down Expand Up @@ -356,7 +358,7 @@ private List<IgnoreVersion> getIgnoredVersions(Artifact artifact) {
@Override
public void resolveArtifact(Artifact artifact, boolean usePluginRepositories) throws ArtifactResolutionException {
try {
ArtifactResult artifactResult = aetherRepositorySystem.resolveArtifact(
ArtifactResult artifactResult = repositorySystem.resolveArtifact(
mavenSession.getRepositorySession(),
new ArtifactRequest(
toArtifact(artifact),
Expand Down Expand Up @@ -445,11 +447,7 @@ protected Rule getBestFitRule(String groupId, String artifactId) {

@Override
public Artifact createPluginArtifact(String groupId, String artifactId, String version) {
Plugin plugin = new Plugin();
plugin.setGroupId(groupId);
plugin.setArtifactId(artifactId);
plugin.setVersion(StringUtils.isNotBlank(version) ? version : "[0,]");
return repositorySystem.createPluginArtifact(plugin);
return createDependencyArtifact(groupId, artifactId, version, "maven-plugin", null, "runtime", false);
}

@Override
Expand All @@ -461,31 +459,37 @@ public Artifact createDependencyArtifact(
String classifier,
String scope,
boolean optional) {
return repositorySystem.createDependencyArtifact(DependencyBuilder.newBuilder()
.withGroupId(groupId)
.withArtifactId(artifactId)
.withType(type)
.withClassifier(classifier)
.withScope(scope)
.withOptional(optional)
.withVersion(StringUtils.isNotBlank(version) ? version : "[0,]")
.build());
}

@Override
public Artifact createDependencyArtifact(
String groupId, String artifactId, String version, String type, String classifier, String scope) {
return createDependencyArtifact(groupId, artifactId, version, type, classifier, scope, false);
try {
return new DefaultArtifact(
groupId,
artifactId,
VersionRange.createFromVersionSpec(StringUtils.isNotBlank(version) ? version : "[0,]"),
scope,
type,
classifier,
artifactHandlerManager.getArtifactHandler(type),
optional);
} catch (InvalidVersionSpecificationException e) {
// version should have a proper format
throw new RuntimeException(e);
}
}

@Override
public Artifact createDependencyArtifact(Dependency dependency) {
if (StringUtils.isBlank(dependency.getVersion())) {
dependency = dependency.clone();
dependency.setVersion("[,0]");
}

return repositorySystem.createDependencyArtifact(dependency);
Artifact artifact = createDependencyArtifact(
dependency.getGroupId(),
dependency.getArtifactId(),
dependency.getVersion(),
dependency.getType(),
dependency.getClassifier(),
dependency.getScope(),
false);

if (Artifact.SCOPE_SYSTEM.equals(dependency.getScope()) && dependency.getSystemPath() != null) {
artifact.setFile(new File(dependency.getSystemPath()));
}
return artifact;
}

@Override
Expand Down Expand Up @@ -710,15 +714,16 @@ public Map<Property, PropertyVersions> getVersionPropertiesMap(VersionProperties
* Builder class for {@linkplain DefaultVersionsHelper}
*/
public static class Builder {
private RepositorySystem repositorySystem;
private ArtifactHandlerManager artifactHandlerManager;
private Collection<String> ignoredVersions;
private RuleSet ruleSet;
private String serverId;
private String rulesUri;
private Log log;
private MavenSession mavenSession;
private MojoExecution mojoExecution;
private org.eclipse.aether.RepositorySystem aetherRepositorySystem;
private RepositorySystem repositorySystem;

private Map<String, Wagon> wagonMap;

public Builder() {}
Expand Down Expand Up @@ -919,8 +924,8 @@ public static Optional<String> protocol(final String url) {
return pos == -1 ? empty() : of(url.substring(0, pos).trim());
}

public Builder withRepositorySystem(RepositorySystem repositorySystem) {
this.repositorySystem = repositorySystem;
public Builder withArtifactHandlerManager(ArtifactHandlerManager artifactHandlerManager) {
this.artifactHandlerManager = artifactHandlerManager;
return this;
}

Expand Down Expand Up @@ -959,8 +964,8 @@ public Builder withMojoExecution(MojoExecution mojoExecution) {
return this;
}

public Builder withAetherRepositorySystem(org.eclipse.aether.RepositorySystem aetherRepositorySystem) {
this.aetherRepositorySystem = aetherRepositorySystem;
public Builder withRepositorySystem(RepositorySystem repositorySystem) {
this.repositorySystem = repositorySystem;
return this;
}

Expand All @@ -976,7 +981,7 @@ public Builder withWagonMap(Map<String, Wagon> wagonMap) {
*/
public DefaultVersionsHelper build() throws MojoExecutionException {
DefaultVersionsHelper instance = new DefaultVersionsHelper(
repositorySystem, aetherRepositorySystem, mavenSession, mojoExecution, log);
artifactHandlerManager, repositorySystem, mavenSession, mojoExecution, log);
if (ruleSet != null) {
if (!isBlank(rulesUri)) {
log.warn("rulesUri is ignored if rules are specified in pom or as parameters");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,21 +103,6 @@ Artifact createDependencyArtifact(
String scope,
boolean optional);

/**
* Shorthand method for <code>getArtifactFactory().createDependencyArtifact(...)</code>.
*
* @param groupId The group id.
* @param artifactId The artifact id.
* @param version The version.
* @param type The type.
* @param classifier The classifier.
* @param scope The scope.
* @return The corresponding dependency artifact.
* @since 1.0-beta-1
*/
Artifact createDependencyArtifact(
String groupId, String artifactId, String version, String type, String classifier, String scope);

/**
* Shorthand method for <code>repositorySystem.createDependencyArtifact(...)</code> which extracts the
* parameters from the Dependency instance.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
import org.apache.maven.plugin.logging.Log;
import org.apache.maven.plugin.logging.SystemStreamLog;
import org.apache.maven.project.MavenProject;
import org.apache.maven.repository.RepositorySystem;
import org.apache.maven.wagon.ConnectionException;
import org.apache.maven.wagon.ResourceDoesNotExistException;
import org.apache.maven.wagon.TransferFailedException;
Expand All @@ -53,6 +52,7 @@
import org.codehaus.mojo.versions.model.RuleSet;
import org.codehaus.mojo.versions.ordering.VersionComparators;
import org.eclipse.aether.DefaultRepositorySystemSession;
import org.eclipse.aether.RepositorySystem;
import org.eclipse.aether.repository.RemoteRepository;
import org.eclipse.aether.repository.RepositoryPolicy;
import org.eclipse.aether.resolution.VersionRangeRequest;
Expand All @@ -67,6 +67,7 @@
import static java.util.Collections.emptyList;
import static java.util.Collections.singletonList;
import static java.util.Collections.singletonMap;
import static org.codehaus.mojo.versions.utils.MockUtils.mockArtifactHandlerManager;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.equalTo;
Expand All @@ -90,7 +91,7 @@
class DefaultVersionsHelperTest {
@Test
public void testPerRuleVersionsIgnored() throws Exception {
final org.eclipse.aether.RepositorySystem repositorySystem = mock(org.eclipse.aether.RepositorySystem.class);
final RepositorySystem repositorySystem = mock(org.eclipse.aether.RepositorySystem.class);
final Artifact artifact = mock(Artifact.class);
when(artifact.getGroupId()).thenReturn("com.mycompany.maven");
when(artifact.getArtifactId()).thenReturn("artifact-one");
Expand Down Expand Up @@ -119,7 +120,7 @@ public void testPerRuleVersionsIgnored() throws Exception {

@Test
public void testGlobalRuleVersionsIgnored() throws Exception {
final org.eclipse.aether.RepositorySystem repositorySystem = mock(org.eclipse.aether.RepositorySystem.class);
final RepositorySystem repositorySystem = mock(org.eclipse.aether.RepositorySystem.class);
final Artifact artifact = mock(Artifact.class);
when(artifact.getGroupId()).thenReturn("other.company");
when(artifact.getArtifactId()).thenReturn("artifact-two");
Expand Down Expand Up @@ -249,8 +250,7 @@ private static Wagon mockFileWagon(URI rulesUri)
return fileWagon;
}

private DefaultVersionsHelper createHelper(org.eclipse.aether.RepositorySystem aetherRepositorySystem)
throws Exception {
private DefaultVersionsHelper createHelper(RepositorySystem repositorySystem) throws Exception {
final String resourcePath = "/" + getClass().getPackage().getName().replace('.', '/') + "/rules.xml";
final String rulesUri =
Objects.requireNonNull(getClass().getResource(resourcePath)).toExternalForm();
Expand All @@ -261,8 +261,7 @@ private DefaultVersionsHelper createHelper(org.eclipse.aether.RepositorySystem a
when(mavenSession.getRepositorySession()).thenReturn(new DefaultRepositorySystemSession());

return new DefaultVersionsHelper.Builder()
.withRepositorySystem(mock(RepositorySystem.class))
.withAetherRepositorySystem(aetherRepositorySystem)
.withRepositorySystem(repositorySystem)
.withWagonMap(singletonMap("file", mockFileWagon(new URI(rulesUri))))
.withServerId("")
.withRulesUri(rulesUri)
Expand Down Expand Up @@ -380,6 +379,22 @@ void testRemoteRepositoryWithNeverUpdatePolicyShouldBeChangToDaily() {
equalTo(RepositoryPolicy.UPDATE_POLICY_DAILY));
}

@Test
void createPluginArtifact() throws Exception {

DefaultVersionsHelper versionsHelper = new DefaultVersionsHelper.Builder()
.withArtifactHandlerManager(mockArtifactHandlerManager())
.build();

Artifact artifact = versionsHelper.createPluginArtifact("groupId", "artifactId", "version");
assertEquals("groupId", artifact.getGroupId());
assertEquals("artifactId", artifact.getArtifactId());
assertEquals("version", artifact.getVersion());
assertEquals("maven-plugin", artifact.getType());
assertEquals("runtime", artifact.getScope());
assertEquals("jar", artifact.getArtifactHandler().getExtension());
}

private static Version parseVersion(String version) {
try {
return new GenericVersionScheme().parseVersion(version);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.util.TreeSet;
import java.util.stream.Collectors;

import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager;
import org.apache.maven.artifact.versioning.ArtifactVersion;
import org.apache.maven.enforcer.rule.api.AbstractEnforcerRule;
import org.apache.maven.enforcer.rule.api.EnforcerRuleError;
Expand All @@ -38,7 +39,6 @@
import org.apache.maven.plugin.MojoExecution;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.project.MavenProject;
import org.apache.maven.repository.RepositorySystem;
import org.apache.maven.wagon.Wagon;
import org.codehaus.mojo.versions.api.ArtifactVersions;
import org.codehaus.mojo.versions.api.DefaultVersionsHelper;
Expand All @@ -47,6 +47,7 @@
import org.codehaus.mojo.versions.api.VersionsHelper;
import org.codehaus.mojo.versions.model.RuleSet;
import org.codehaus.mojo.versions.utils.DependencyComparator;
import org.eclipse.aether.RepositorySystem;

import static java.util.Collections.emptyList;
import static java.util.Collections.singletonList;
Expand Down Expand Up @@ -256,9 +257,9 @@ public class MaxDependencyUpdates extends AbstractEnforcerRule {

private final MavenProject project;

private final RepositorySystem repositorySystem;
private final ArtifactHandlerManager artifactHandlerManager;

private final org.eclipse.aether.RepositorySystem aetherRepositorySystem;
private final RepositorySystem repositorySystem;

private final Map<String, Wagon> wagonMap;

Expand All @@ -269,14 +270,14 @@ public class MaxDependencyUpdates extends AbstractEnforcerRule {
@Inject
public MaxDependencyUpdates(
MavenProject project,
ArtifactHandlerManager artifactHandlerManager,
RepositorySystem repositorySystem,
org.eclipse.aether.RepositorySystem aetherRepositorySystem,
Map<String, Wagon> wagonMap,
MavenSession mavenSession,
MojoExecution mojoExecution) {
this.project = project;
this.artifactHandlerManager = artifactHandlerManager;
this.repositorySystem = repositorySystem;
this.aetherRepositorySystem = aetherRepositorySystem;
this.wagonMap = wagonMap;
this.mavenSession = mavenSession;
this.mojoExecution = mojoExecution;
Expand All @@ -290,8 +291,8 @@ private VersionsHelper createVersionsHelper(String serverId, String rulesUri, Ru
throws EnforcerRuleError {
try {
return new DefaultVersionsHelper.Builder()
.withArtifactHandlerManager(artifactHandlerManager)
.withRepositorySystem(repositorySystem)
.withAetherRepositorySystem(aetherRepositorySystem)
.withWagonMap(wagonMap)
.withServerId(serverId)
.withRulesUri(rulesUri)
Expand Down
Loading

0 comments on commit 1a0e937

Please sign in to comment.