Skip to content

Commit

Permalink
Switch to maven 4 and the new api
Browse files Browse the repository at this point in the history
  • Loading branch information
gnodet committed Jul 21, 2022
1 parent bee2a14 commit 8436101
Show file tree
Hide file tree
Showing 30 changed files with 863 additions and 2,044 deletions.
117 changes: 44 additions & 73 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,10 @@ under the License.
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-plugins</artifactId>
<version>36</version>
<relativePath />
</parent>

<artifactId>maven-deploy-plugin</artifactId>
<version>3.0.1-SNAPSHOT</version>
<version>4.0.0-SNAPSHOT</version>
<packaging>maven-plugin</packaging>

<name>Apache Maven Deploy Plugin</name>
Expand Down Expand Up @@ -63,39 +62,20 @@ under the License.
</distributionManagement>

<properties>
<javaVersion>7</javaVersion>
<mavenVersion>3.2.5</mavenVersion>
<javaVersion>8</javaVersion>
<mavenVersion>4.0.0-alpha-1-SNAPSHOT</mavenVersion>
<slf4jVersion>1.7.5</slf4jVersion> <!-- Keep in sync with resolver used in maven above -->
<resolverVersion>1.0.0.v20140518</resolverVersion> <!-- Keep in sync with resolver used in maven above -->
<resolverVersion>1.8.1</resolverVersion> <!-- Keep in sync with resolver used in maven above -->
<project.build.outputTimestamp>2022-07-16T16:14:30Z</project.build.outputTimestamp>
</properties>

<dependencies>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
<artifactId>maven-api-core</artifactId>
<version>${mavenVersion}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-core</artifactId>
<version>${mavenVersion}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-model</artifactId>
<version>${mavenVersion}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-artifact</artifactId>
<version>${mavenVersion}</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
Expand All @@ -105,77 +85,58 @@ under the License.
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-utils</artifactId>
<version>3.3.1</version>
</dependency>
<dependency>
<groupId>org.eclipse.aether</groupId>
<artifactId>aether-api</artifactId>
<version>${resolverVersion}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.eclipse.aether</groupId>
<artifactId>aether-util</artifactId>
<version>${resolverVersion}</version>
<scope>compile</scope> <!-- To work in Maven versions older than 3.9.0 -->
</dependency>

<!-- dependencies to annotations -->
<dependency>
<groupId>org.apache.maven.plugin-tools</groupId>
<artifactId>maven-plugin-annotations</artifactId>
<scope>provided</scope>
<version>${mavenVersion}</version>
</dependency>

<!-- Test -->
<dependency>
<groupId>org.apache.maven.plugin-testing</groupId>
<artifactId>maven-plugin-testing-harness</artifactId>
<version>3.3.0</version>
<version>4.0.0-SNAPSHOT</version>
<scope>test</scope>
</dependency>
<dependency> <!-- used by maven-plugin-testing-harness, don't give it compile scope! -->
<groupId>org.apache.maven</groupId>
<artifactId>maven-compat</artifactId>
<artifactId>maven-core</artifactId>
<version>${mavenVersion}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-aether-provider</artifactId>
<artifactId>maven-resolver-provider</artifactId>
<version>${mavenVersion}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.aether</groupId>
<artifactId>aether-connector-basic</artifactId>
<version>${resolverVersion}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.aether</groupId>
<artifactId>aether-transport-file</artifactId>
<version>${resolverVersion}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.aether</groupId>
<artifactId>aether-transport-http</artifactId>
<version>${resolverVersion}</version>
<scope>test</scope>
</dependency>
<!-- <dependency>-->
<!-- <groupId>org.apache.maven.resolver</groupId>-->
<!-- <artifactId>maven-resolver-connector-basic</artifactId>-->
<!-- <version>${resolverVersion}</version>-->
<!-- <scope>test</scope>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>org.apache.maven.resolver</groupId>-->
<!-- <artifactId>maven-resolver-transport-file</artifactId>-->
<!-- <version>${resolverVersion}</version>-->
<!-- <scope>test</scope>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>org.apache.maven.resolver</groupId>-->
<!-- <artifactId>maven-resolver-transport-http</artifactId>-->
<!-- <version>${resolverVersion}</version>-->
<!-- <scope>test</scope>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>org.mockito</groupId>-->
<!-- <artifactId>mockito-core</artifactId>-->
<!-- <version>2.28.2</version>-->
<!-- <scope>test</scope>-->
<!-- </dependency>-->
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>2.28.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-nop</artifactId>
Expand Down Expand Up @@ -204,6 +165,16 @@ under the License.
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-plugin-plugin</artifactId>
<version>4.0.0-SNAPSHOT</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M7</version>
</plugin>
</plugins>
</build>
<profiles>
Expand Down
2 changes: 1 addition & 1 deletion src/it/deploy-at-end-fail/module1/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<version>1.2</version>
<version>3.0.0</version>
<executions>
<execution>
<id>enforce</id>
Expand Down
2 changes: 1 addition & 1 deletion src/it/deploy-at-end-pass/module1/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<version>1.2</version>
<version>3.0.0</version>
<executions>
<execution>
<id>enforce</id>
Expand Down
146 changes: 43 additions & 103 deletions src/main/java/org/apache/maven/plugins/deploy/AbstractDeployMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,28 @@
* under the License.
*/

