From b9d651d64a66ff42da07651654a0ae918940473a Mon Sep 17 00:00:00 2001 From: Tamas Cservenak Date: Mon, 20 Jun 2022 16:22:02 +0200 Subject: [PATCH 1/2] Drop MAT Now that Maven is 3.2.5 no need for MAT anymore. --- pom.xml | 14 +-- .../plugins/deploy/AbstractDeployMojo.java | 93 +++++++++++++-- .../maven/plugins/deploy/DeployFileMojo.java | 67 ++--------- .../maven/plugins/deploy/DeployMojo.java | 112 +++++++++--------- .../plugins/deploy/DeployFileMojoTest.java | 5 +- .../deploy/DeployFileMojoUnitTest.java | 33 +++--- .../maven/plugins/deploy/DeployMojoTest.java | 80 ++++++------- .../deploy/stubs/ArtifactDeployerStub.java | 47 -------- 8 files changed, 208 insertions(+), 243 deletions(-) delete mode 100644 src/test/java/org/apache/maven/plugins/deploy/stubs/ArtifactDeployerStub.java diff --git a/pom.xml b/pom.xml index 1284f130..385b4d9f 100644 --- a/pom.xml +++ b/pom.xml @@ -63,10 +63,10 @@ under the License. + 7 3.2.5 1.7.5 1.0.0.v20140518 - 7 2021-12-27T14:11:19Z @@ -102,16 +102,6 @@ under the License. ${slf4jVersion} provided - - org.apache.maven.shared - maven-artifact-transfer - 0.13.1 - - - commons-io - commons-io - 2.6 - org.codehaus.plexus plexus-utils @@ -121,11 +111,13 @@ under the License. org.eclipse.aether aether-api ${resolverVersion} + provided org.eclipse.aether aether-util ${resolverVersion} + compile diff --git a/src/main/java/org/apache/maven/plugins/deploy/AbstractDeployMojo.java b/src/main/java/org/apache/maven/plugins/deploy/AbstractDeployMojo.java index 3939a8ea..0466495b 100644 --- a/src/main/java/org/apache/maven/plugins/deploy/AbstractDeployMojo.java +++ b/src/main/java/org/apache/maven/plugins/deploy/AbstractDeployMojo.java @@ -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; +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; @@ -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. */ @@ -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"; @@ -68,7 +82,7 @@ public abstract class AbstractDeployMojo /* Setters and Getters */ void failIfOffline() - throws MojoFailureException + throws MojoFailureException { if ( offline ) { @@ -84,12 +98,7 @@ int getRetryFailedDeploymentCount() 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 ) @@ -116,4 +125,64 @@ protected void warnIfAffectedPackagingAndMaven( final String packaging ) } } } + + private RemoteRepository getRemoteRepository( ArtifactRepository remoteRepository ) + { + RemoteRepository aetherRepo = RepositoryUtils.toRepo( remoteRepository ); + + 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 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 + { + try + { + repositorySystem.deploy( session.getRepositorySession(), deployRequest ); + } + catch ( DeploymentException e ) + { + throw new MojoExecutionException( e.getMessage(), e ); + } + } } diff --git a/src/main/java/org/apache/maven/plugins/deploy/DeployFileMojo.java b/src/main/java/org/apache/maven/plugins/deploy/DeployFileMojo.java index 12267e7e..eb2e42dd 100644 --- a/src/main/java/org/apache/maven/plugins/deploy/DeployFileMojo.java +++ b/src/main/java/org/apache/maven/plugins/deploy/DeployFileMojo.java @@ -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; @@ -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; @@ -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. @@ -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. @@ -208,9 +193,6 @@ public class DeployFileMojo @Parameter( property = "files" ) private String files; - @Component - private RepositoryManager repoManager; - void initProperties() throws MojoExecutionException { @@ -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() ) @@ -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 ); } @@ -472,23 +447,10 @@ public void execute() } } - List 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 ) ); } /** @@ -513,7 +475,7 @@ private MavenProject createMavenProject() + "" + "" + artifactId + "" + "" + version + "" + "" + ( classifier == null ? packaging : "pom" ) + "" + "" ); DefaultProjectBuildingRequest buildingRequest = - new DefaultProjectBuildingRequest( getSession().getProjectBuildingRequest() ); + new DefaultProjectBuildingRequest( session.getProjectBuildingRequest() ); buildingRequest.setProcessPlugins( false ); try { @@ -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 ); @@ -536,16 +498,11 @@ private MavenProject createMavenProject() * * @return The absolute path to the artifact when installed, never null. */ - 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 ); } /** diff --git a/src/main/java/org/apache/maven/plugins/deploy/DeployMojo.java b/src/main/java/org/apache/maven/plugins/deploy/DeployMojo.java index 4aaa971a..cae73c44 100644 --- a/src/main/java/org/apache/maven/plugins/deploy/DeployMojo.java +++ b/src/main/java/org/apache/maven/plugins/deploy/DeployMojo.java @@ -19,6 +19,7 @@ * under the License. */ +import java.io.File; import java.util.ArrayList; import java.util.Collections; import java.util.List; @@ -26,20 +27,17 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; +import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.ArtifactUtils; import org.apache.maven.artifact.repository.ArtifactRepository; 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.LifecyclePhase; import org.apache.maven.plugins.annotations.Mojo; import org.apache.maven.plugins.annotations.Parameter; import org.apache.maven.project.MavenProject; -import org.apache.maven.project.ProjectBuildingRequest; -import org.apache.maven.shared.transfer.artifact.deploy.ArtifactDeployerException; -import org.apache.maven.shared.transfer.project.NoFileAssignedException; -import org.apache.maven.shared.transfer.project.deploy.ProjectDeployer; -import org.apache.maven.shared.transfer.project.deploy.ProjectDeployerRequest; +import org.apache.maven.project.artifact.ProjectArtifactMetadata; +import org.eclipse.aether.deployment.DeployRequest; /** * Deploys an artifact to remote repository. @@ -61,8 +59,8 @@ public class DeployMojo */ private static final AtomicInteger READYPROJECTSCOUNTER = new AtomicInteger(); - private static final List DEPLOYREQUESTS = - Collections.synchronizedList( new ArrayList() ); + private static final List DEPLOYREQUESTS = + Collections.synchronizedList( new ArrayList() ); /** */ @@ -137,12 +135,6 @@ public class DeployMojo @Parameter( property = "maven.deploy.skip", defaultValue = "false" ) private String skip = Boolean.FALSE.toString(); - /** - * Component used to deploy project. - */ - @Component - private ProjectDeployer projectDeployer; - public void execute() throws MojoExecutionException, MojoFailureException { @@ -158,26 +150,59 @@ public void execute() { failIfOffline(); - // CHECKSTYLE_OFF: LineLength - // @formatter:off - ProjectDeployerRequest pdr = new ProjectDeployerRequest() - .setProject( project ) - .setRetryFailedDeploymentCount( getRetryFailedDeploymentCount() ) - .setAltReleaseDeploymentRepository( altReleaseDeploymentRepository ) - .setAltSnapshotDeploymentRepository( altSnapshotDeploymentRepository ) - .setAltDeploymentRepository( altDeploymentRepository ); - // @formatter:on - // CHECKSTYLE_ON: LineLength + ArrayList deployableArtifacts = new ArrayList<>(); + + Artifact artifact = project.getArtifact(); + String packaging = project.getPackaging(); + File pomFile = project.getFile(); + List attachedArtifacts = project.getAttachedArtifacts(); + + // Deploy the POM + boolean isPomArtifact = "pom".equals( packaging ); + if ( isPomArtifact ) + { + artifact.setFile( pomFile ); + } + else + { + ProjectArtifactMetadata metadata = new ProjectArtifactMetadata( artifact, pomFile ); + artifact.addMetadata( metadata ); + } + + if ( isPomArtifact ) + { + deployableArtifacts.add( artifact ); + } + else + { + File file = artifact.getFile(); + + if ( file != null && file.isFile() ) + { + deployableArtifacts.add( artifact ); + } + else if ( !attachedArtifacts.isEmpty() ) + { + throw new MojoExecutionException( "The packaging plugin for this project did not assign " + + "a main file to the project but it has attachments. Change packaging to 'pom'." ); + } + else + { + throw new MojoExecutionException( "The packaging for this project did not assign " + + "a file to the build artifact" ); + } + } + deployableArtifacts.addAll( attachedArtifacts ); - ArtifactRepository repo = getDeploymentRepository( pdr ); + DeployRequest deployRequest = deployRequest( getDeploymentRepository(), deployableArtifacts ); if ( !deployAtEnd ) { - deployProject( getSession().getProjectBuildingRequest(), pdr, repo ); + deploy( deployRequest ); } else { - DEPLOYREQUESTS.add( pdr ); + DEPLOYREQUESTS.add( deployRequest ); addedDeployRequest = true; } } @@ -189,9 +214,7 @@ public void execute() { while ( !DEPLOYREQUESTS.isEmpty() ) { - ArtifactRepository repo = getDeploymentRepository( DEPLOYREQUESTS.get( 0 ) ); - - deployProject( getSession().getProjectBuildingRequest(), DEPLOYREQUESTS.remove( 0 ), repo ); + deploy( DEPLOYREQUESTS.remove( 0 ) ); } } } @@ -202,34 +225,13 @@ else if ( addedDeployRequest ) } } - private void deployProject( ProjectBuildingRequest pbr, ProjectDeployerRequest pir, ArtifactRepository repo ) - throws MojoFailureException, MojoExecutionException - { - try - { - warnIfAffectedPackagingAndMaven( pir.getProject().getPackaging() ); - projectDeployer.deploy( pbr, pir, repo ); - } - catch ( NoFileAssignedException e ) - { - throw new MojoExecutionException( "NoFileAssignedException", e ); - } - catch ( ArtifactDeployerException e ) - { - throw new MojoExecutionException( "ArtifactDeployerException", e ); - } - - } - - ArtifactRepository getDeploymentRepository( ProjectDeployerRequest pdr ) + /** + * Visible for testing. + */ + ArtifactRepository getDeploymentRepository() throws MojoExecutionException, MojoFailureException { - MavenProject project = pdr.getProject(); - String altDeploymentRepository = pdr.getAltDeploymentRepository(); - String altReleaseDeploymentRepository = pdr.getAltReleaseDeploymentRepository(); - String altSnapshotDeploymentRepository = pdr.getAltSnapshotDeploymentRepository(); - ArtifactRepository repo = null; String altDeploymentRepo; diff --git a/src/test/java/org/apache/maven/plugins/deploy/DeployFileMojoTest.java b/src/test/java/org/apache/maven/plugins/deploy/DeployFileMojoTest.java index 371838bd..d8538705 100644 --- a/src/test/java/org/apache/maven/plugins/deploy/DeployFileMojoTest.java +++ b/src/test/java/org/apache/maven/plugins/deploy/DeployFileMojoTest.java @@ -97,7 +97,8 @@ public void testBasicDeployFile() DefaultRepositorySystemSession repositorySession = new DefaultRepositorySystemSession(); repositorySession.setLocalRepositoryManager( new SimpleLocalRepositoryManagerFactory().newInstance( repositorySession, new LocalRepository( LOCAL_REPO ) ) ); when( buildingRequest.getRepositorySession() ).thenReturn( repositorySession ); - + when( session.getRepositorySession() ).thenReturn( repositorySession ); + String groupId = (String) getVariableValueFromObject( mojo, "groupId" ); String artifactId = (String) getVariableValueFromObject( mojo, "artifactId" ); @@ -199,6 +200,7 @@ public void testDeployIfClassifierIsSet() DefaultRepositorySystemSession repositorySession = new DefaultRepositorySystemSession(); repositorySession.setLocalRepositoryManager( new SimpleLocalRepositoryManagerFactory().newInstance( repositorySession, new LocalRepository( LOCAL_REPO ) ) ); when( buildingRequest.getRepositorySession() ).thenReturn( repositorySession ); + when( session.getRepositorySession() ).thenReturn( repositorySession ); String classifier = ( String ) getVariableValueFromObject( mojo, "classifier" ); @@ -248,6 +250,7 @@ public void testDeployIfArtifactIsNotJar() DefaultRepositorySystemSession repositorySession = new DefaultRepositorySystemSession(); repositorySession.setLocalRepositoryManager( new SimpleLocalRepositoryManagerFactory().newInstance( repositorySession, new LocalRepository( LOCAL_REPO ) ) ); when( buildingRequest.getRepositorySession() ).thenReturn( repositorySession ); + when( session.getRepositorySession() ).thenReturn( repositorySession ); String groupId = (String) getVariableValueFromObject( mojo, "groupId" ); diff --git a/src/test/java/org/apache/maven/plugins/deploy/DeployFileMojoUnitTest.java b/src/test/java/org/apache/maven/plugins/deploy/DeployFileMojoUnitTest.java index 6131f79d..a7bbd8b7 100644 --- a/src/test/java/org/apache/maven/plugins/deploy/DeployFileMojoUnitTest.java +++ b/src/test/java/org/apache/maven/plugins/deploy/DeployFileMojoUnitTest.java @@ -19,36 +19,27 @@ * under the License. */ -import junit.framework.Test; -import junit.framework.TestCase; -import junit.framework.TestSuite; import org.apache.maven.model.Model; import org.apache.maven.model.Parent; import org.apache.maven.plugin.MojoExecutionException; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; import java.io.File; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + /** * @author Jerome Lacoste */ public class DeployFileMojoUnitTest - extends TestCase { - public static void main( String[] args ) - { - junit.textui.TestRunner.run( suite() ); - } - - public static Test suite() - { - TestSuite suite = new TestSuite( DeployFileMojoUnitTest.class ); - - return suite; - } - MockDeployFileMojo mojo; Parent parent; + @Before public void setUp() { Model pomModel = new Model(); @@ -62,12 +53,13 @@ public void setUp() mojo = new MockDeployFileMojo( pomModel ); } + @After public void tearDown() { mojo = null; } - class MockDeployFileMojo extends DeployFileMojo { + static class MockDeployFileMojo extends DeployFileMojo { private Model model; public MockDeployFileMojo(Model model) { @@ -83,6 +75,7 @@ protected Model readModel(File pomFile) throws MojoExecutionException { } } + @Test public void testProcessPomFromPomFileWithParent1() throws MojoExecutionException { mojo.setPomFile( new File( "foo.bar" ) ); @@ -98,6 +91,7 @@ public void testProcessPomFromPomFileWithParent1() throws MojoExecutionException checkMojoProperties("parentGroup", null, "parentVersion", null); } + @Test public void testProcessPomFromPomFileWithParent2() throws MojoExecutionException { mojo.setPomFile( new File( "foo.bar" ) ); @@ -113,6 +107,7 @@ public void testProcessPomFromPomFileWithParent2() throws MojoExecutionException } + @Test public void testProcessPomFromPomFileWithParent3() throws MojoExecutionException { mojo.setPomFile( new File( "foo.bar" ) ); @@ -127,6 +122,7 @@ public void testProcessPomFromPomFileWithParent3() throws MojoExecutionException checkMojoProperties( "parentGroup", "artifact", "version", null ); } + @Test public void testProcessPomFromPomFileWithParent4() throws MojoExecutionException { mojo.setPomFile( new File( "foo.bar" ) ); @@ -137,6 +133,7 @@ public void testProcessPomFromPomFileWithParent4() throws MojoExecutionException checkMojoProperties("parentGroup", "artifact", "version", "packaging"); } + @Test public void testProcessPomFromPomFileWithParent5() throws MojoExecutionException { mojo.setPomFile( new File( "foo.bar" ) ); @@ -147,6 +144,7 @@ public void testProcessPomFromPomFileWithParent5() throws MojoExecutionException checkMojoProperties("group", "artifact", "version", "packaging"); } + @Test public void testProcessPomFromPomFileWithParent6() throws MojoExecutionException { mojo.setPomFile( new File( "foo.bar" ) ); @@ -158,6 +156,7 @@ public void testProcessPomFromPomFileWithParent6() throws MojoExecutionException } + @Test public void testProcessPomFromPomFileWithOverrides() throws MojoExecutionException { mojo.setPomFile( new File( "foo.bar" ) ); diff --git a/src/test/java/org/apache/maven/plugins/deploy/DeployMojoTest.java b/src/test/java/org/apache/maven/plugins/deploy/DeployMojoTest.java index 6404f5ad..5e3e102c 100644 --- a/src/test/java/org/apache/maven/plugins/deploy/DeployMojoTest.java +++ b/src/test/java/org/apache/maven/plugins/deploy/DeployMojoTest.java @@ -35,14 +35,13 @@ import org.apache.maven.plugin.MojoFailureException; import org.apache.maven.plugin.testing.AbstractMojoTestCase; import org.apache.maven.plugin.testing.stubs.MavenProjectStub; -import org.apache.maven.plugins.deploy.stubs.ArtifactDeployerStub; import org.apache.maven.plugins.deploy.stubs.ArtifactRepositoryStub; import org.apache.maven.plugins.deploy.stubs.DeployArtifactStub; import org.apache.maven.project.MavenProject; import org.apache.maven.project.ProjectBuildingRequest; -import org.apache.maven.shared.transfer.project.deploy.ProjectDeployerRequest; import org.codehaus.plexus.util.FileUtils; import org.eclipse.aether.DefaultRepositorySystemSession; +import org.eclipse.aether.RepositorySystem; import org.eclipse.aether.repository.LocalRepository; import org.junit.Ignore; import org.mockito.InjectMocks; @@ -136,7 +135,8 @@ public void testBasicDeploy() DefaultRepositorySystemSession repositorySession = new DefaultRepositorySystemSession(); repositorySession.setLocalRepositoryManager( new SimpleLocalRepositoryManagerFactory().newInstance( repositorySession, new LocalRepository( LOCAL_REPO ) ) ); when( buildingRequest.getRepositorySession() ).thenReturn( repositorySession ); - + when( session.getRepositorySession() ).thenReturn( repositorySession ); + File file = new File( getBasedir(), "target/test-classes/unit/basic-deploy-test/target/" + "deploy-test-file-1.0-SNAPSHOT.jar" ); @@ -307,6 +307,7 @@ public void testBasicDeployWithPackagingAsPom() DefaultRepositorySystemSession repositorySession = new DefaultRepositorySystemSession(); repositorySession.setLocalRepositoryManager( new SimpleLocalRepositoryManagerFactory().newInstance( repositorySession, new LocalRepository( LOCAL_REPO ) ) ); when( buildingRequest.getRepositorySession() ).thenReturn( repositorySession ); + when( session.getRepositorySession() ).thenReturn( repositorySession ); File pomFile = new File( getBasedir(), "target/test-classes/unit/basic-deploy-pom/target/" + @@ -419,6 +420,7 @@ public void testDeployWithAttachedArtifacts() DefaultRepositorySystemSession repositorySession = new DefaultRepositorySystemSession(); repositorySession.setLocalRepositoryManager( new SimpleLocalRepositoryManagerFactory().newInstance( repositorySession, new LocalRepository( LOCAL_REPO ) ) ); when( buildingRequest.getRepositorySession() ).thenReturn( repositorySession ); + when( session.getRepositorySession() ).thenReturn( repositorySession ); MavenProject project = (MavenProject) getVariableValueFromObject( mojo, "project" ); @@ -511,9 +513,9 @@ public void _testBasicDeployWithScpAsProtocol() assertNotNull( mojo ); - ArtifactDeployerStub deployer = new ArtifactDeployerStub(); + RepositorySystem repositorySystem = mock( RepositorySystem.class ); - setVariableValueToObject( mojo, "deployer", deployer ); + setVariableValueToObject( mojo, "repositorySystem", repositorySystem ); File file = new File( getBasedir(), "target/test-classes/unit/basic-deploy-scp/target/" + @@ -559,19 +561,17 @@ public void testLegacyAltDeploymentRepositoryWithDefaultLayout() { DeployMojo mojo = spy( new DeployMojo() ); + setVariableValueToObject( mojo, "project", project ); + setVariableValueToObject( mojo, "altDeploymentRepository", "altDeploymentRepository::default::http://localhost" ); + ArtifactRepository repository = mock( ArtifactRepository.class ); when( mojo.createDeploymentArtifactRepository( "altDeploymentRepository", "http://localhost" ) ).thenReturn( repository ); project.setVersion( "1.0-SNAPSHOT" ); - ProjectDeployerRequest pdr = - new ProjectDeployerRequest() - .setProject( project ) - .setAltDeploymentRepository( "altDeploymentRepository::default::http://localhost" ); - assertEquals( repository, - mojo.getDeploymentRepository( pdr ) ); + mojo.getDeploymentRepository() ); } @@ -580,19 +580,18 @@ public void testLegacyAltDeploymentRepositoryWithLegacyLayout() { DeployMojo mojo = spy( new DeployMojo() ); + setVariableValueToObject( mojo, "project", project ); + setVariableValueToObject( mojo, "altDeploymentRepository", "altDeploymentRepository::legacy::http://localhost" ); + ArtifactRepository repository = mock( ArtifactRepository.class ); when( mojo.createDeploymentArtifactRepository( "altDeploymentRepository", "http://localhost" ) ).thenReturn( repository ); project.setVersion( "1.0-SNAPSHOT" ); - ProjectDeployerRequest pdr = - new ProjectDeployerRequest() - .setProject( project ) - .setAltDeploymentRepository( "altDeploymentRepository::legacy::http://localhost" ); try { - mojo.getDeploymentRepository( pdr ); + mojo.getDeploymentRepository(); fail( "Should throw: Invalid legacy syntax and layout for repository." ); } catch( MojoFailureException e ) @@ -607,19 +606,17 @@ public void testInsaneAltDeploymentRepository() { DeployMojo mojo = spy( new DeployMojo() ); + setVariableValueToObject( mojo, "project", project ); + setVariableValueToObject( mojo, "altDeploymentRepository", "altDeploymentRepository::hey::wow::foo::http://localhost" ); + ArtifactRepository repository = mock( ArtifactRepository.class ); when( mojo.createDeploymentArtifactRepository( "altDeploymentRepository", "http://localhost" ) ).thenReturn( repository ); project.setVersion( "1.0-SNAPSHOT" ); - - ProjectDeployerRequest pdr = - new ProjectDeployerRequest() - .setProject( project ) - .setAltDeploymentRepository( "altDeploymentRepository::hey::wow::foo::http://localhost" ); try { - mojo.getDeploymentRepository( pdr ); + mojo.getDeploymentRepository(); fail( "Should throw: Invalid legacy syntax and layout for repository." ); } catch( MojoFailureException e ) @@ -634,38 +631,34 @@ public void testDefaultScmSvnAltDeploymentRepository() { DeployMojo mojo = spy( new DeployMojo() ); + setVariableValueToObject( mojo, "project", project ); + setVariableValueToObject( mojo, "altDeploymentRepository", "altDeploymentRepository::default::scm:svn:http://localhost" ); + ArtifactRepository repository = mock( ArtifactRepository.class ); when( mojo.createDeploymentArtifactRepository( "altDeploymentRepository", "scm:svn:http://localhost" ) ).thenReturn( repository ); project.setVersion( "1.0-SNAPSHOT" ); - ProjectDeployerRequest pdr = - new ProjectDeployerRequest() - .setProject( project ) - .setAltDeploymentRepository( "altDeploymentRepository::default::scm:svn:http://localhost" ); - assertEquals( repository, - mojo.getDeploymentRepository( pdr ) ); + mojo.getDeploymentRepository() ); } public void testLegacyScmSvnAltDeploymentRepository() throws Exception { DeployMojo mojo = spy( new DeployMojo() ); + setVariableValueToObject( mojo, "project", project ); + setVariableValueToObject( mojo, "altDeploymentRepository", "altDeploymentRepository::legacy::scm:svn:http://localhost" ); + ArtifactRepository repository = mock( ArtifactRepository.class ); when( mojo.createDeploymentArtifactRepository( "altDeploymentRepository", "http://localhost" ) ).thenReturn( repository ); project.setVersion( "1.0-SNAPSHOT" ); - - ProjectDeployerRequest pdr = - new ProjectDeployerRequest() - .setProject( project ) - .setAltDeploymentRepository( "altDeploymentRepository::legacy::scm:svn:http://localhost" ); try { - mojo.getDeploymentRepository( pdr ); + mojo.getDeploymentRepository(); fail( "Should throw: Invalid legacy syntax and layout for repository." ); } catch( MojoFailureException e ) @@ -680,18 +673,17 @@ public void testAltSnapshotDeploymentRepository() { DeployMojo mojo = spy( new DeployMojo() ); + setVariableValueToObject( mojo, "project", project ); + setVariableValueToObject( mojo, "altSnapshotDeploymentRepository", "altSnapshotDeploymentRepository::http://localhost" ); + ArtifactRepository repository = mock( ArtifactRepository.class ); when( mojo.createDeploymentArtifactRepository( "altSnapshotDeploymentRepository", "http://localhost" ) ).thenReturn( repository ); project.setVersion( "1.0-SNAPSHOT" ); - ProjectDeployerRequest pdr = - new ProjectDeployerRequest() - .setProject( project ) - .setAltDeploymentRepository( "altSnapshotDeploymentRepository::http://localhost" ); assertEquals( repository, - mojo.getDeploymentRepository( pdr )); + mojo.getDeploymentRepository()); } public void testAltReleaseDeploymentRepository() @@ -699,18 +691,16 @@ public void testAltReleaseDeploymentRepository() { DeployMojo mojo = spy( new DeployMojo() ); + setVariableValueToObject( mojo, "project", project ); + setVariableValueToObject( mojo, "altReleaseDeploymentRepository", "altReleaseDeploymentRepository::http://localhost" ); + ArtifactRepository repository = mock( ArtifactRepository.class ); when( mojo.createDeploymentArtifactRepository( "altReleaseDeploymentRepository", "http://localhost" ) ).thenReturn( repository ); project.setVersion( "1.0" ); - ProjectDeployerRequest pdr = - new ProjectDeployerRequest() - .setProject( project ) - .setAltReleaseDeploymentRepository( "altReleaseDeploymentRepository::http://localhost" ); - assertEquals( repository, - mojo.getDeploymentRepository( pdr )); + mojo.getDeploymentRepository()); } private void addFileToList( File file, List fileList ) diff --git a/src/test/java/org/apache/maven/plugins/deploy/stubs/ArtifactDeployerStub.java b/src/test/java/org/apache/maven/plugins/deploy/stubs/ArtifactDeployerStub.java deleted file mode 100644 index d0b47d43..00000000 --- a/src/test/java/org/apache/maven/plugins/deploy/stubs/ArtifactDeployerStub.java +++ /dev/null @@ -1,47 +0,0 @@ -package org.apache.maven.plugins.deploy.stubs; - -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -import java.util.Collection; - -import org.apache.maven.artifact.Artifact; -import org.apache.maven.artifact.repository.ArtifactRepository; -import org.apache.maven.project.ProjectBuildingRequest; -import org.apache.maven.shared.transfer.artifact.deploy.ArtifactDeployer; -import org.apache.maven.shared.transfer.artifact.deploy.ArtifactDeployerException; - -public class ArtifactDeployerStub - implements ArtifactDeployer -{ - - @Override - public void deploy( ProjectBuildingRequest request, Collection mavenArtifacts ) - throws ArtifactDeployerException - { - // does nothing - } - - @Override - public void deploy( ProjectBuildingRequest arg0, ArtifactRepository arg1, Collection arg2) - throws ArtifactDeployerException - { - // does nothing - } -} From 332ad374292dfa789683dd809e4de512b9ec14e4 Mon Sep 17 00:00:00 2001 From: Tamas Cservenak Date: Mon, 20 Jun 2022 16:29:22 +0200 Subject: [PATCH 2/2] Return retries --- .../plugins/deploy/AbstractDeployMojo.java | 41 ++++++++++++++----- 1 file changed, 30 insertions(+), 11 deletions(-) diff --git a/src/main/java/org/apache/maven/plugins/deploy/AbstractDeployMojo.java b/src/main/java/org/apache/maven/plugins/deploy/AbstractDeployMojo.java index 0466495b..6fe7f601 100644 --- a/src/main/java/org/apache/maven/plugins/deploy/AbstractDeployMojo.java +++ b/src/main/java/org/apache/maven/plugins/deploy/AbstractDeployMojo.java @@ -90,11 +90,6 @@ void failIfOffline() } } - int getRetryFailedDeploymentCount() - { - return retryFailedDeploymentCount; - } - protected ArtifactRepository createDeploymentArtifactRepository( String id, String url ) { return new MavenArtifactRepository( id, url, new DefaultRepositoryLayout(), new ArtifactRepositoryPolicy(), @@ -173,16 +168,40 @@ protected DeployRequest deployRequest( ArtifactRepository repository, List 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() ); + } + if ( exception == null ) + { + exception = e; + } + } } - catch ( DeploymentException e ) + if ( exception != null ) { - throw new MojoExecutionException( e.getMessage(), e ); + throw new MojoExecutionException( exception.getMessage(), exception ); } } }