You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by cs...@apache.org on 2022/07/12 11:16:51 UTC

[maven-deploy-plugin] branch full-resolver created (now 241c28b)

This is an automated email from the ASF dual-hosted git repository.

cstamas pushed a change to branch full-resolver
in repository https://gitbox.apache.org/repos/asf/maven-deploy-plugin.git


      at 241c28b  Cleanup

This branch includes the following new commits:

     new bd80505  Apply comments
     new 241c28b  Cleanup

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



[maven-deploy-plugin] 02/02: Cleanup

Posted by cs...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

cstamas pushed a commit to branch full-resolver
in repository https://gitbox.apache.org/repos/asf/maven-deploy-plugin.git

commit 241c28b0c85ae4e097a233992cc5de885a6cf54f
Author: Tamas Cservenak <ta...@cservenak.net>
AuthorDate: Tue Jul 12 13:16:38 2022 +0200

    Cleanup
---
 .../maven/plugins/deploy/AbstractDeployMojo.java   |  51 +---
 .../maven/plugins/deploy/DeployFileMojo.java       | 272 ++++++++++++---------
 .../apache/maven/plugins/deploy/DeployMojo.java    | 108 ++++----
 .../maven/plugins/deploy/DeployMojoTest.java       |  79 +++---
 4 files changed, 261 insertions(+), 249 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 dda506b..42dda04 100644
--- a/src/main/java/org/apache/maven/plugins/deploy/AbstractDeployMojo.java
+++ b/src/main/java/org/apache/maven/plugins/deploy/AbstractDeployMojo.java
@@ -19,28 +19,18 @@ package org.apache.maven.plugins.deploy;
  * 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.RepositorySystemSession;
 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;
@@ -90,12 +80,6 @@ public abstract class AbstractDeployMojo
         }
     }
 
-    protected ArtifactRepository createDeploymentArtifactRepository( String id, String url )
-    {
-        return new MavenArtifactRepository( id, url, new DefaultRepositoryLayout(), new ArtifactRepositoryPolicy(),
-                new ArtifactRepositoryPolicy() );
-    }
-
     protected void warnIfAffectedPackagingAndMaven( final String packaging )
     {
         if ( AFFECTED_MAVEN_PACKAGING.equals( packaging ) )
@@ -121,9 +105,9 @@ public abstract class AbstractDeployMojo
         }
     }
 
