You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by rf...@apache.org on 2016/06/08 21:23:55 UTC

svn commit: r1747455 - /maven/plugins/trunk/maven-invoker-plugin/src/main/java/org/apache/maven/plugin/invoker/InstallMojo.java

Author: rfscholte
Date: Wed Jun  8 21:23:54 2016
New Revision: 1747455

URL: http://svn.apache.org/viewvc?rev=1747455&view=rev
Log:
[MINVOKER-204] Upgrade to Maven3 plugin
Introduce repositoryManager for localRepository related interaction

Modified:
    maven/plugins/trunk/maven-invoker-plugin/src/main/java/org/apache/maven/plugin/invoker/InstallMojo.java

Modified: maven/plugins/trunk/maven-invoker-plugin/src/main/java/org/apache/maven/plugin/invoker/InstallMojo.java
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-invoker-plugin/src/main/java/org/apache/maven/plugin/invoker/InstallMojo.java?rev=1747455&r1=1747454&r2=1747455&view=diff
==============================================================================
--- maven/plugins/trunk/maven-invoker-plugin/src/main/java/org/apache/maven/plugin/invoker/InstallMojo.java (original)
+++ maven/plugins/trunk/maven-invoker-plugin/src/main/java/org/apache/maven/plugin/invoker/InstallMojo.java Wed Jun  8 21:23:54 2016
@@ -28,7 +28,6 @@ import java.util.HashSet;
 import java.util.LinkedHashSet;
 import java.util.List;
 import java.util.Map;
-import java.util.Set;
 
 import org.apache.maven.artifact.Artifact;
 import org.apache.maven.artifact.factory.ArtifactFactory;
@@ -48,7 +47,9 @@ import org.apache.maven.plugins.annotati
 import org.apache.maven.plugins.annotations.Parameter;
 import org.apache.maven.plugins.annotations.ResolutionScope;
 import org.apache.maven.project.MavenProject;
+import org.apache.maven.project.ProjectBuildingRequest;
 import org.apache.maven.shared.artifact.install.ArtifactInstaller;
+import org.apache.maven.shared.repository.RepositoryManager;
 import org.codehaus.plexus.util.FileUtils;
 
 /**
@@ -73,6 +74,9 @@ public class InstallMojo
      */
     @Component
     private ArtifactInstaller installer;
+    
+    @Component
+    private RepositoryManager repositoryManager;
 
     /**
      * The component used to create artifacts.
@@ -97,7 +101,8 @@ public class InstallMojo
      * possibly broken artifacts, it is strongly recommended to use an isolated repository for the integration tests
      * (e.g. <code>${project.build.directory}/it-repo</code>).
      */
-    @Parameter( property = "invoker.localRepositoryPath" )
+    @Parameter( property = "invoker.localRepositoryPath", 
+                defaultValue = "${session.localRepository.basedir}", required = true )
     private File localRepositoryPath;
 
     /**
@@ -177,6 +182,8 @@ public class InstallMojo
     @Component
     private ArtifactMetadataSource artifactMetadataSource;
 
+    private ProjectBuildingRequest projectBuildingRequest;
+
     /**
      * Performs this mojo's tasks.
      *
@@ -191,16 +198,16 @@ public class InstallMojo
             return;
         }
 
-        ArtifactRepository testRepository = createTestRepository();
+        createTestRepository();
 
         installedArtifacts = new HashSet<String>();
         copiedArtifacts = new HashSet<String>();
 
-        installProjectDependencies( project, reactorProjects, testRepository );
-        installProjectParents( project, testRepository );
-        installProjectArtifacts( project, testRepository );
+        installProjectDependencies( project, reactorProjects );
+        installProjectParents( project );
+        installProjectArtifacts( project );
 
-        installExtraArtifacts( testRepository, extraArtifacts );
+        installExtraArtifacts( extraArtifacts );
     }
 
     /**
@@ -212,50 +219,31 @@ public class InstallMojo
      * @return The local repository for the integration tests, never <code>null</code>.
      * @throws MojoExecutionException If the repository could not be created.
      */
