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-295] Drop MAT #25

Closed
wants to merge 3 commits into from
Closed
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
122 changes: 105 additions & 17 deletions src/main/java/org/apache/maven/plugins/deploy/AbstractDeployMojo.java
Expand Up @@ -19,16 +19,28 @@
* under the License.
*/

import java.util.List;

import org.apache.maven.RepositoryUtils;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.metadata.ArtifactMetadata;
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.project.artifact.ProjectArtifactMetadata;
import org.apache.maven.rtinfo.RuntimeInformation;
import org.eclipse.aether.RepositorySystem;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and soon we will change to org.apache.maven package... ?
maybe think in the other way… why we have not yet change package from maven-resolver?

Copy link
Member Author

@cstamas cstamas Jun 21, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unsure what you talk about:

  • if you refer to package change for resolver, yes, it will happen, somewhere around Maven 5, but that will not affect you, as
  • Maven 4 introduces new API and seals Maven internals off from plugins, hence from 4.1 or so, plugin will not "tamper" with resolver and other internal things of Maven anymore, will have to go for Maven API
  • in short, resolver package change, if that's you refer to will NOT affect you, and if the "soon we will change to" refers to Maven API 4.x, is still WIP and of course non existent in Maven 3.x line.

import org.eclipse.aether.deployment.DeployRequest;
import org.eclipse.aether.deployment.DeploymentException;
import org.eclipse.aether.repository.RemoteRepository;
import org.eclipse.aether.util.artifact.SubArtifact;
import org.eclipse.aether.util.version.GenericVersionScheme;
import org.eclipse.aether.version.InvalidVersionSpecificationException;
import org.eclipse.aether.version.Version;
Expand All @@ -37,9 +49,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 +60,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,28 +82,18 @@ 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;
new ArtifactRepositoryPolicy() );
}

protected void warnIfAffectedPackagingAndMaven( final String packaging )
Expand All @@ -116,4 +120,88 @@ protected void warnIfAffectedPackagingAndMaven( final String packaging )
}
}
}

private RemoteRepository getRemoteRepository( ArtifactRepository remoteRepository )
{
RemoteRepository aetherRepo = RepositoryUtils.toRepo( remoteRepository );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we still use the old term aether?


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

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

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

aetherRepo = builder.build();
}

return aetherRepo;
}

protected DeployRequest deployRequest( ArtifactRepository repository, List<Artifact> artifacts )
{
DeployRequest deployRequest = new DeployRequest();
deployRequest.setRepository( getRemoteRepository( repository ) );
for ( Artifact artifact : artifacts )
{
org.eclipse.aether.artifact.Artifact aetherArtifact = RepositoryUtils.toArtifact( artifact );
deployRequest.addArtifact( aetherArtifact );

for ( ArtifactMetadata metadata : artifact.getMetadataList() )
{
if ( metadata instanceof ProjectArtifactMetadata )
{
org.eclipse.aether.artifact.Artifact pomArtifact = new SubArtifact( aetherArtifact, "", "pom" );
pomArtifact = pomArtifact.setFile( ( (ProjectArtifactMetadata) metadata ).getFile() );
deployRequest.addArtifact( pomArtifact );
}
}
}
return deployRequest;
}

protected void deploy( 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.getRepositorySession(), deployRequest );
exception = null;
break;
}
catch ( DeploymentException e )
{
if ( count + 1 < retryFailedDeploymentCounter )
{
getLog().warn( "Encountered issue during deployment: " + e.getLocalizedMessage() );
getLog().debug( e.getMessage() );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Honestly, just use warn with e and that's it. No brain magic.

}
if ( exception == null )
{
exception = e;
}
}
}
if ( exception != null )
{
throw new MojoExecutionException( exception.getMessage(), exception );
}
}
}
67 changes: 12 additions & 55 deletions src/main/java/org/apache/maven/plugins/deploy/DeployFileMojo.java
Expand Up @@ -34,6 +34,7 @@
import java.util.jar.JarFile;
import java.util.regex.Pattern;