-    private RemoteRepository getRemoteRepository( ArtifactRepository remoteRepository )
+    protected RemoteRepository getRemoteRepository( final String repositoryId, final String url )
     {
-        RemoteRepository result = RepositoryUtils.toRepo( remoteRepository );
+        RemoteRepository result = new RemoteRepository.Builder( repositoryId, "default", url ).build();
 
         if ( result.getAuthentication() == null || result.getProxy() == null )
         {
@@ -146,29 +130,10 @@ public abstract class AbstractDeployMojo
         return result;
     }
 
-    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
+    /**
+     * 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;
@@ -182,7 +147,7 @@ public abstract class AbstractDeployMojo
                             + retryFailedDeploymentCounter );
                 }
 
-                repositorySystem.deploy( session.getRepositorySession(), deployRequest );
+                repositorySystem.deploy( session, deployRequest );
                 exception = null;
                 break;
             }
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 eb2e42d..a51b4a7 100644
--- a/src/main/java/org/apache/maven/plugins/deploy/DeployFileMojo.java
+++ b/src/main/java/org/apache/maven/plugins/deploy/DeployFileMojo.java
@@ -27,40 +27,34 @@ import java.io.InputStream;
 import java.io.OutputStream;
 import java.io.Reader;
 import java.io.Writer;
-import java.util.ArrayList;
 import java.util.Enumeration;
-import java.util.List;
+import java.util.Objects;
 import java.util.jar.JarEntry;
 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;
 import org.apache.maven.model.Parent;
-import org.apache.maven.model.building.ModelBuildingException;
-import org.apache.maven.model.building.ModelSource;
-import org.apache.maven.model.building.StringModelSource;
 import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
 import org.apache.maven.model.io.xpp3.MavenXpp3Writer;
 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.Mojo;
 import org.apache.maven.plugins.annotations.Parameter;
-import org.apache.maven.project.DefaultProjectBuildingRequest;
-import org.apache.maven.project.MavenProject;
-import org.apache.maven.project.MavenProjectHelper;
-import org.apache.maven.project.ProjectBuilder;
-import org.apache.maven.project.ProjectBuildingException;
-import org.apache.maven.project.artifact.ProjectArtifactMetadata;
 import org.codehaus.plexus.util.FileUtils;
 import org.codehaus.plexus.util.IOUtil;
 import org.codehaus.plexus.util.ReaderFactory;
 import org.codehaus.plexus.util.StringUtils;
 import org.codehaus.plexus.util.WriterFactory;
 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
+import org.eclipse.aether.RepositorySystemSession;
+import org.eclipse.aether.artifact.Artifact;
+import org.eclipse.aether.artifact.ArtifactType;
+import org.eclipse.aether.artifact.DefaultArtifact;
+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;
 
 /**
  * Installs the artifact in the remote repository.
@@ -71,19 +65,7 @@ import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
 public class DeployFileMojo
     extends AbstractDeployMojo
 {
-    private static final String LINE_SEP = System.getProperty( "line.separator" );
-
-    /**
-     * Used for attaching the artifacts to deploy to the project.
-     */
-    @Component
-    private MavenProjectHelper projectHelper;
-
-    /**
-     * Used for creating the project to which the artifacts to deploy will be attached.
-     */
-    @Component
-    private ProjectBuilder projectBuilder;
+    private static final String LS = System.getProperty( "line.separator" );
 
     /**
      * GroupId of the artifact to be deployed. Retrieved from POM file if specified.
@@ -285,7 +267,7 @@ public class DeployFileMojo
 
         if ( packaging == null && file != null )
         {
-            packaging = FileUtils.getExtension( file.getName() );
+            packaging = getExtension( file );
         }
     }
 
@@ -301,68 +283,84 @@ public class DeployFileMojo
 
         initProperties();
 
-        ArtifactRepository deploymentRepository = createDeploymentArtifactRepository( repositoryId, url );
-
-        String protocol = deploymentRepository.getProtocol();
+        RemoteRepository remoteRepository = getRemoteRepository( repositoryId, url );
 
-        if ( StringUtils.isEmpty( protocol ) )
+        if ( StringUtils.isEmpty( remoteRepository.getProtocol() ) )
         {
             throw new MojoExecutionException( "No transfer protocol found." );
         }
 
-        MavenProject project = createMavenProject();
-        Artifact artifact = project.getArtifact();
+        if ( groupId == null || artifactId == null || version == null || packaging == null )
+        {
+            throw new MojoExecutionException( "The artifact information is incomplete: 'groupId', 'artifactId', "
+                    + "'version' and 'packaging' are required." );
+        }
 
-        if ( file.equals( getLocalRepoFile( artifact ) ) )
+        if ( !isValidId( groupId )
+                || !isValidId( artifactId )
+                || !isValidVersion( version ) )
         {
-            throw new MojoFailureException( "Cannot deploy artifact from the local repository: " + file );
+            throw new MojoExecutionException( "The artifact information is not valid: uses invalid characters." );
         }
 
-        List<Artifact> deployableArtifacts = new ArrayList<Artifact>();
+        DeployRequest deployRequest = new DeployRequest();
+        deployRequest.setRepository( remoteRepository );
 
-        if ( classifier == null )
+        boolean isFilePom = classifier == null && "pom".equals( packaging );
+        if ( !isFilePom )
         {
-            artifact.setFile( file );
-            deployableArtifacts.add( artifact );
+            ArtifactType artifactType = session.getRepositorySession().getArtifactTypeRegistry().get( packaging );
+            if ( artifactType != null
+                    && StringUtils.isEmpty( classifier )
+                    && !StringUtils.isEmpty( artifactType.getClassifier() ) )
+            {
+                classifier = artifactType.getClassifier();
+            }
         }
-        else
+        Artifact mainArtifact = new DefaultArtifact(
+                groupId,
+                artifactId,
+                classifier,
+                isFilePom ? "pom" : getExtension( file ),
+                version
+        ).setFile( file );
+        deployRequest.addArtifact( mainArtifact );
+
+        File artifactLocalFile = getLocalRepositoryFile( session.getRepositorySession(), mainArtifact );
+        File pomLocalFile = getPomLocalRepositoryFile( session.getRepositorySession(), mainArtifact );
+
+        if ( file.equals( artifactLocalFile ) )
         {
-            projectHelper.attachArtifact( project, packaging, classifier, file );
+            throw new MojoFailureException( "Cannot deploy artifact from the local repository: " + file );
         }
 
-        // Upload the POM if requested, generating one if need be
+        File temporaryPom = null;
         if ( !"pom".equals( packaging ) )
         {
-            File pom = pomFile;
-            if ( pom == null && generatePom )
+            if ( pomFile != null )
             {
-                pom = generatePomFile();
+                deployRequest.addArtifact( new SubArtifact( mainArtifact, "", "pom", pomFile ) );
             }
-            if ( pom != null )
+            else if ( generatePom )
             {
-                if ( classifier == null )
-                {
-                    ProjectArtifactMetadata metadata = new ProjectArtifactMetadata( artifact, pom );
-                    artifact.addMetadata( metadata );
-                }
-                else
-                {
-                    artifact.setFile( pom );
-                    deployableArtifacts.add( artifact );
-                }
+                temporaryPom = generatePomFile();
+                getLog().debug( "Deploying generated POM" );
+                deployRequest.addArtifact( new SubArtifact( mainArtifact, "", "pom", temporaryPom ) );
+            }
+            else
+            {
+                getLog().debug( "Skipping deploying POM" );
             }
         }
 
-        artifact.setRepository( deploymentRepository );
-
         if ( sources != null )
         {
-            projectHelper.attachArtifact( project, "jar", "sources", sources );
+            deployRequest.addArtifact( new SubArtifact( mainArtifact, "sources", "jar", sources ) );
         }
 
         if ( javadoc != null )
         {
-            projectHelper.attachArtifact( project, "jar", "javadoc", javadoc );
+            deployRequest.addArtifact( new SubArtifact( mainArtifact, "javadoc", "jar", javadoc ) );
         }
 
         if ( files != null )
@@ -381,12 +379,12 @@ public class DeployFileMojo
             if ( typesLength != filesLength )
             {
                 throw new MojoExecutionException( "You must specify the same number of entries in 'files' and "
-                    + "'types' (respectively " + filesLength + " and " + typesLength + " entries )" );
+                        + "'types' (respectively " + filesLength + " and " + typesLength + " entries )" );
             }
             if ( classifiersLength != filesLength )
             {
                 throw new MojoExecutionException( "You must specify the same number of entries in 'files' and "
-                    + "'classifiers' (respectively " + filesLength + " and " + classifiersLength + " entries )" );
+                        + "'classifiers' (respectively " + filesLength + " and " + classifiersLength + " entries )" );
             }
             int fi = 0;
             int ti = 0;
@@ -412,19 +410,21 @@ public class DeployFileMojo
                 if ( !file.isFile() )
                 {
                     // try relative to the project basedir just in case
-                    file = new File( project.getBasedir(), files.substring( fi, nfi ) );
+                    file = new File( files.substring( fi, nfi ) );
                 }
                 if ( file.isFile() )
                 {
-                    if ( StringUtils.isWhitespace( classifiers.substring( ci, nci ) ) )
-                    {
-                        projectHelper.attachArtifact( project, types.substring( ti, nti ).trim(), file );
-                    }
-                    else
+                    String extension = getExtension( file );
+                    ArtifactType artifactType = session.getRepositorySession().getArtifactTypeRegistry()
+                            .get( types.substring( ti, nti ).trim() );
+                    if ( artifactType != null && !Objects.equals( extension, artifactType.getExtension() ) )
                     {
-                        projectHelper.attachArtifact( project, types.substring( ti, nti ).trim(),
-                                                      classifiers.substring( ci, nci ).trim(), file );
+                        extension = artifactType.getExtension();
                     }
+
+                    deployRequest.addArtifact(
+                            new SubArtifact( mainArtifact, classifiers.substring( ci, nci ).trim(), extension, file )
+                    );
                 }
                 else
                 {
@@ -447,62 +447,43 @@ public class DeployFileMojo
             }
         }
 
-        deployableArtifacts.addAll( project.getAttachedArtifacts() );
-
-        warnIfAffectedPackagingAndMaven( packaging );
-        deploy( deployRequest( deploymentRepository, deployableArtifacts ) );
-    }
-
-    /**
-     * Creates a Maven project in-memory from the user-supplied groupId, artifactId and version. When a classifier is
-     * supplied, the packaging must be POM because the project with only have attachments. This project serves as basis
-     * to attach the artifacts to deploy to.
-     * 
-     * @return The created Maven project, never <code>null</code>.
-     * @throws MojoExecutionException When the model of the project could not be built.
-     * @throws MojoFailureException When building the project failed.
-     */
-    private MavenProject createMavenProject()
-        throws MojoExecutionException, MojoFailureException
-    {
-        if ( groupId == null || artifactId == null || version == null || packaging == null )
-        {
-            throw new MojoExecutionException( "The artifact information is incomplete: 'groupId', 'artifactId', "
-                + "'version' and 'packaging' are required." );
-        }
-        ModelSource modelSource =
-            new StringModelSource( "<project>" + "<modelVersion>4.0.0</modelVersion>" + "<groupId>" + groupId
-                + "</groupId>" + "<artifactId>" + artifactId + "</artifactId>" + "<version>" + version + "</version>"
-                + "<packaging>" + ( classifier == null ? packaging : "pom" ) + "</packaging>" + "</project>" );
-        DefaultProjectBuildingRequest buildingRequest =
-            new DefaultProjectBuildingRequest( session.getProjectBuildingRequest() );
-        buildingRequest.setProcessPlugins( false );
         try
         {
-            return projectBuilder.build( modelSource, buildingRequest ).getProject();
+            repositorySystem.deploy( session.getRepositorySession(), deployRequest );
+        }
+        catch ( DeploymentException e )
+        {
+            throw new MojoExecutionException( e.getMessage(), e );
         }
-        catch ( ProjectBuildingException e )
+        finally
         {
-            if ( e.getCause() instanceof ModelBuildingException )
+            if ( temporaryPom != null )
             {
-                throw new MojoExecutionException( "The artifact information is not valid:" + LINE_SEP
-                    + e.getCause().getMessage() );
+                // noinspection ResultOfMethodCallIgnored
+                temporaryPom.delete();
             }
-            throw new MojoFailureException( "Unable to create the project.", e );
         }
     }
 
     /**
-     * Gets the path of the artifact constructed from the supplied groupId, artifactId, version, classifier and
-     * packaging within the local repository. Note that the returned path need not exist (yet).
-     * 
-     * @return The absolute path to the artifact when installed, never <code>null</code>.
+     * Gets the path of the specified artifact within the local repository. Note that the returned path need not exist
+     * (yet).
+     */
+    private File getLocalRepositoryFile( RepositorySystemSession session, Artifact artifact )
+    {
+        String path = session.getLocalRepositoryManager().getPathForLocalArtifact( artifact );
+        return new File( session.getLocalRepository().getBasedir(), path );
+    }
+
+    /**
+     * Gets the path of the specified artifact POM within the local repository. Note that the returned path need
+     * not exist (yet).
      */
-    private File getLocalRepoFile( Artifact artifact )
+    private File getPomLocalRepositoryFile( RepositorySystemSession session, Artifact artifact )
     {
-        String path = session.getRepositorySession().getLocalRepositoryManager().getPathForLocalArtifact(
-                RepositoryUtils.toArtifact( artifact ) );
-        return new File( session.getRepositorySession().getLocalRepository().getBasedir(), path );
+        SubArtifact pomArtifact = new SubArtifact( artifact, "", "pom" );
+        String path = session.getLocalRepositoryManager().getPathForLocalArtifact( pomArtifact );
+        return new File( session.getLocalRepository().getBasedir(), path );
     }
 
     /**
@@ -694,4 +675,63 @@ public class DeployFileMojo
         this.classifier = classifier;
     }
 
+    // these below should be shared (duplicated in m-install-p, m-deploy-p)
+
+    /**
+     * Specialization of {@link FileUtils#getExtension(String)} that honors various {@code tar.xxx} combinations.
+     */
+    private String getExtension( final File file )
+    {
+        String filename = file.getName();
+        if ( filename.contains( ".tar." ) )
+        {
+            return "tar." + FileUtils.getExtension( filename );
+        }
+        else
+        {
+            return FileUtils.getExtension( filename );
+        }
+    }
+
+    /**
+     * Returns {@code true} if passed in string is "valid Maven ID" (groupId or artifactId).
+     */
+    private boolean isValidId( String id )
+    {
+        if ( id == null )
+        {
+            return false;
+        }
+        for ( int i = 0; i < id.length(); i++ )
+        {
+            char c = id.charAt( i );
+            if ( !( c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z'
+                    || c >= '0' && c <= '9' || c == '-' || c == '_' || c == '.' ) )
+            {
+                return false;
+            }
+        }
+        return true;
+    }
+
+    private static final String ILLEGAL_VERSION_CHARS = "\\/:\"<>|?*[](){},";
+
+    /**
+     * Returns {@code true} if passed in string is "valid Maven (simple. non range, expression, etc) version".
+     */
+    private boolean isValidVersion( String version )
+    {
+        if ( version == null )
+        {
+            return false;
+        }
+        for ( int i = version.length() - 1; i >= 0; i-- )
+        {
+            if ( ILLEGAL_VERSION_CHARS.indexOf( version.charAt( i ) ) >= 0 )
+            {
+                return false;
+            }
+        }
+        return true;
+    }
 }
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 98223ee..9ae0e8c 100644
--- a/src/main/java/org/apache/maven/plugins/deploy/DeployMojo.java
+++ b/src/main/java/org/apache/maven/plugins/deploy/DeployMojo.java
@@ -20,15 +20,13 @@ package org.apache.maven.plugins.deploy;
  */
 
 import java.io.File;
