You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by sj...@apache.org on 2022/11/03 19:34:06 UTC

[maven-install-plugin] branch MINSTALL-183 created (now 72840ca)

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

sjaranowski pushed a change to branch MINSTALL-183
in repository https://gitbox.apache.org/repos/asf/maven-install-plugin.git


      at 72840ca  [MINSTALL-183] Don't use metadata from main artifact to fetch pom.xml

This branch includes the following new commits:

     new 72840ca  [MINSTALL-183] Don't use metadata from main artifact to fetch pom.xml

The 1 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-install-plugin] 01/01: [MINSTALL-183] Don't use metadata from main artifact to fetch pom.xml

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

sjaranowski pushed a commit to branch MINSTALL-183
in repository https://gitbox.apache.org/repos/asf/maven-install-plugin.git

commit 72840ca8fdfbdf20241db5e590fc1eaf3ad88ce8
Author: Slawomir Jaranowski <s....@gmail.com>
AuthorDate: Thu Nov 3 10:31:28 2022 +0100

    [MINSTALL-183] Don't use metadata from main artifact to fetch pom.xml
---
 .../apache/maven/plugins/install/InstallMojo.java  | 71 +++++++---------------
 .../maven/plugins/install/InstallMojoTest.java     | 15 +----
 2 files changed, 24 insertions(+), 62 deletions(-)

diff --git a/src/main/java/org/apache/maven/plugins/install/InstallMojo.java b/src/main/java/org/apache/maven/plugins/install/InstallMojo.java
index 5f06261..d564ff8 100644
--- a/src/main/java/org/apache/maven/plugins/install/InstallMojo.java
+++ b/src/main/java/org/apache/maven/plugins/install/InstallMojo.java
@@ -30,7 +30,6 @@ import org.apache.maven.model.Plugin;
 import org.apache.maven.model.PluginExecution;
 import org.apache.maven.plugin.AbstractMojo;
 import org.apache.maven.plugin.MojoExecutionException;
-import org.apache.maven.plugin.MojoFailureException;
 import org.apache.maven.plugin.descriptor.PluginDescriptor;
 import org.apache.maven.plugins.annotations.Component;
 import org.apache.maven.plugins.annotations.LifecyclePhase;
@@ -38,17 +37,14 @@ 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.RepositorySystem;
-import org.eclipse.aether.artifact.Artifact;
 import org.eclipse.aether.installation.InstallRequest;
 import org.eclipse.aether.installation.InstallationException;
-import org.eclipse.aether.util.artifact.SubArtifact;
 
 /**
  * Installs the project's main artifact, and any other artifacts attached by other plugins in the lifecycle, to the
  * local repository.
- * 
+ *
  * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
  */
 @Mojo( name = "install", defaultPhase = LifecyclePhase.INSTALL, threadSafe = true )
@@ -74,7 +70,7 @@ public class InstallMojo
      * Whether every project should be installed during its own install-phase or at the end of the multimodule build. If
      * set to {@code true} and the build fails, none of the reactor projects is installed.
      * <strong>(experimental)</strong>
-     * 
+     *
      * @since 2.5
      */
     @Parameter( defaultValue = "false", property = "installAtEnd" )
@@ -83,7 +79,7 @@ public class InstallMojo
     /**
      * Set this to <code>true</code> to bypass artifact installation. Use this for artifacts that do not need to be
      * installed in the local repository.
-     * 
+     *
      * @since 2.4
      */
     @Parameter( property = "maven.install.skip", defaultValue = "false" )
@@ -115,7 +111,7 @@ public class InstallMojo
 
     @Override
     public void execute()