-    private ArtifactRepository createTestRepository()
+    private void createTestRepository()
         throws MojoExecutionException
     {
-        ArtifactRepository testRepository = localRepository;
-
-        if ( localRepositoryPath != null )
+        
+        if ( !localRepositoryPath.exists() && !localRepositoryPath.mkdirs() )
         {
-            try
-            {
-                if ( !localRepositoryPath.exists() && !localRepositoryPath.mkdirs() )
-                {
-                    throw new IOException( "Failed to create directory: " + localRepositoryPath );
-                }
-
-                testRepository =
-                    repositoryFactory.createArtifactRepository( localRepository.getId(),
-                                                                localRepositoryPath.toURL().toExternalForm(),
-                                                                localRepository.getLayout(),
-                                                                localRepository.getSnapshots(),
-                                                                localRepository.getReleases() );
-            }
-            catch ( Exception e )
-            {
-                throw new MojoExecutionException( "Failed to create local repository: " + localRepositoryPath, e );
-            }
+            throw new MojoExecutionException( "Failed to create directory: " + localRepositoryPath );
         }
-
-        return testRepository;
+        projectBuildingRequest =
+            repositoryManager.setLocalRepositoryBasedir( session.getProjectBuildingRequest(), localRepositoryPath );
     }
 
     /**
      * Installs the specified artifact to the local repository. Note: This method should only be used for artifacts that
      * originate from the current (reactor) build. Artifacts that have been grabbed from the user's local repository
-     * should be installed to the test repository via {@link #copyArtifact(File, Artifact, ArtifactRepository)}.
+     * should be installed to the test repository via {@link #copyArtifact(File, Artifact)}.
      *
      * @param file The file associated with the artifact, must not be <code>null</code>. This is in most cases the value
      *            of <code>artifact.getFile()</code> with the exception of the main artifact from a project with
      *            packaging "pom". Projects with packaging "pom" have no main artifact file. They have however artifact
      *            metadata (e.g. site descriptors) which needs to be installed.
      * @param artifact The artifact to install, must not be <code>null</code>.
-     * @param testRepository The local repository to install the artifact to, must not be <code>null</code>.
      * @throws MojoExecutionException If the artifact could not be installed (e.g. has no associated file).
      */