-import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
-import org.apache.maven.artifact.Artifact;
+import org.apache.maven.RepositoryUtils;
 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.plugin.descriptor.PluginDescriptor;
@@ -36,8 +34,11 @@ 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.artifact.ProjectArtifact;
 import org.apache.maven.project.artifact.ProjectArtifactMetadata;
 import org.eclipse.aether.deployment.DeployRequest;
+import org.eclipse.aether.repository.RemoteRepository;
+import org.eclipse.aether.util.artifact.SubArtifact;
 
 /**
  * Deploys an artifact to remote repository.
@@ -189,7 +190,8 @@ public class DeployMojo
 
             if ( !deployAtEnd )
             {
-                deploy( processProject( project,
+                deploy( session.getRepositorySession(),
+                        processProject( project,
                         altSnapshotDeploymentRepository, altReleaseDeploymentRepository, altDeploymentRepository ) );
                 putState( State.DEPLOYED );
             }
@@ -199,7 +201,8 @@ public class DeployMojo
                 putPluginContextValue( DEPLOY_ALT_SNAPSHOT_DEPLOYMENT_REPOSITORY, altSnapshotDeploymentRepository );
                 putPluginContextValue( DEPLOY_ALT_DEPLOYMENT_REPOSITORY, altDeploymentRepository );
                 putState( State.TO_BE_DEPLOYED );
-                getLog().info( "Deferring deploy for " + getProjectReferenceId( project ) + " at end" );
+                getLog().info( "Deferring deploy for " + project.getGroupId()
+                        + ":" + project.getArtifactId() + ":" + project.getVersion() + " at end" );
             }
         }
 
@@ -218,7 +221,8 @@ public class DeployMojo
                     String altDeploymentRepository =
                         getPluginContextValue( pluginContext, DEPLOY_ALT_DEPLOYMENT_REPOSITORY );
 
-                    deploy( processProject( reactorProject,
+                    deploy( session.getRepositorySession(),
+                            processProject( reactorProject,
                             altSnapshotDeploymentRepository, altReleaseDeploymentRepository, altDeploymentRepository )
                     );
                 }
@@ -226,11 +230,6 @@ public class DeployMojo
         }
     }
 
-    private String getProjectReferenceId( MavenProject mavenProject )
-    {
-        return mavenProject.getGroupId() + ":" + mavenProject.getArtifactId() + ":" + mavenProject.getVersion();
-    }
-
     private boolean allProjectsMarked()
     {
         for ( MavenProject reactorProject : reactorProjects )
@@ -249,37 +248,47 @@ public class DeployMojo
                                           final String altDeploymentRepository )
             throws MojoExecutionException, MojoFailureException
     {
-        ArrayList<Artifact> deployableArtifacts = new ArrayList<>();
-        Artifact artifact = project.getArtifact();
+        DeployRequest request = new DeployRequest();
+        request.setRepository( getDeploymentRepository( project,
+                altSnapshotDeploymentRepository, altReleaseDeploymentRepository, altDeploymentRepository ) );
+
+        org.apache.maven.artifact.Artifact mavenMainArtifact = project.getArtifact();
         String packaging = project.getPackaging();
         File pomFile = project.getFile();
-        List<Artifact> 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 );
-        }
+        boolean pomArtifactAttached = false;
 
-        if ( isPomArtifact )
+        if ( pomFile != null )
         {
-            deployableArtifacts.add( artifact );
+            request.addArtifact( RepositoryUtils.toArtifact( new ProjectArtifact( project ) ) );
+            pomArtifactAttached = true;
         }
-        else
-        {
-            File file = artifact.getFile();
 
+        if ( !isPomArtifact )
+        {
+            File file = mavenMainArtifact.getFile();
             if ( file != null && file.isFile() )
             {
-                deployableArtifacts.add( artifact );
+                org.eclipse.aether.artifact.Artifact mainArtifact = RepositoryUtils.toArtifact( mavenMainArtifact );
+                request.addArtifact( mainArtifact );
+
+                if ( !pomArtifactAttached )
+                {
+                    for ( Object metadata : mavenMainArtifact.getMetadataList() )
+                    {
+                        if ( metadata instanceof ProjectArtifactMetadata )
+                        {
+                            request.addArtifact( new SubArtifact(
+                                    mainArtifact,
+                                    "",
+                                    "pom"
+                            ).setFile( ( (ProjectArtifactMetadata) metadata ).getFile() ) );
+                            pomArtifactAttached = true;
+                        }
+                    }
+                }
             }
-            else if ( !attachedArtifacts.isEmpty() )
+            else if ( !project.getAttachedArtifacts().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'." );
@@ -290,25 +299,32 @@ public class DeployMojo
                         + "a file to the build artifact" );
             }
         }
-        deployableArtifacts.addAll( attachedArtifacts );
-        return deployRequest(
-                getDeploymentRepository( project,
-                        altSnapshotDeploymentRepository, altReleaseDeploymentRepository, altDeploymentRepository ),
-                deployableArtifacts
-        );
+
+        if ( !pomArtifactAttached )
+        {
+            throw new MojoExecutionException( "The POM could not be attached" );
+        }
+
+        for ( org.apache.maven.artifact.Artifact attached : project.getAttachedArtifacts() )
+        {
+            getLog().debug( "Attaching for install: " + attached.getId() );
+            request.addArtifact( RepositoryUtils.toArtifact( attached ) );
+        }
+
+        return request;
     }
 
     /**
      * Visible for testing.
      */