import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.rtinfo.RuntimeInformation;
import org.eclipse.aether.RepositorySystem;
import org.eclipse.aether.RepositorySystemSession;
import org.eclipse.aether.deployment.DeployRequest;
import org.eclipse.aether.deployment.DeploymentException;
import org.eclipse.aether.repository.RemoteRepository;
import org.eclipse.aether.util.version.GenericVersionScheme;
import org.eclipse.aether.version.InvalidVersionSpecificationException;
import org.eclipse.aether.version.Version;
import org.apache.maven.api.RemoteRepository;
import org.apache.maven.api.Version;
import org.apache.maven.api.plugin.Mojo;
import org.apache.maven.api.Session;
import org.apache.maven.api.plugin.MojoException;
import org.apache.maven.api.plugin.annotations.Parameter;
import org.apache.maven.api.services.VersionParser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Abstract class for Deploy mojo's.
*/
public abstract class AbstractDeployMojo
extends AbstractMojo
implements Mojo
{
private static final String AFFECTED_MAVEN_PACKAGING = "maven-plugin";

private static final String FIXED_MAVEN_VERSION = "3.9.0";

protected final Logger logger = LoggerFactory.getLogger( getClass() );

/**
* Flag whether Maven is currently in online/offline mode.
*/
Expand All @@ -56,123 +56,63 @@ public abstract class AbstractDeployMojo
@Parameter( property = "retryFailedDeploymentCount", defaultValue = "1" )
private int retryFailedDeploymentCount;

@Component
private RuntimeInformation runtimeInformation;

@Parameter( defaultValue = "${session}", readonly = true, required = true )
protected MavenSession session;

@Component
protected RepositorySystem repositorySystem;

private static final String AFFECTED_MAVEN_PACKAGING = "maven-plugin";

private static final String FIXED_MAVEN_VERSION = "3.9.0";
protected Session session;

/* Setters and Getters */

void failIfOffline()
throws MojoFailureException
throws MojoException
{
if ( offline )
{
throw new MojoFailureException( "Cannot deploy artifacts when Maven is in offline mode" );
throw new MojoException( "Cannot deploy artifacts when Maven is in offline mode" );
}
}

public int getRetryFailedDeploymentCount()
{
return retryFailedDeploymentCount;
}

/**
* If this plugin used in pre-3.9.0 Maven, the packaging {@code maven-plugin} will not deploy G level metadata.
*/
protected void warnIfAffectedPackagingAndMaven( final String packaging )
{
if ( AFFECTED_MAVEN_PACKAGING.equals( packaging ) )
{
try
{
GenericVersionScheme versionScheme = new GenericVersionScheme();
Version fixedMavenVersion = versionScheme.parseVersion( FIXED_MAVEN_VERSION );
Version currentMavenVersion = versionScheme.parseVersion( runtimeInformation.getMavenVersion() );
if ( fixedMavenVersion.compareTo( currentMavenVersion ) > 0 )
{
getLog().warn( "" );
getLog().warn( "You are about to deploy a maven-plugin using Maven " + currentMavenVersion + "." );
getLog().warn( "This plugin should be used ONLY with Maven 3.9.0 and newer, as MNG-7055" );
getLog().warn( "is fixed in those versions of Maven only!" );
getLog().warn( "" );
}
}
catch ( InvalidVersionSpecificationException e )
VersionParser parser = session.getService( VersionParser.class );
Version fixedMavenVersion = parser.parseVersion( FIXED_MAVEN_VERSION );
Version currentMavenVersion = parser.parseVersion( session.getMavenVersion() );
if ( fixedMavenVersion.compareTo( currentMavenVersion ) > 0 )
{
// skip it: Generic does not throw, only API contains this exception
getLog().warn( "" );
getLog().warn( "You are about to deploy a maven-plugin using Maven " + currentMavenVersion + "." );
getLog().warn( "This plugin should be used ONLY with Maven 3.9.0 and newer, as MNG-7055" );
getLog().warn( "is fixed in those versions of Maven only!" );
getLog().warn( "" );
}
}
}

/**
* Creates resolver {@link RemoteRepository} equipped with needed whistles and bells.
*/
protected RemoteRepository getRemoteRepository( final String repositoryId, final String url )
protected RemoteRepository createDeploymentArtifactRepository( String id, String url )
{
RemoteRepository result = new RemoteRepository.Builder( repositoryId, "default", url ).build();

if ( result.getAuthentication() == null || result.getProxy() == null )
{
RemoteRepository.Builder builder = new RemoteRepository.Builder( result );

if ( result.getAuthentication() == null )
{
builder.setAuthentication( session.getRepositorySession().getAuthenticationSelector()
.getAuthentication( result ) );
}

if ( result.getProxy() == null )
{
builder.setProxy( session.getRepositorySession().getProxySelector().getProxy( result ) );
}

result = builder.build();
}
return getSession().createRemoteRepository( id, url );
}

return result;
protected Session getSession()
{
return session;
}

/**
* Handles high level retries (this was buried into MAT).
*/
protected void deploy( RepositorySystemSession session, DeployRequest deployRequest ) throws MojoExecutionException
protected Logger getLog()
{
int retryFailedDeploymentCounter = Math.max( 1, Math.min( 10, retryFailedDeploymentCount ) );
DeploymentException exception = null;
for ( int count = 0; count < retryFailedDeploymentCounter; count++ )
{
try
{
if ( count > 0 )
{
getLog().info( "Retrying deployment attempt " + ( count + 1 ) + " of "
+ retryFailedDeploymentCounter );
}

repositorySystem.deploy( session, deployRequest );
exception = null;
break;
}
catch ( DeploymentException e )
{
if ( count + 1 < retryFailedDeploymentCounter )
{
getLog().warn( "Encountered issue during deployment: " + e.getLocalizedMessage() );
getLog().debug( e );
}
if ( exception == null )
{
exception = e;
}
}
}
if ( exception != null )
{
throw new MojoExecutionException( exception.getMessage(), exception );
}
return logger;
}


}

0 comments on commit 8436101

Please sign in to comment.