-    private void installArtifact( File file, Artifact artifact, ArtifactRepository testRepository )
+    private void installArtifact( File file, Artifact artifact )
         throws MojoExecutionException
     {
         try
@@ -272,7 +260,7 @@ public class InstallMojo
             if ( installedArtifacts.add( artifact.getId() ) )
             {
                 artifact.setFile( file );
-                installer.install( session.getProjectBuildingRequest(), new File( testRepository.getBasedir() ),
+                installer.install( session.getProjectBuildingRequest(), localRepositoryPath,
                                    Collections.singletonList( artifact ) );
             }
             else
@@ -288,17 +276,16 @@ public class InstallMojo
 
     /**
      * Installs the specified artifact to the local repository. This method serves basically the same purpose as
-     * {@link #installArtifact(File, Artifact, ArtifactRepository)} but is meant for artifacts that have been resolved
+     * {@link #installArtifact(File, Artifact)} but is meant for artifacts that have been resolved
      * from the user's local repository (and not the current build outputs). The subtle difference here is that
      * artifacts from the repository have already undergone transformations and these manipulations should not be redone
      * by the artifact installer. For this reason, this method performs plain copy operations to install the artifacts.
      *
      * @param file The file associated with the artifact, must not be <code>null</code>.
      * @param artifact The artifact to install, must not be <code>null</code>.
-     * @param testRepository The local repository to install the artifact to, must not be <code>null</code>.
      * @throws MojoExecutionException If the artifact could not be installed (e.g. has no associated file).
      */
-    private void copyArtifact( File file, Artifact artifact, ArtifactRepository testRepository )
+    private void copyArtifact( File file, Artifact artifact )
         throws MojoExecutionException
     {
         try
@@ -314,7 +301,9 @@ public class InstallMojo
 
             if ( copiedArtifacts.add( artifact.getId() ) )
             {
-                File destination = new File( testRepository.getBasedir(), testRepository.pathOf( artifact ) );
+                File destination =
+                    new File( localRepositoryPath,
+                              repositoryManager.getPathForLocalArtifact( projectBuildingRequest, artifact ) );
 
                 getLog().debug( "Installing " + file + " to " + destination );
 
@@ -347,29 +336,28 @@ public class InstallMojo
      * Installs the main artifact and any attached artifacts of the specified project to the local repository.
      *
      * @param mvnProject The project whose artifacts should be installed, must not be <code>null</code>.
-     * @param testRepository The local repository to install the artifacts to, must not be <code>null</code>.
      * @throws MojoExecutionException If any artifact could not be installed.
      */
-    private void installProjectArtifacts( MavenProject mvnProject, ArtifactRepository testRepository )
+    private void installProjectArtifacts( MavenProject mvnProject )
         throws MojoExecutionException
     {
         try
         {
             // Install POM (usually attached as metadata but that happens only as a side effect of the Install Plugin)
-            installProjectPom( mvnProject, testRepository );
+            installProjectPom( mvnProject );
 
             // Install the main project artifact (if the project has one, e.g. has no "pom" packaging)
             Artifact mainArtifact = mvnProject.getArtifact();
             if ( mainArtifact.getFile() != null )
             {
-                installArtifact( mainArtifact.getFile(), mainArtifact, testRepository );
+                installArtifact( mainArtifact.getFile(), mainArtifact );
             }
 
             // Install any attached project artifacts
             Collection<Artifact> attachedArtifacts = (Collection<Artifact>) mvnProject.getAttachedArtifacts();
             for ( Artifact attachedArtifact : attachedArtifacts )
             {
-                installArtifact( attachedArtifact.getFile(), attachedArtifact, testRepository );
+                installArtifact( attachedArtifact.getFile(), attachedArtifact );
             }
         }
         catch ( Exception e )
@@ -383,10 +371,9 @@ public class InstallMojo
      * from the reactor must be installed or the forked IT builds will fail when using a clean repository.
      *
      * @param mvnProject The project whose parent POMs should be installed, must not be <code>null</code>.
-     * @param testRepository The local repository to install the POMs to, must not be <code>null</code>.
      * @throws MojoExecutionException If any POM could not be installed.
      */
-    private void installProjectParents( MavenProject mvnProject, ArtifactRepository testRepository )
+    private void installProjectParents( MavenProject mvnProject )
         throws MojoExecutionException
     {
         try
@@ -395,10 +382,10 @@ public class InstallMojo
             {
                 if ( parent.getFile() == null )
                 {
-                    copyParentPoms( parent.getGroupId(), parent.getArtifactId(), parent.getVersion(), testRepository );
+                    copyParentPoms( parent.getGroupId(), parent.getArtifactId(), parent.getVersion() );
                     break;
                 }
-                installProjectPom( parent, testRepository );
+                installProjectPom( parent );
             }
         }
         catch ( Exception e )
@@ -411,10 +398,9 @@ public class InstallMojo
      * Installs the POM of the specified project to the local repository.
      *
      * @param mvnProject The project whose POM should be installed, must not be <code>null</code>.
-     * @param testRepository The local repository to install the POM to, must not be <code>null</code>.
      * @throws MojoExecutionException If the POM could not be installed.
      */
-    private void installProjectPom( MavenProject mvnProject, ArtifactRepository testRepository )
+    private void installProjectPom( MavenProject mvnProject )
         throws MojoExecutionException
     {
         try
@@ -430,7 +416,7 @@ public class InstallMojo
                     artifactFactory.createProjectArtifact( mvnProject.getGroupId(), mvnProject.getArtifactId(),
                                                            mvnProject.getVersion() );
             }
-            installArtifact( mvnProject.getFile(), pomArtifact, testRepository );
+            installArtifact( mvnProject.getFile(), pomArtifact );
         }
         catch ( Exception e )
         {
@@ -444,11 +430,9 @@ public class InstallMojo
      *
      * @param mvnProject The project whose dependent projects should be installed, must not be <code>null</code>.
      * @param reactorProjects The set of projects in the reactor build, must not be <code>null</code>.
-     * @param testRepository The local repository to install the POMs to, must not be <code>null</code>.
      * @throws MojoExecutionException If any dependency could not be installed.
      */
-    private void installProjectDependencies( MavenProject mvnProject, Collection<MavenProject> reactorProjects,
-                                             ArtifactRepository testRepository )
+    private void installProjectDependencies( MavenProject mvnProject, Collection<MavenProject> reactorProjects )
         throws MojoExecutionException
     {
         // keep track if we have passed mvnProject in reactorProjects
@@ -498,7 +482,7 @@ public class InstallMojo
             // copy dependencies that where resolved from the local repo
             for ( Artifact artifact : dependencyArtifacts )
             {
-                copyArtifact( artifact, testRepository );
+                copyArtifact( artifact );
             }
 
             // install dependencies that were resolved from the reactor
@@ -506,8 +490,8 @@ public class InstallMojo
             {
                 MavenProject dependencyProject = projects.get( projectId );
 
-                installProjectArtifacts( dependencyProject, testRepository );
-                installProjectParents( dependencyProject, testRepository );
+                installProjectArtifacts( dependencyProject );
+                installProjectParents( dependencyProject );
             }
         }
         catch ( Exception e )
@@ -516,10 +500,10 @@ public class InstallMojo
         }
     }
 
-    private void copyArtifact( Artifact artifact, ArtifactRepository testRepository )
+    private void copyArtifact( Artifact artifact )
         throws MojoExecutionException
     {
-        copyPoms( artifact, testRepository );
+        copyPoms( artifact );
 
         Artifact depArtifact =
             artifactFactory.createArtifactWithClassifier( artifact.getGroupId(), artifact.getArtifactId(),
@@ -528,10 +512,10 @@ public class InstallMojo
 
         File artifactFile = artifact.getFile();
 
-        copyArtifact( artifactFile, depArtifact, testRepository );
+        copyArtifact( artifactFile, depArtifact );
     }
 
-    private void copyPoms( Artifact artifact, ArtifactRepository testRepository )
+    private void copyPoms( Artifact artifact )
         throws MojoExecutionException
     {
         Artifact pomArtifact =
@@ -542,8 +526,8 @@ public class InstallMojo
 
         if ( pomFile.isFile() )
         {
-            copyArtifact( pomFile, pomArtifact, testRepository );
-            copyParentPoms( pomFile, testRepository );
+            copyArtifact( pomFile, pomArtifact );
+            copyParentPoms( pomFile );
         }
     }
 
@@ -551,17 +535,16 @@ public class InstallMojo
      * Installs all parent POMs of the specified POM file that are available in the local repository.
      *
      * @param pomFile The path to the POM file whose parents should be installed, must not be <code>null</code>.
-     * @param testRepository The local repository to install the POMs to, must not be <code>null</code>.
      * @throws MojoExecutionException If any (existing) parent POM could not be installed.
      */
-    private void copyParentPoms( File pomFile, ArtifactRepository testRepository )
+    private void copyParentPoms( File pomFile )
         throws MojoExecutionException
     {
         Model model = PomUtils.loadPom( pomFile );
         Parent parent = model.getParent();
         if ( parent != null )
         {
-            copyParentPoms( parent.getGroupId(), parent.getArtifactId(), parent.getVersion(), testRepository );
+            copyParentPoms( parent.getGroupId(), parent.getArtifactId(), parent.getVersion() );
         }
     }
 
@@ -571,10 +554,9 @@ public class InstallMojo
      * @param groupId The group id of the POM which should be installed, must not be <code>null</code>.
      * @param artifactId The artifact id of the POM which should be installed, must not be <code>null</code>.
      * @param version The version of the POM which should be installed, must not be <code>null</code>.
-     * @param testRepository The local repository to install the POMs to, must not be <code>null</code>.
      * @throws MojoExecutionException If any (existing) parent POM could not be installed.
      */
-    private void copyParentPoms( String groupId, String artifactId, String version, ArtifactRepository testRepository )
+    private void copyParentPoms( String groupId, String artifactId, String version )
         throws MojoExecutionException
     {
         Artifact pomArtifact = artifactFactory.createProjectArtifact( groupId, artifactId, version );
@@ -588,12 +570,12 @@ public class InstallMojo
         File pomFile = new File( localRepository.getBasedir(), localRepository.pathOf( pomArtifact ) );
         if ( pomFile.isFile() )
         {
-            copyArtifact( pomFile, pomArtifact, testRepository );
-            copyParentPoms( pomFile, testRepository );
+            copyArtifact( pomFile, pomArtifact );
+            copyParentPoms( pomFile );
         }
     }
 
-    private void installExtraArtifacts( ArtifactRepository testRepository, String[] extraArtifacts )
+    private void installExtraArtifacts( String[] extraArtifacts )
         throws MojoExecutionException
     {
         if ( extraArtifacts == null )
@@ -652,12 +634,12 @@ public class InstallMojo
                 {
                     artifact =
                         artifactFactory.createArtifactWithClassifier( groupId, artifactId, version, type, classifier );
-                    copyPoms( artifact, testRepository );
+                    copyPoms( artifact );
                 }
 
-                for ( Artifact arrArtifact : (Set<Artifact>) arr.getArtifacts() )
+                for ( Artifact arrArtifact : arr.getArtifacts() )
                 {
-                    copyArtifact( arrArtifact, testRepository );
+                    copyArtifact( arrArtifact );
                 }
             }
             catch ( Exception e )