-    ArtifactRepository getDeploymentRepository( final MavenProject project,
-                                                        final String altSnapshotDeploymentRepository,
-                                                        final String altReleaseDeploymentRepository,
-                                                        final String altDeploymentRepository )
+    RemoteRepository getDeploymentRepository( final MavenProject project,
+                                              final String altSnapshotDeploymentRepository,
+                                              final String altReleaseDeploymentRepository,
+                                              final String altDeploymentRepository )
 
         throws MojoExecutionException, MojoFailureException
     {
-        ArtifactRepository repo = null;
+        RemoteRepository repo = null;
 
         String altDeploymentRepo;
         if ( ArtifactUtils.isSnapshot( project.getVersion() ) && altSnapshotDeploymentRepository != null )
@@ -340,7 +356,7 @@ public class DeployMojo
                 {
                     getLog().warn( "Using legacy syntax for alternative repository. "
                             + "Use \"" + id + "::" + url + "\" instead." );
-                    repo = createDeploymentArtifactRepository( id, url );
+                    repo = getRemoteRepository( id, url );
                 }
                 else
                 {
@@ -367,14 +383,14 @@ public class DeployMojo
                     String id = matcher.group( 1 ).trim();
                     String url = matcher.group( 2 ).trim();
 
-                    repo = createDeploymentArtifactRepository( id, url );
+                    repo = getRemoteRepository( id, url );
                 }
             }
         }
 
         if ( repo == null )
         {
-            repo = project.getDistributionManagementArtifactRepository();
+            repo = RepositoryUtils.toRepo( project.getDistributionManagementArtifactRepository() );
         }
 
         if ( repo == null )
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 49f645b..f34abd0 100644
--- a/src/test/java/org/apache/maven/plugins/deploy/DeployMojoTest.java
+++ b/src/test/java/org/apache/maven/plugins/deploy/DeployMojoTest.java
@@ -20,6 +20,7 @@ package org.apache.maven.plugins.deploy;
  */
 
 import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.doReturn;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.spy;
 import static org.mockito.Mockito.when;