import org.apache.maven.RepositoryUtils;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.model.Model;
Expand All @@ -54,11 +55,6 @@
import org.apache.maven.project.ProjectBuilder;
import org.apache.maven.project.ProjectBuildingException;
import org.apache.maven.project.artifact.ProjectArtifactMetadata;
import org.apache.maven.shared.transfer.artifact.DefaultArtifactCoordinate;
import org.apache.maven.shared.transfer.artifact.deploy.ArtifactDeployer;
import org.apache.maven.shared.transfer.artifact.deploy.ArtifactDeployerException;
import org.apache.maven.shared.transfer.repository.RepositoryManager;
import org.apache.maven.shared.utils.Os;
import org.codehaus.plexus.util.FileUtils;
import org.codehaus.plexus.util.IOUtil;
import org.codehaus.plexus.util.ReaderFactory;
Expand All @@ -75,8 +71,7 @@
public class DeployFileMojo
extends AbstractDeployMojo
{
@Component
private ArtifactDeployer artifactDeployer;
private static final String LINE_SEP = System.getProperty( "line.separator" );

/**
* Used for attaching the artifacts to deploy to the project.
Expand Down Expand Up @@ -177,16 +172,6 @@ public class DeployFileMojo
@Parameter( property = "classifier" )
private String classifier;

/**
* Whether to deploy snapshots with a unique version or not.
*
* @deprecated As of Maven 3, this isn't supported anymore and this parameter is only present to break the build if
* you use it!
*/
@Parameter( property = "uniqueVersion" )
@Deprecated
private Boolean uniqueVersion;

/**
* A comma separated list of types for each of the extra side artifacts to deploy. If there is a mis-match in the
* number of entries in {@link #files} or {@link #classifiers}, then an error will be raised.
Expand All @@ -208,9 +193,6 @@ public class DeployFileMojo
@Parameter( property = "files" )
private String files;

@Component
private RepositoryManager repoManager;

void initProperties()
throws MojoExecutionException
{
Expand Down Expand Up @@ -310,13 +292,6 @@ void initProperties()
public void execute()
throws MojoExecutionException, MojoFailureException
{
if ( uniqueVersion != null )
{
throw new MojoExecutionException( "You are using 'uniqueVersion' which has been removed"
+ " from the maven-deploy-plugin. "
+ "Please see the >>Major Version Upgrade to version 3.0.0<< on the plugin site." );
}

failIfOffline();

if ( !file.exists() )
Expand All @@ -338,7 +313,7 @@ public void execute()
MavenProject project = createMavenProject();
Artifact artifact = project.getArtifact();

if ( file.equals( getLocalRepoFile() ) )
if ( file.equals( getLocalRepoFile( artifact ) ) )
{
throw new MojoFailureException( "Cannot deploy artifact from the local repository: " + file );
}
Expand Down Expand Up @@ -472,23 +447,10 @@ public void execute()
}
}

List<Artifact> attachedArtifacts = project.getAttachedArtifacts();
deployableArtifacts.addAll( project.getAttachedArtifacts() );

for ( Artifact attached : attachedArtifacts )
{
deployableArtifacts.add( attached );
}

try
{
warnIfAffectedPackagingAndMaven( packaging );
artifactDeployer.deploy( getSession().getProjectBuildingRequest(), deploymentRepository,
deployableArtifacts );
}
catch ( ArtifactDeployerException e )
{
throw new MojoExecutionException( e.getMessage(), e );
}
warnIfAffectedPackagingAndMaven( packaging );
deploy( deployRequest( deploymentRepository, deployableArtifacts ) );
}

/**
Expand All @@ -513,7 +475,7 @@ private MavenProject createMavenProject()
+ "</groupId>" + "<artifactId>" + artifactId + "</artifactId>" + "<version>" + version + "</version>"
+ "<packaging>" + ( classifier == null ? packaging : "pom" ) + "</packaging>" + "</project>" );
DefaultProjectBuildingRequest buildingRequest =
new DefaultProjectBuildingRequest( getSession().getProjectBuildingRequest() );
new DefaultProjectBuildingRequest( session.getProjectBuildingRequest() );
buildingRequest.setProcessPlugins( false );
try
{
Expand All @@ -523,7 +485,7 @@ private MavenProject createMavenProject()
{
if ( e.getCause() instanceof ModelBuildingException )
{
throw new MojoExecutionException( "The artifact information is not valid:" + Os.LINE_SEP
throw new MojoExecutionException( "The artifact information is not valid:" + LINE_SEP
+ e.getCause().getMessage() );
}
throw new MojoFailureException( "Unable to create the project.", e );
Expand All @@ -536,16 +498,11 @@ private MavenProject createMavenProject()
*
* @return The absolute path to the artifact when installed, never <code>null</code>.
*/
private File getLocalRepoFile()
private File getLocalRepoFile( Artifact artifact )
{
DefaultArtifactCoordinate coordinate = new DefaultArtifactCoordinate();
coordinate.setGroupId( groupId );
coordinate.setArtifactId( artifactId );
coordinate.setVersion( version );
coordinate.setClassifier( classifier );
coordinate.setExtension( packaging );
String path = repoManager.getPathForLocalArtifact( getSession().getProjectBuildingRequest(), coordinate );
return new File( repoManager.getLocalRepositoryBasedir( getSession().getProjectBuildingRequest() ), path );
String path = session.getRepositorySession().getLocalRepositoryManager().getPathForLocalArtifact(
RepositoryUtils.toArtifact( artifact ) );
return new File( session.getRepositorySession().getLocalRepository().getBasedir(), path );
}

/**
Expand Down