Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MDEPLOY-296] Streamline plugin #26

Merged
merged 8 commits into from Jul 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 3 additions & 11 deletions pom.xml
Expand Up @@ -63,10 +63,10 @@ under the License.
</distributionManagement>

<properties>
<javaVersion>7</javaVersion>
<mavenVersion>3.2.5</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 -->
<javaVersion>7</javaVersion>
<project.build.outputTimestamp>2021-12-27T14:11:19Z</project.build.outputTimestamp>
</properties>

Expand Down Expand Up @@ -102,16 +102,6 @@ under the License.
<version>${slf4jVersion}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.maven.shared</groupId>
<artifactId>maven-artifact-transfer</artifactId>
<version>0.13.1</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-utils</artifactId>
Expand All @@ -121,11 +111,13 @@ under the License.
<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 -->
Expand Down
111 changes: 85 additions & 26 deletions src/main/java/org/apache/maven/plugins/deploy/AbstractDeployMojo.java
Expand Up @@ -19,16 +19,18 @@
* under the License.
*/

import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.repository.ArtifactRepositoryPolicy;
import org.apache.maven.artifact.repository.MavenArtifactRepository;
import org.apache.maven.artifact.repository.layout.DefaultRepositoryLayout;
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;
Expand All @@ -37,9 +39,8 @@
* Abstract class for Deploy mojo's.
*/
public abstract class AbstractDeployMojo
extends AbstractMojo
extends AbstractMojo
{

/**
* Flag whether Maven is currently in online/offline mode.
*/
Expand All @@ -49,17 +50,20 @@ public abstract class AbstractDeployMojo
/**
* Parameter used to control how many times a failed deployment will be retried before giving up and failing. If a
* value outside the range 1-10 is specified it will be pulled to the nearest value within the range 1-10.
*
*
* @since 2.7
*/
@Parameter( property = "retryFailedDeploymentCount", defaultValue = "1" )
private int retryFailedDeploymentCount;

@Component
private RuntimeInformation runtimeInformation;

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

@Component
private RuntimeInformation runtimeInformation;
protected RepositorySystem repositorySystem;

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

Expand All @@ -68,30 +72,17 @@ public abstract class AbstractDeployMojo
/* Setters and Getters */

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

int getRetryFailedDeploymentCount()
{
return retryFailedDeploymentCount;
}

protected ArtifactRepository createDeploymentArtifactRepository( String id, String url )
{
return new MavenArtifactRepository( id, url, new DefaultRepositoryLayout(), new ArtifactRepositoryPolicy(),
new ArtifactRepositoryPolicy() );
}

protected final MavenSession getSession()
{
return session;
}

/**
* 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 ) )
Expand All @@ -116,4 +107,72 @@ protected void warnIfAffectedPackagingAndMaven( final String packaging )
}
}
}

/**
* Creates resolver {@link RemoteRepository} equipped with needed whistles and bells.
*/
protected RemoteRepository getRemoteRepository( final String repositoryId, final 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 result;
}

/**
* Handles high level retries (this was buried into MAT).
*/
protected void deploy( RepositorySystemSession session, DeployRequest deployRequest ) throws MojoExecutionException
{
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 );
}
}
}