@@ -31,7 +32,6 @@ import java.util.List;
 import java.util.Properties;
 import java.util.concurrent.ConcurrentHashMap;
 
-import org.apache.maven.artifact.repository.ArtifactRepository;
 import org.apache.maven.execution.MavenSession;
 import org.apache.maven.plugin.MojoExecutionException;
 import org.apache.maven.plugin.MojoFailureException;
@@ -46,9 +46,9 @@ import org.codehaus.plexus.util.FileUtils;
 import org.eclipse.aether.DefaultRepositorySystemSession;
 import org.eclipse.aether.RepositorySystem;
 import org.eclipse.aether.repository.LocalRepository;
+import org.eclipse.aether.repository.RemoteRepository;
 import org.junit.Ignore;
 import org.mockito.InjectMocks;
-import org.mockito.Mock;
 import org.mockito.MockitoAnnotations;
 import org.eclipse.aether.internal.impl.SimpleLocalRepositoryManagerFactory;
 
@@ -83,6 +83,9 @@ public class DeployMojoTest
         session = mock( MavenSession.class );
         when( session.getPluginContext(any(PluginDescriptor.class), any(MavenProject.class)))
                 .thenReturn( new ConcurrentHashMap<String, Object>() );
+        DefaultRepositorySystemSession repositorySession = new DefaultRepositorySystemSession();
+        repositorySession.setLocalRepositoryManager( new SimpleLocalRepositoryManagerFactory().newInstance( repositorySession, new LocalRepository( LOCAL_REPO ) ) );
+        when( session.getRepositorySession() ).thenReturn( repositorySession );
 
         remoteRepo = new File( REMOTE_REPO );
         