-        throws MojoExecutionException, MojoFailureException
+        throws MojoExecutionException
     {
         if ( skip )
         {
@@ -194,16 +190,12 @@ public class InstallMojo
         return false;
     }
 
-    private void installProject( MavenProject project ) throws MojoExecutionException, MojoFailureException
+    private void installProject( MavenProject project ) throws MojoExecutionException
     {
         try
         {
             repositorySystem.install( session.getRepositorySession(), processProject( project ) );
         }
-        catch ( IllegalArgumentException e )
-        {
-            throw new MojoFailureException( e.getMessage(), e );
-        }
         catch ( InstallationException e )
         {
             throw new MojoExecutionException( e.getMessage(), e );
@@ -213,63 +205,40 @@ public class InstallMojo
     /**
      * Processes passed in {@link MavenProject} and produces {@link InstallRequest} out of it.
      *
-     * @throws IllegalArgumentException if project is badly set up.
+     * @throws MojoExecutionException if project is badly set up.
      */
-    private InstallRequest processProject( MavenProject project )
+    private InstallRequest processProject( MavenProject project ) throws MojoExecutionException
     {
         InstallRequest request = new InstallRequest();
-        org.apache.maven.artifact.Artifact mavenMainArtifact = project.getArtifact();
-        String packaging = project.getPackaging();
-        File pomFile = project.getFile();
-        boolean isPomArtifact = "pom".equals( packaging );
-        boolean pomArtifactAttached = false;
 
-        if ( pomFile != null )
+        if ( isFile( project.getFile() ) )
         {
             request.addArtifact( RepositoryUtils.toArtifact( new ProjectArtifact( project ) ) );
-            pomArtifactAttached = true;
+        }
+        else
+        {
+            throw new MojoExecutionException( "The project POM could not be attached" );
         }
 
-        if ( !isPomArtifact )
+        if ( !"pom".equals( project.getPackaging() ) )
         {
-            File file = mavenMainArtifact.getFile();
-            if ( file != null && file.isFile() )
+            org.apache.maven.artifact.Artifact mavenMainArtifact = project.getArtifact();
+            if ( isFile( mavenMainArtifact.getFile() ) )
             {
-                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;
-                        }
-                    }
-                }
+                request.addArtifact( RepositoryUtils.toArtifact( mavenMainArtifact ) );
             }
             else if ( !project.getAttachedArtifacts().isEmpty() )
             {
-                throw new IllegalArgumentException( "The packaging plugin for this project did not assign "
+                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 IllegalArgumentException( "The packaging for this project did not assign "
+                throw new MojoExecutionException( "The packaging for this project did not assign "
                         + "a file to the build artifact" );
             }
         }
 
-        if ( !pomArtifactAttached )
-        {
-            throw new IllegalArgumentException( "The POM could not be attached" );
-        }
 
         for ( org.apache.maven.artifact.Artifact attached : project.getAttachedArtifacts() )
         {
@@ -280,4 +249,8 @@ public class InstallMojo
         return request;
     }
 
+    private boolean isFile( File file )
+    {
+        return file != null && file.isFile();
+    }
 }
diff --git a/src/test/java/org/apache/maven/plugins/install/InstallMojoTest.java b/src/test/java/org/apache/maven/plugins/install/InstallMojoTest.java
index 04c26e2..134b9eb 100644
--- a/src/test/java/org/apache/maven/plugins/install/InstallMojoTest.java
+++ b/src/test/java/org/apache/maven/plugins/install/InstallMojoTest.java
@@ -25,12 +25,11 @@ import java.util.List;
 import java.util.concurrent.ConcurrentHashMap;
 
 import org.apache.maven.artifact.Artifact;
-import org.apache.maven.artifact.metadata.ArtifactMetadata;
 import org.apache.maven.execution.MavenSession;
 import org.apache.maven.model.Build;
 import org.apache.maven.model.Plugin;
 import org.apache.maven.plugin.AbstractMojo;
-import org.apache.maven.plugin.MojoFailureException;
+import org.apache.maven.plugin.MojoExecutionException;
 import org.apache.maven.plugin.descriptor.PluginDescriptor;
 import org.apache.maven.plugin.testing.AbstractMojoTestCase;
 import org.apache.maven.plugins.install.stubs.AttachedArtifactStub0;
@@ -216,7 +215,7 @@ public class InstallMojoTest
 
             fail( "Did not throw mojo execution exception" );
         }
-        catch ( MojoFailureException e )
+        catch ( MojoExecutionException e )
         {
             //expected
         }
@@ -286,16 +285,6 @@ public class InstallMojoTest
 
         mojo.execute();
 
-        ArtifactMetadata metadata = null;
-        for ( Object o : artifact.getMetadataList() )
-        {
-            metadata = (ArtifactMetadata) o;
-            if ( metadata.getRemoteFilename().endsWith( "pom" ) )
-            {
-                break;
-            }
-        }
-
         File pom = new File( new File( LOCAL_REPO ), mavenSession.getRepositorySession().getLocalRepositoryManager().getPathForLocalArtifact( new DefaultArtifact( artifact.getGroupId(), artifact.getArtifactId(), "pom", artifact.getVersion() ) ) );
 
         assertTrue( pom.exists() );