From b6b6dd059690af8dde36bb453e8f3f902d647ff3 Mon Sep 17 00:00:00 2001 From: Andrzej Jarmoniuk Date: Fri, 25 Nov 2022 15:36:37 +0100 Subject: [PATCH] #704: Remove WagonManager --- .../versions/api/DefaultVersionsHelper.java | 176 +++++------------- .../markdown/max-dependency-updates.md.vm | 2 +- .../it/it-transporter-001/invoker.properties | 2 + .../src/it/it-transporter-001/pom.xml | 18 ++ .../src/it/it-transporter-001/rules.xml | 12 ++ .../src/it/it-transporter-001/verify.groovy | 3 + .../it/it-transporter-002/invoker.properties | 2 + .../src/it/it-transporter-002/pom.xml | 38 ++++ .../src/it/it-transporter-002/rules.xml | 12 ++ .../src/it/it-transporter-002/verify.groovy | 3 + ...AbstractVersionsDependencyUpdaterMojo.java | 4 +- .../mojo/versions/AbstractVersionsReport.java | 13 +- 12 files changed, 138 insertions(+), 147 deletions(-) create mode 100644 versions-maven-plugin/src/it/it-transporter-001/invoker.properties create mode 100644 versions-maven-plugin/src/it/it-transporter-001/pom.xml create mode 100644 versions-maven-plugin/src/it/it-transporter-001/rules.xml create mode 100644 versions-maven-plugin/src/it/it-transporter-001/verify.groovy create mode 100644 versions-maven-plugin/src/it/it-transporter-002/invoker.properties create mode 100644 versions-maven-plugin/src/it/it-transporter-002/pom.xml create mode 100644 versions-maven-plugin/src/it/it-transporter-002/rules.xml create mode 100644 versions-maven-plugin/src/it/it-transporter-002/verify.groovy diff --git a/versions-common/src/main/java/org/codehaus/mojo/versions/api/DefaultVersionsHelper.java b/versions-common/src/main/java/org/codehaus/mojo/versions/api/DefaultVersionsHelper.java index 85ce4a8aef..0c1d7ef504 100644 --- a/versions-common/src/main/java/org/codehaus/mojo/versions/api/DefaultVersionsHelper.java +++ b/versions-common/src/main/java/org/codehaus/mojo/versions/api/DefaultVersionsHelper.java @@ -21,10 +21,10 @@ import java.io.BufferedInputStream; import java.io.IOException; +import java.io.StringReader; +import java.net.URI; import java.net.URISyntaxException; import java.net.URL; -import java.nio.file.Files; -import java.nio.file.Path; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; @@ -35,6 +35,7 @@ import java.util.LinkedHashMap; import java.util.List; import java.util.Map; +import java.util.Objects; import java.util.Optional; import java.util.Set; import java.util.TreeMap; @@ -61,10 +62,6 @@ 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; -import org.apache.maven.wagon.proxy.ProxyInfo; import org.codehaus.mojo.versions.model.IgnoreVersion; import org.codehaus.mojo.versions.model.Rule; import org.codehaus.mojo.versions.model.RuleSet; @@ -80,17 +77,18 @@ 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.repository.AuthenticationContext; import org.eclipse.aether.repository.RemoteRepository; import org.eclipse.aether.resolution.ArtifactRequest; import org.eclipse.aether.resolution.ArtifactResult; import org.eclipse.aether.resolution.VersionRangeRequest; import org.eclipse.aether.resolution.VersionRangeResolutionException; +import org.eclipse.aether.spi.connector.transport.GetTask; +import org.eclipse.aether.spi.connector.transport.TransporterFactory; +import org.eclipse.aether.transfer.NoTransporterException; -import static java.util.Collections.singletonList; import static java.util.Optional.empty; import static java.util.Optional.of; -import static java.util.Optional.ofNullable; + import static org.apache.maven.RepositoryUtils.toArtifact; /** @@ -144,6 +142,7 @@ public class DefaultVersionsHelper * @since 2.12 */ private final Map artifactBestFitRule = new HashMap<>(); + /** * Private constructor used by the builder */ @@ -689,7 +688,6 @@ public static class Builder private MavenSession mavenSession; private MojoExecution mojoExecution; private org.eclipse.aether.RepositorySystem aetherRepositorySystem; - private Map wagonMap; public Builder() { @@ -766,93 +764,29 @@ private static RuleSet enrichRuleSet( Collection ignoredVersions, RuleSe private static class RulesUri { - String basePath; - String resource; + String baseUri; + URI fileUri; - private RulesUri( String basePath, String resource ) + private RulesUri( String baseUri, URI fileUri ) { - this.basePath = basePath; - this.resource = resource; + this.baseUri = baseUri; + this.fileUri = fileUri; } static RulesUri build( String rulesUri ) throws URISyntaxException { int split = rulesUri.lastIndexOf( '/' ); return split == -1 - ? new RulesUri( rulesUri, "" ) + ? new RulesUri( rulesUri, new URI( "" ) ) : new RulesUri( rulesUri.substring( 0, split ) + '/', - split + 1 < rulesUri.length() + new URI( split + 1 < rulesUri.length() ? rulesUri.substring( split + 1 ) - : "" ) ; + : "" ) ); } } - private RemoteRepository remoteRepository( RulesUri uri ) - { - RemoteRepository prototype = new RemoteRepository.Builder( serverId, null, uri.basePath ).build(); - RemoteRepository.Builder builder = new RemoteRepository.Builder( prototype ); - ofNullable( mavenSession.getRepositorySession().getProxySelector().getProxy( prototype ) ) - .ifPresent( builder::setProxy ); - ofNullable( mavenSession.getRepositorySession().getAuthenticationSelector().getAuthentication( prototype ) ) - .ifPresent( builder::setAuthentication ); - ofNullable( mavenSession.getRepositorySession().getMirrorSelector().getMirror( prototype ) ) - .ifPresent( mirror -> builder.setMirroredRepositories( singletonList( mirror ) ) ); - return builder.build(); - } - private Optional getProxyInfo( RemoteRepository repository ) - { - return ofNullable( repository.getProxy() ) - .map( proxy -> new ProxyInfo() - {{ - setHost( proxy.getHost() ); - setPort( proxy.getPort() ); - setType( proxy.getType() ); - ofNullable( proxy.getAuthentication() ) - .ifPresent( auth -> - { - try ( AuthenticationContext authCtx = AuthenticationContext - .forProxy( mavenSession.getRepositorySession(), repository ) ) - { - ofNullable( authCtx.get( AuthenticationContext.USERNAME ) ) - .ifPresent( this::setUserName ); - ofNullable( authCtx.get( AuthenticationContext.PASSWORD ) ) - .ifPresent( this::setPassword ); - ofNullable( authCtx.get( AuthenticationContext.NTLM_DOMAIN ) ) - .ifPresent( this::setNtlmDomain ); - ofNullable( authCtx.get( AuthenticationContext - .NTLM_WORKSTATION ) ).ifPresent( this::setNtlmHost ); - } - } ); - }} ); - } - - private Optional getAuthenticationInfo( RemoteRepository repository ) - { - return ofNullable( repository.getAuthentication() ) - .map( authentication -> new AuthenticationInfo() - {{ - try ( AuthenticationContext authCtx = AuthenticationContext - .forProxy( mavenSession.getRepositorySession(), repository ) ) - { - ofNullable( authCtx.get( AuthenticationContext.USERNAME ) ) - .ifPresent( this::setUserName ); - ofNullable( authCtx.get( AuthenticationContext.PASSWORD ) ) - .ifPresent( this::setPassword ); - ofNullable( authCtx.get( AuthenticationContext.PRIVATE_KEY_PASSPHRASE ) ) - .ifPresent( this::setPassphrase ); - ofNullable( authCtx.get( AuthenticationContext.PRIVATE_KEY_PATH ) ) - .ifPresent( this::setPrivateKey ); - } - }} ); - } - - private org.apache.maven.wagon.repository.Repository wagonRepository( RemoteRepository repository ) - { - return new org.apache.maven.wagon.repository.Repository( repository.getId(), repository.getUrl() ); - } - - private RuleSet getRulesUsingWagon() throws MojoExecutionException + private RuleSet getRulesUsingTransporter() { RulesUri uri; try @@ -865,58 +799,44 @@ private RuleSet getRulesUsingWagon() throws MojoExecutionException return null; } - RemoteRepository repository = remoteRepository( uri ); - return ofNullable( wagonMap.get( repository.getProtocol() ) ) - .map( wagon -> + RemoteRepository repository = new RemoteRepository.Builder( serverId, null, uri.baseUri ) + .build(); + return transporterFactoryMap + .values() + .stream() + // highest priority first -> reversing the order of arguments: + .sorted( ( f1, f2 ) -> Float.compare( f2.getPriority(), f1.getPriority() ) ) + .map( factory -> { - if ( log.isDebugEnabled() ) + try { - Debug debug = new Debug(); - wagon.addSessionListener( debug ); - wagon.addTransferListener( debug ); + return factory.newInstance( mavenSession.getRepositorySession(), repository ); } - + catch ( NoTransporterException e ) + { + log.warn( "No transporter possible for " + uri.baseUri + ": " + + e.getMessage() ); + return null; + } + } ) + .filter( Objects::nonNull ) + .map( transporter -> + { try { - Optional proxyInfo = getProxyInfo( repository ); - Optional authenticationInfo = getAuthenticationInfo( repository ); - if ( log.isDebugEnabled() ) - { - log.debug( "Connecting to remote repository \"" + repository.getId() + "\"" - + proxyInfo.map( pi -> " using proxy " + pi.getHost() + ":" - + pi.getPort() ).orElse( "" ) - + authenticationInfo.map( ai -> " as " + ai.getUserName() ).orElse( "" ) ); - } - wagon.connect( wagonRepository( repository ), getAuthenticationInfo( repository ) - .orElse( null ), getProxyInfo( repository ).orElse( null ) ); - try - { - Path tempFile = Files.createTempFile( "rules-", ".xml" ); - wagon.get( uri.resource, tempFile.toFile() ); - try ( BufferedInputStream is = new BufferedInputStream( - Files.newInputStream( tempFile ) ) ) - { - return new RuleXpp3Reader().read( is ); - } - finally - { - Files.deleteIfExists( tempFile ); - } - - } - finally - { - wagon.disconnect(); - } + GetTask getTask = new GetTask( uri.fileUri ); + transporter.get( getTask ); + return new RuleXpp3Reader().read( new StringReader( getTask.getDataString() ) ); } catch ( Exception e ) { - log.warn( e.getMessage() ); + log.warn( "Error while reading the rules string: " + e.getMessage() ); return null; } } ) - .orElseThrow( () -> new MojoExecutionException( "Could not load specified rules from " - + rulesUri ) ); + .filter( Objects::nonNull ) + .findFirst() + .orElse( null ); } public static Optional protocol( final String url ) @@ -981,12 +901,6 @@ public Builder withAetherRepositorySystem( org.eclipse.aether.RepositorySystem a return this; } - public Builder withWagonMap( Map wagonMap ) - { - this.wagonMap = wagonMap; - return this; - } - /** * Builds the constructed {@linkplain DefaultVersionsHelper} object * @return constructed {@linkplain DefaultVersionsHelper} @@ -1012,7 +926,7 @@ public DefaultVersionsHelper build() throws MojoExecutionException ? new RuleSet() : isClasspathUri( rulesUri ) ? getRulesFromClasspath( rulesUri, log ) - : getRulesUsingWagon(); + : getRulesUsingTransporter(); } if ( ignoredVersions != null && !ignoredVersions.isEmpty() ) { diff --git a/versions-enforcer/src/site/markdown/max-dependency-updates.md.vm b/versions-enforcer/src/site/markdown/max-dependency-updates.md.vm index d5912718f2..8f3158223b 100644 --- a/versions-enforcer/src/site/markdown/max-dependency-updates.md.vm +++ b/versions-enforcer/src/site/markdown/max-dependency-updates.md.vm @@ -46,7 +46,7 @@ The following parameters are supported by this rule: | `pluginDependencyExcludes` | (empty) | List of plugin dependency exclusion patterns. Only dependencies matching none of the patterns will be considered.
The wildcard (`*`) can be used as the only, first, last or both characters in each token. The version token does support version ranges. | | `pluginManagementDependencyIncludes` | `*` | List of plugin management dependency inclusion patterns. Only dependencies matching all the patterns will be considered.
The wildcard (`*`) can be used as the only, first, last or both characters in each token. The version token does support version ranges. | | `pluginManagementDependencyExcludes` | (empty) | List of plugin management dependency exclusion patterns. Only dependencies matching none of the patterns will be considered.
The wildcard (`*`) can be used as the only, first, last or both characters in each token. The version token does support version ranges. | -| `serverId` | `serverId` | *settings.xml*'s server id for the URL. This is used when Wagon needs extra authentication information. | +| `serverId` | `serverId` | *settings.xml*'s server id for the URL. This is used when there is extra authentication information needed. | | `rulesUri` | | URI of a ruleSet file containing the rules that control how to compare version numbers. The URI could be either a Wagon URI or a classpath URI (e.g. *classpath:\/\/\/package/sub/package/rules.xml*). | | `ruleSet` | | Allows specifying the `RuleSet` object describing rules on artifact versions to ignore when considering updates.
See: [Using the ruleSet element in the POM](../version-rules.html#Using_the_ruleSet_element_in_the_POM) | diff --git a/versions-maven-plugin/src/it/it-transporter-001/invoker.properties b/versions-maven-plugin/src/it/it-transporter-001/invoker.properties new file mode 100644 index 0000000000..1c628a5b17 --- /dev/null +++ b/versions-maven-plugin/src/it/it-transporter-001/invoker.properties @@ -0,0 +1,2 @@ +invoker.goals = ${project.groupId}:${project.artifactId}:${project.version}:use-latest-releases +invoker.mavenOpts = -Dmaven.version.rules=file:rules.xml diff --git a/versions-maven-plugin/src/it/it-transporter-001/pom.xml b/versions-maven-plugin/src/it/it-transporter-001/pom.xml new file mode 100644 index 0000000000..8263900678 --- /dev/null +++ b/versions-maven-plugin/src/it/it-transporter-001/pom.xml @@ -0,0 +1,18 @@ + + 4.0.0 + + localhost + it-transporter-001 + 1.0 + pom + + + + localhost + dummy-api + 1.0 + + + + diff --git a/versions-maven-plugin/src/it/it-transporter-001/rules.xml b/versions-maven-plugin/src/it/it-transporter-001/rules.xml new file mode 100644 index 0000000000..99d7cb705c --- /dev/null +++ b/versions-maven-plugin/src/it/it-transporter-001/rules.xml @@ -0,0 +1,12 @@ + + + + + 3.* + + + + diff --git a/versions-maven-plugin/src/it/it-transporter-001/verify.groovy b/versions-maven-plugin/src/it/it-transporter-001/verify.groovy new file mode 100644 index 0000000000..921f69ef90 --- /dev/null +++ b/versions-maven-plugin/src/it/it-transporter-001/verify.groovy @@ -0,0 +1,3 @@ +def project = new XmlSlurper().parse( new File( basedir, 'pom.xml' ) ) + +assert !( project.dependencies.dependency.find { node -> node.artifactId == 'dummy-api' }.version =~ /3.*/ ) diff --git a/versions-maven-plugin/src/it/it-transporter-002/invoker.properties b/versions-maven-plugin/src/it/it-transporter-002/invoker.properties new file mode 100644 index 0000000000..1c628a5b17 --- /dev/null +++ b/versions-maven-plugin/src/it/it-transporter-002/invoker.properties @@ -0,0 +1,2 @@ +invoker.goals = ${project.groupId}:${project.artifactId}:${project.version}:use-latest-releases +invoker.mavenOpts = -Dmaven.version.rules=file:rules.xml diff --git a/versions-maven-plugin/src/it/it-transporter-002/pom.xml b/versions-maven-plugin/src/it/it-transporter-002/pom.xml new file mode 100644 index 0000000000..dfaa164ae7 --- /dev/null +++ b/versions-maven-plugin/src/it/it-transporter-002/pom.xml @@ -0,0 +1,38 @@ + + 4.0.0 + + localhost + it-transporter-001 + 1.0 + pom + + + + localhost + dummy-api + 1.0 + + + + + + + + @project.groupId@ + @project.artifactId@ + @project.version@ + + + org.apache.maven.resolver + maven-resolver-transport-file + 1.8.2 + runtime + + + + + + + + diff --git a/versions-maven-plugin/src/it/it-transporter-002/rules.xml b/versions-maven-plugin/src/it/it-transporter-002/rules.xml new file mode 100644 index 0000000000..99d7cb705c --- /dev/null +++ b/versions-maven-plugin/src/it/it-transporter-002/rules.xml @@ -0,0 +1,12 @@ + + + + + 3.* + + + + diff --git a/versions-maven-plugin/src/it/it-transporter-002/verify.groovy b/versions-maven-plugin/src/it/it-transporter-002/verify.groovy new file mode 100644 index 0000000000..921f69ef90 --- /dev/null +++ b/versions-maven-plugin/src/it/it-transporter-002/verify.groovy @@ -0,0 +1,3 @@ +def project = new XmlSlurper().parse( new File( basedir, 'pom.xml' ) ) + +assert !( project.dependencies.dependency.find { node -> node.artifactId == 'dummy-api' }.version =~ /3.*/ ) diff --git a/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/AbstractVersionsDependencyUpdaterMojo.java b/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/AbstractVersionsDependencyUpdaterMojo.java index 04b5dc541f..7f0d746bfd 100644 --- a/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/AbstractVersionsDependencyUpdaterMojo.java +++ b/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/AbstractVersionsDependencyUpdaterMojo.java @@ -40,7 +40,6 @@ import org.apache.maven.repository.RepositorySystem; import org.apache.maven.shared.artifact.filter.PatternExcludesArtifactFilter; import org.apache.maven.shared.artifact.filter.PatternIncludesArtifactFilter; -import org.apache.maven.wagon.Wagon; import org.codehaus.mojo.versions.api.PomHelper; import org.codehaus.mojo.versions.api.recording.ChangeRecord; import org.codehaus.mojo.versions.api.recording.ChangeRecorder; @@ -158,10 +157,9 @@ public abstract class AbstractVersionsDependencyUpdaterMojo @Inject protected AbstractVersionsDependencyUpdaterMojo( RepositorySystem repositorySystem, org.eclipse.aether.RepositorySystem aetherRepositorySystem, - Map wagonMap, Map changeRecorders ) { - super( repositorySystem, aetherRepositorySystem, wagonMap, changeRecorders ); + super( repositorySystem, aetherRepositorySystem, changeRecorders ); } /** diff --git a/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/AbstractVersionsReport.java b/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/AbstractVersionsReport.java index b33e642016..8509212c62 100644 --- a/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/AbstractVersionsReport.java +++ b/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/AbstractVersionsReport.java @@ -37,7 +37,6 @@ import org.apache.maven.reporting.AbstractMavenReport; import org.apache.maven.reporting.MavenReportException; 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; import org.codehaus.mojo.versions.api.VersionRetrievalException; @@ -79,7 +78,7 @@ public abstract class AbstractVersionsReport protected org.eclipse.aether.RepositorySystem aetherRepositorySystem; /** - * settings.xml's server id for the URL. This is used when wagon needs extra authentication information. + * settings.xml's server id for the URL. This is used when there is extra authentication information needed. * * @since 1.0-alpha-3 */ @@ -164,24 +163,15 @@ public abstract class AbstractVersionsReport */ protected ReportRendererFactory rendererFactory; - /** - * (injected) map of {@link Wagon} instances - * - * @since 2.14.0 - */ - protected Map wagonMap; - // --------------------- GETTER / SETTER METHODS --------------------- protected AbstractVersionsReport( I18N i18n, RepositorySystem repositorySystem, org.eclipse.aether.RepositorySystem aetherRepositorySystem, - Map wagonMap, ReportRendererFactory rendererFactory ) { this.i18n = i18n; this.repositorySystem = repositorySystem; this.aetherRepositorySystem = aetherRepositorySystem; - this.wagonMap = wagonMap; this.rendererFactory = rendererFactory; } @@ -195,7 +185,6 @@ public VersionsHelper getHelper() helper = new DefaultVersionsHelper.Builder() .withRepositorySystem( repositorySystem ) .withAetherRepositorySystem( aetherRepositorySystem ) - .withWagonMap( wagonMap ) .withServerId( serverId ) .withRulesUri( rulesUri ) .withRuleSet( ruleSet )