@@ -99,8 +102,6 @@ public class DeployMojoTest
         {
             FileUtils.deleteDirectory( remoteRepo );
         }
-        
-        
     }
 
     public void tearDown()
@@ -135,7 +136,7 @@ public class DeployMojoTest
         MockitoAnnotations.initMocks( this );
         
         assertNotNull( mojo );
-        
+
         ProjectBuildingRequest buildingRequest = mock ( ProjectBuildingRequest.class );
         when( session.getProjectBuildingRequest() ).thenReturn( buildingRequest );
         DefaultRepositorySystemSession repositorySession = new DefaultRepositorySystemSession();
@@ -150,6 +151,9 @@ public class DeployMojoTest
         assertTrue( file.exists() );
 
         MavenProject project = (MavenProject) getVariableValueFromObject( mojo, "project" );
+        project.setGroupId( "org.apache.maven.test" );
+        project.setArtifactId( "maven-deploy-test" );
+        project.setVersion( "1.0-SNAPSHOT" );
 
         setVariableValueToObject( mojo, "pluginContext", new ConcurrentHashMap<>() );
         setVariableValueToObject( mojo, "reactorProjects", Collections.singletonList( project ) );
@@ -326,6 +330,9 @@ public class DeployMojoTest
         assertTrue( pomFile.exists() );
         
         MavenProject project = (MavenProject) getVariableValueFromObject( mojo, "project" );
