View Javadoc
1   package org.apache.maven.plugins.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.List;
24  import java.util.Map;
25  
26  import org.apache.maven.RepositoryUtils;
27  import org.apache.maven.execution.MavenSession;
28  import org.apache.maven.plugin.AbstractMojo;
29  import org.apache.maven.plugin.MojoExecutionException;
30  import org.apache.maven.plugin.MojoFailureException;
31  import org.apache.maven.plugin.descriptor.PluginDescriptor;
32  import org.apache.maven.plugins.annotations.Component;
33  import org.apache.maven.plugins.annotations.LifecyclePhase;
34  import org.apache.maven.plugins.annotations.Mojo;
35  import org.apache.maven.plugins.annotations.Parameter;
36  import org.apache.maven.project.MavenProject;
37  import org.apache.maven.project.artifact.ProjectArtifact;
38  import org.apache.maven.project.artifact.ProjectArtifactMetadata;
39  import org.eclipse.aether.RepositorySystem;
40  import org.eclipse.aether.artifact.Artifact;
41  import org.eclipse.aether.installation.InstallRequest;
42  import org.eclipse.aether.installation.InstallationException;
43  import org.eclipse.aether.util.artifact.SubArtifact;
44  
45  /**
46   * Installs the project's main artifact, and any other artifacts attached by other plugins in the lifecycle, to the
47   * local repository.
48   * 
49   * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
50   */
51  @Mojo( name = "install", defaultPhase = LifecyclePhase.INSTALL, threadSafe = true )
52  public class InstallMojo
53          extends AbstractMojo
54  {
55      @Component
56      private RepositorySystem repositorySystem;
57  
58      @Parameter( defaultValue = "${session}", required = true, readonly = true )
59      private MavenSession session;
60  
61      @Parameter( defaultValue = "${project}", readonly = true, required = true )
62      private MavenProject project;
63  
64      @Parameter( defaultValue = "${reactorProjects}", required = true, readonly = true )
65      private List<MavenProject> reactorProjects;
66  
67      @Parameter( defaultValue = "${plugin}", required = true, readonly = true )
68      private PluginDescriptor pluginDescriptor;
69  
70      /**
71       * Whether every project should be installed during its own install-phase or at the end of the multimodule build. If
72       * set to {@code true} and the build fails, none of the reactor projects is installed.
73       * <strong>(experimental)</strong>
74       * 
75       * @since 2.5
76       */
77      @Parameter( defaultValue = "false", property = "installAtEnd" )
78      private boolean installAtEnd;
79  
80      /**
81       * Set this to <code>true</code> to bypass artifact installation. Use this for artifacts that do not need to be
82       * installed in the local repository.
83       * 
84       * @since 2.4
85       */
86      @Parameter( property = "maven.install.skip", defaultValue = "false" )
87      private boolean skip;
88  
89      private enum State
90      {
91          SKIPPED, INSTALLED, TO_BE_INSTALLED
92      }
93  
94      private static final String INSTALL_PROCESSED_MARKER = InstallMojo.class.getName() + ".processed";
95  
96      private void putState( State state )
97      {
98          getPluginContext().put( INSTALL_PROCESSED_MARKER, state.name() );
99      }
100 
101     private State getState( MavenProject project )
102     {
103         Map<String, Object> pluginContext = session.getPluginContext( pluginDescriptor, project );
104         return State.valueOf( (String) pluginContext.get( INSTALL_PROCESSED_MARKER ) );
105     }
106 
107     private boolean hasState( MavenProject project )
108     {
109         Map<String, Object> pluginContext = session.getPluginContext( pluginDescriptor, project );
110         return pluginContext.containsKey( INSTALL_PROCESSED_MARKER );
111     }
112 
113     @Override
114     public void execute()
115         throws MojoExecutionException, MojoFailureException
116     {
117         if ( skip )
118         {
119             getLog().info( "Skipping artifact installation" );
120             putState( State.SKIPPED );
121         }
122         else
123         {
124             if ( !installAtEnd )
125             {
126                 installProject( project );
127                 putState( State.INSTALLED );
128             }
129             else
130             {
131                 getLog().info( "Deferring install for " + project.getGroupId()
132                         + ":" + project.getArtifactId() + ":" + project.getVersion() + " at end" );
133                 putState( State.TO_BE_INSTALLED );
134             }
135         }
136 
137         if ( allProjectsMarked() )
138         {
139             for ( MavenProject reactorProject : reactorProjects )
140             {
141                 State state = getState( reactorProject );
142                 if ( state == State.TO_BE_INSTALLED )
143                 {
144                     installProject( reactorProject );
145                 }
146             }
147         }
148     }
149 
150     private boolean allProjectsMarked()
151     {
152         for ( MavenProject reactorProject : reactorProjects )
153         {
154             if ( !hasState( reactorProject ) )
155             {
156                 return false;
157             }
158         }
159         return true;
160     }
161 
162     private void installProject( MavenProject project ) throws MojoExecutionException, MojoFailureException
163     {
164         try
165         {
166             repositorySystem.install( session.getRepositorySession(), processProject( project ) );
167         }
168         catch ( IllegalArgumentException e )
169         {
170             throw new MojoFailureException( e.getMessage(), e );
171         }
172         catch ( InstallationException e )
173         {
174             throw new MojoExecutionException( e.getMessage(), e );
175         }
176     }
177 
178     /**
179      * Processes passed in {@link MavenProject} and produces {@link InstallRequest} out of it.
180      *
181      * @throws IllegalArgumentException if project is badly set up.
182      */
183     private InstallRequest processProject( MavenProject project )
184     {
185         InstallRequest request = new InstallRequest();
186         org.apache.maven.artifact.Artifact mavenMainArtifact = project.getArtifact();
187         String packaging = project.getPackaging();
188         File pomFile = project.getFile();
189         boolean isPomArtifact = "pom".equals( packaging );
190         boolean pomArtifactAttached = false;
191 
192         if ( pomFile != null )
193         {
194             request.addArtifact( RepositoryUtils.toArtifact( new ProjectArtifact( project ) ) );
195             pomArtifactAttached = true;
196         }
197 
198         if ( !isPomArtifact )
199         {
200             File file = mavenMainArtifact.getFile();
201             if ( file != null && file.isFile() )
202             {
203                 Artifact mainArtifact = RepositoryUtils.toArtifact( mavenMainArtifact );
204                 request.addArtifact( mainArtifact );
205 
206                 if ( !pomArtifactAttached )
207                 {
208                     for ( Object metadata : mavenMainArtifact.getMetadataList() )
209                     {
210                         if ( metadata instanceof ProjectArtifactMetadata )
211                         {
212                             request.addArtifact( new SubArtifact(
213                                     mainArtifact,
214                                     "",
215                                     "pom"
216                             ).setFile( ( (ProjectArtifactMetadata) metadata ).getFile() ) );
217                             pomArtifactAttached = true;
218                         }
219                     }
220                 }
221             }
222             else if ( !project.getAttachedArtifacts().isEmpty() )
223             {
224                 throw new IllegalArgumentException( "The packaging plugin for this project did not assign "
225                         + "a main file to the project but it has attachments. Change packaging to 'pom'." );
226             }
227             else
228             {
229                 throw new IllegalArgumentException( "The packaging for this project did not assign "
230                         + "a file to the build artifact" );
231             }
232         }
233 
234         if ( !pomArtifactAttached )
235         {
236             throw new IllegalArgumentException( "The POM could not be attached" );
237         }
238 
239         for ( org.apache.maven.artifact.Artifact attached : project.getAttachedArtifacts() )
240         {
241             getLog().debug( "Attaching for install: " + attached.getId() );
242             request.addArtifact( RepositoryUtils.toArtifact( attached ) );
243         }
244 
245         return request;
246     }
247 
248 }