Skip to content

Commit

Permalink
[MDEPLOY-296] Streamline plugin (#26)
Browse files Browse the repository at this point in the history
Original plugin made hoops and loops, instead to perform what it needed to perform. Partly to blame this was unfinished state of MAT API (it was able to install project only).

Deploying project is needed in DeployMojo, but DeployFileMojo was forced to make hoops and loops due this, as it was passed one file (and maybe pomFile), and it was forced to create "fake" project, decorate and fake setup it with all whistle and bells, only to get it via MAT to resolver that would "decompose" it back into set of artifacts needing a deploy. So it went this file-artifact-project-artifact route, that made all the logic fragile and overly complicated.

This PR completely reworks m-deploy-p making it (almost trivially) simple: it does what it needs to do, without any fuss, and does it in streamlined way: No fuss, no magic, no fake project building etc.
  • Loading branch information
cstamas committed Jul 12, 2022
1 parent 89c28fb commit 16541da
Show file tree
Hide file tree
Showing 8 changed files with 407 additions and 408 deletions.
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 );
}
}
}

0 comments on commit 16541da

Please sign in to comment.