+        project.setGroupId( "org.apache.maven.test" );
+        project.setArtifactId( "maven-deploy-test" );
+        project.setVersion( "1.0-SNAPSHOT" );
 
         setVariableValueToObject( mojo, "pluginContext", new ConcurrentHashMap<>() );
         setVariableValueToObject( mojo, "reactorProjects", Collections.singletonList( project ) );
@@ -392,6 +399,9 @@ public class DeployMojoTest
         assertNotNull( mojo );
         
         MavenProject project = (MavenProject) getVariableValueFromObject( mojo, "project" );
+        project.setGroupId( "org.apache.maven.test" );
+        project.setArtifactId( "maven-deploy-test" );
+        project.setVersion( "1.0-SNAPSHOT" );
 
         setVariableValueToObject( mojo, "pluginContext", new ConcurrentHashMap<>() );
         setVariableValueToObject( mojo, "reactorProjects", Collections.singletonList( project ) );
@@ -435,6 +445,9 @@ public class DeployMojoTest
         when( session.getRepositorySession() ).thenReturn( repositorySession );
 
         MavenProject project = (MavenProject) getVariableValueFromObject( mojo, "project" );
+        project.setGroupId( "org.apache.maven.test" );
+        project.setArtifactId( "maven-deploy-test" );
+        project.setVersion( "1.0-SNAPSHOT" );
 
         setVariableValueToObject( mojo, "pluginContext", new ConcurrentHashMap<>() );
         setVariableValueToObject( mojo, "reactorProjects", Collections.singletonList( project ) );
