View Javadoc

1   package org.apache.maven.plugin.install;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.File;
23  import java.util.ArrayList;
24  import java.util.Collection;
25  import java.util.Collections;
26  import java.util.LinkedHashSet;
27  import java.util.List;
28  import java.util.concurrent.atomic.AtomicInteger;
29  
30  import org.apache.maven.artifact.Artifact;
31  import org.apache.maven.artifact.installer.ArtifactInstallationException;
32  import org.apache.maven.artifact.metadata.ArtifactMetadata;
33  import org.apache.maven.plugin.MojoExecutionException;
34  import org.apache.maven.plugins.annotations.Component;
35  import org.apache.maven.plugins.annotations.LifecyclePhase;
36  import org.apache.maven.plugins.annotations.Mojo;
37  import org.apache.maven.plugins.annotations.Parameter;
38  import org.apache.maven.project.MavenProject;
39  import org.apache.maven.project.artifact.ProjectArtifactMetadata;
40  
41  /**
42   * Installs the project's main artifact, and any other artifacts attached by other plugins in the lifecycle, to the
43   * local repository.
44   * 
45   * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
46   * @version $Id: InstallMojo.java 1530748 2013-10-09 18:24:31Z rfscholte $
47   */
48  @Mojo( name = "install", defaultPhase = LifecyclePhase.INSTALL, threadSafe = true )
49  public class InstallMojo
50      extends AbstractInstallMojo
51  {
52  
53      /**
54       * When building with multiple threads, reaching the last project doesn't have to mean that all projects are ready
55       * to be installed
56       */
57      private static final AtomicInteger readyProjectsCounter = new AtomicInteger();
58  
59      private static final List<InstallRequest> installRequests =
60          Collections.synchronizedList( new ArrayList<InstallRequest>() );
61  
62      /**
63       */
64      @Component
65      private MavenProject project;
66  
67      @SuppressWarnings( "MismatchedQueryAndUpdateOfCollection" )
68      @Parameter( defaultValue = "${reactorProjects}", required = true, readonly = true )
69      private List<MavenProject> reactorProjects;
70  
71      /**
72       * Whether every project should be deployed during its own deploy-phase or at the end of the multimodule build. If
73       * set to {@code true} and the build fails, none of the reactor projects is deployed
74       * 
75       * @since 2.5
76       */
77      @Parameter( defaultValue = "false", property = "installAtEnd" )
78      private boolean installAtEnd;
79  
80      /**
81       * @deprecated either use project.getPackaging() or reactorProjects.get(i).getPackaging()
82       */
83      @Parameter( defaultValue = "${project.packaging}", required = true, readonly = true )
84      protected String packaging;
85  
86      /**
87       * @deprecated either use project.getFile() or reactorProjects.get(i).getFile()
88       */
89      @Parameter( defaultValue = "${project.file}", required = true, readonly = true )
90      private File pomFile;
91  
92      /**
93       * Set this to <code>true</code> to bypass artifact installation. Use this for artifacts that does not need to be
94       * installed in the local repository.
95       * 
96       * @since 2.4
97       */
98      @Parameter( property = "maven.install.skip", defaultValue = "false" )
99      private boolean skip;
100 
101     /**
102      * @deprecated either use project.getArtifact() or reactorProjects.get(i).getArtifact()
103      */
104     @Parameter( defaultValue = "${project.artifact}", required = true, readonly = true )
105     private Artifact artifact;
106 
107     /**
108      * @deprecated either use project.getAttachedArtifacts() or reactorProjects.get(i).getAttachedArtifacts()
109      */
110     @Parameter( defaultValue = "${project.attachedArtifacts}", required = true, readonly = true )
111     private List<Artifact> attachedArtifacts;
112 
113     public void execute()
114         throws MojoExecutionException
115     {
116         boolean projectsReady = readyProjectsCounter.incrementAndGet() == reactorProjects.size();
117 
118         if ( skip )
119         {
120             getLog().info( "Skipping artifact installation" );
121         }
122         else
123         {
124             InstallRequest currentExecutionInstallRequest =
125                 new InstallRequest().setProject( project ).setCreateChecksum( createChecksum ).setUpdateReleaseInfo( updateReleaseInfo );
126 
127             if ( !installAtEnd )
128             {
129                 installProject( currentExecutionInstallRequest );
130             }
131             else
132             {
133                 installRequests.add( currentExecutionInstallRequest );
134 
135                 if ( !projectsReady )
136                 {
137                     getLog().info( "Installing " + project.getGroupId() + ":" + project.getArtifactId() + ":"
138                                        + project.getVersion() + " at end" );
139                 }
140             }
141 
142         }
143 
144         if ( projectsReady )
145         {
146             synchronized ( installRequests )
147             {
148                 while ( !installRequests.isEmpty() )
149                 {
150                     installProject( installRequests.remove( 0 ) );
151                 }
152             }
153         }
154     }
155 
156     private void installProject( InstallRequest request )
157         throws MojoExecutionException
158     {
159         MavenProject project = request.getProject();
160         boolean createChecksum = request.isCreateChecksum();
161         boolean updateReleaseInfo = request.isUpdateReleaseInfo();
162 
163         Artifact artifact = project.getArtifact();
164         String packaging = project.getPackaging();
165         File pomFile = project.getFile();
166         @SuppressWarnings( "unchecked" )
167         List<Artifact> attachedArtifacts = project.getAttachedArtifacts();
168 
169         // TODO: push into transformation
170         boolean isPomArtifact = "pom".equals( packaging );
171 
172         ArtifactMetadata metadata;
173 
174         if ( updateReleaseInfo )
175         {
176             artifact.setRelease( true );
177         }
178 
179         try
180         {
181             Collection<File> metadataFiles = new LinkedHashSet<File>();
182 
183             if ( isPomArtifact )
184             {
185                 installer.install( pomFile, artifact, localRepository );
186                 installChecksums( artifact, createChecksum );
187                 addMetaDataFilesForArtifact( artifact, metadataFiles, createChecksum );
188             }
189             else
190             {
191                 metadata = new ProjectArtifactMetadata( artifact, pomFile );
192                 artifact.addMetadata( metadata );
193 
194                 File file = artifact.getFile();
195 
196                 // Here, we have a temporary solution to MINSTALL-3 (isDirectory() is true if it went through compile
197                 // but not package). We are designing in a proper solution for Maven 2.1
198                 if ( file != null && file.isFile() )
199                 {
200                     installer.install( file, artifact, localRepository );
201                     installChecksums( artifact, createChecksum );
202                     addMetaDataFilesForArtifact( artifact, metadataFiles, createChecksum );
203                 }
204                 else if ( !attachedArtifacts.isEmpty() )
205                 {
206                     getLog().info( "No primary artifact to install, installing attached artifacts instead." );
207 
208                     Artifact pomArtifact =
209                         artifactFactory.createProjectArtifact( artifact.getGroupId(), artifact.getArtifactId(),
210                                                                artifact.getBaseVersion() );
211                     pomArtifact.setFile( pomFile );
212                     if ( updateReleaseInfo )
213                     {
214                         pomArtifact.setRelease( true );
215                     }
216 
217                     installer.install( pomFile, pomArtifact, localRepository );
218                     installChecksums( pomArtifact, createChecksum );
219                     addMetaDataFilesForArtifact( pomArtifact, metadataFiles, createChecksum );
220                 }
221                 else
222                 {
223                     throw new MojoExecutionException(
224                                                       "The packaging for this project did not assign a file to the build artifact" );
225                 }
226             }
227 
228             for ( Artifact attached : attachedArtifacts )
229             {
230                 installer.install( attached.getFile(), attached, localRepository );
231                 installChecksums( attached, createChecksum );
232                 addMetaDataFilesForArtifact( attached, metadataFiles, createChecksum );
233             }
234 
235             installChecksums( metadataFiles );
236         }
237         catch ( ArtifactInstallationException e )
238         {
239             throw new MojoExecutionException( e.getMessage(), e );
240         }
241     }
242 
243     public void setSkip( boolean skip )
244     {
245         this.skip = skip;
246     }
247 
248 }