@@ -573,18 +586,15 @@ public class DeployMojoTest
     public void testLegacyAltDeploymentRepositoryWithDefaultLayout()
         throws Exception
     {
-        DeployMojo mojo = spy( new DeployMojo() );
+        DeployMojo mojo = new DeployMojo();
 
         setVariableValueToObject( mojo, "project", project );
+        setVariableValueToObject( mojo, "session", session );
         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" );
 
-        assertEquals( repository,
+        assertEquals( new RemoteRepository.Builder( "altDeploymentRepository", "default", "http://localhost" ).build(),
                 mojo.getDeploymentRepository( project, null, null, "altDeploymentRepository::default::http://localhost") );
 
     }
@@ -592,17 +602,13 @@ public class DeployMojoTest
     public void testLegacyAltDeploymentRepositoryWithLegacyLayout()
         throws Exception
     {
-        DeployMojo mojo = spy( new DeployMojo() );
+        DeployMojo mojo = new DeployMojo();
 
         setVariableValueToObject( mojo, "project", project );
+        setVariableValueToObject( mojo, "session", session );
         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" );
-
         try
         {
             mojo.getDeploymentRepository( project, null, null, "altDeploymentRepository::legacy::http://localhost" );
@@ -618,15 +624,12 @@ public class DeployMojoTest
     public void testInsaneAltDeploymentRepository()
             throws Exception
     {
-        DeployMojo mojo = spy( new DeployMojo() );
+        DeployMojo mojo = new DeployMojo();
 
         setVariableValueToObject( mojo, "project", project );
+        setVariableValueToObject( mojo, "session", session );
         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" );
         try
         {
@@ -643,32 +646,25 @@ public class DeployMojoTest
     public void testDefaultScmSvnAltDeploymentRepository()
             throws Exception
     {
-        DeployMojo mojo = spy( new DeployMojo() );
+        DeployMojo mojo = new DeployMojo();
 
         setVariableValueToObject( mojo, "project", project );
+        setVariableValueToObject( mojo, "session", session );
         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" );
 
-        assertEquals( repository,
+        assertEquals( new RemoteRepository.Builder( "altDeploymentRepository", "default", "scm:svn:http://localhost" ).build(),
                 mojo.getDeploymentRepository( project, null, null, "altDeploymentRepository::default::scm:svn:http://localhost" ) );
     }
     public void testLegacyScmSvnAltDeploymentRepository()
             throws Exception
     {
-        DeployMojo mojo = spy( new DeployMojo() );
+        DeployMojo mojo = 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" );
         try
         {
@@ -685,35 +681,30 @@ public class DeployMojoTest
     public void testAltSnapshotDeploymentRepository()
         throws Exception
     {
-        DeployMojo mojo = spy( new DeployMojo() );
+        DeployMojo mojo = new DeployMojo();
 
         setVariableValueToObject( mojo, "project", project );
+        setVariableValueToObject( mojo, "session", session );
         setVariableValueToObject( mojo, "altSnapshotDeploymentRepository",  "altSnapshotDeploymentRepository::http://localhost" );
 
-        ArtifactRepository repository = mock( ArtifactRepository.class );
-        when( mojo.createDeploymentArtifactRepository( "altSnapshotDeploymentRepository", "http://localhost"
-                                                       ) ).thenReturn( repository );
-
         project.setVersion( "1.0-SNAPSHOT" );
 
-        assertEquals( repository,
+        assertEquals( new RemoteRepository.Builder( "altSnapshotDeploymentRepository", "default", "http://localhost" ).build(),
                       mojo.getDeploymentRepository( project, "altSnapshotDeploymentRepository::http://localhost", null, null ));
     }
 
     public void testAltReleaseDeploymentRepository()
         throws Exception
     {
-        DeployMojo mojo = spy( new DeployMojo() );
+        DeployMojo mojo = new DeployMojo();
 
         setVariableValueToObject( mojo, "project", project );
+        setVariableValueToObject( mojo, "session", session );
         setVariableValueToObject( mojo, "altReleaseDeploymentRepository",  "altReleaseDeploymentRepository::http://localhost" );
 
-        ArtifactRepository repository = mock( ArtifactRepository.class );
-        when( mojo.createDeploymentArtifactRepository( "altReleaseDeploymentRepository", "http://localhost" ) ).thenReturn( repository );
-
         project.setVersion( "1.0" );
 
-        assertEquals( repository,
+        assertEquals( new RemoteRepository.Builder("altReleaseDeploymentRepository", "default", "http://localhost").build(),
                       mojo.getDeploymentRepository( project, null, "altReleaseDeploymentRepository::http://localhost", null ));
     }
     


[maven-deploy-plugin] 01/02: Apply comments

Posted by cs...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

cstamas pushed a commit to branch full-resolver
in repository https://gitbox.apache.org/repos/asf/maven-deploy-plugin.git

commit bd805053694e38c4f1510aa9c19a5426c8086442
Author: Tamas Cservenak <ta...@cservenak.net>
AuthorDate: Tue Jul 12 11:00:18 2022 +0200

    Apply comments
---
 .../maven/plugins/deploy/AbstractDeployMojo.java     | 20 ++++++++++----------
 1 file changed, 10 insertions(+), 10 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 6fe7f60..dda506b 100644
--- a/src/main/java/org/apache/maven/plugins/deploy/AbstractDeployMojo.java
+++ b/src/main/java/org/apache/maven/plugins/deploy/AbstractDeployMojo.java
@@ -123,27 +123,27 @@ public abstract class AbstractDeployMojo
 
     private RemoteRepository getRemoteRepository( ArtifactRepository remoteRepository )
     {
-        RemoteRepository aetherRepo = RepositoryUtils.toRepo( remoteRepository );
+        RemoteRepository result = RepositoryUtils.toRepo( remoteRepository );
 
-        if ( aetherRepo.getAuthentication() == null || aetherRepo.getProxy() == null )
+        if ( result.getAuthentication() == null || result.getProxy() == null )
         {
-            RemoteRepository.Builder builder = new RemoteRepository.Builder( aetherRepo );
+            RemoteRepository.Builder builder = new RemoteRepository.Builder( result );
 
-            if ( aetherRepo.getAuthentication() == null )
+            if ( result.getAuthentication() == null )
             {
                 builder.setAuthentication( session.getRepositorySession().getAuthenticationSelector()
-                        .getAuthentication( aetherRepo ) );
+                        .getAuthentication( result ) );
             }
 
-            if ( aetherRepo.getProxy() == null )
+            if ( result.getProxy() == null )
             {
-                builder.setProxy( session.getRepositorySession().getProxySelector().getProxy( aetherRepo ) );
+                builder.setProxy( session.getRepositorySession().getProxySelector().getProxy( result ) );
             }
 
-            aetherRepo = builder.build();
+            result = builder.build();
         }
 
-        return aetherRepo;
+        return result;
     }
 
     protected DeployRequest deployRequest( ArtifactRepository repository, List<Artifact> artifacts )
@@ -191,7 +191,7 @@ public abstract class AbstractDeployMojo
                 if ( count + 1 < retryFailedDeploymentCounter )
                 {
                     getLog().warn( "Encountered issue during deployment: " + e.getLocalizedMessage() );
-                    getLog().debug( e.getMessage() );
+                    getLog().debug( e );
                 }
                 if ( exception == null )
                 {