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.IOException;
23  import java.util.ArrayList;
24  import java.util.Collections;
25  import java.util.List;
26  import java.util.concurrent.atomic.AtomicInteger;
27  
28  import org.apache.maven.plugin.MojoExecutionException;
29  import org.apache.maven.plugin.MojoFailureException;
30  import org.apache.maven.plugins.annotations.Component;
31  import org.apache.maven.plugins.annotations.LifecyclePhase;
32  import org.apache.maven.plugins.annotations.Mojo;
33  import org.apache.maven.plugins.annotations.Parameter;
34  import org.apache.maven.project.MavenProject;
35  import org.apache.maven.project.ProjectBuildingRequest;
36  import org.apache.maven.shared.transfer.artifact.install.ArtifactInstallerException;
37  import org.apache.maven.shared.transfer.project.NoFileAssignedException;
38  import org.apache.maven.shared.transfer.project.install.ProjectInstaller;
39  import org.apache.maven.shared.transfer.project.install.ProjectInstallerRequest;
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   */
47  @Mojo( name = "install", defaultPhase = LifecyclePhase.INSTALL, threadSafe = true )
48  public class InstallMojo
49      extends AbstractInstallMojo
50  {
51  
52      /**
53       * When building with multiple threads, reaching the last project doesn't have to mean that all projects are ready
54       * to be installed
55       */
56      private static final AtomicInteger READYPROJECTSCOUTNER = new AtomicInteger();
57  
58      private static final List<ProjectInstallerRequest> INSTALLREQUESTS =
59          Collections.synchronizedList( new ArrayList<ProjectInstallerRequest>() );
60  
61      /**
62       */
63      @Parameter( defaultValue = "${project}", readonly = true, required = true )
64      private MavenProject project;
65  
66      @Parameter( defaultValue = "${reactorProjects}", required = true, readonly = true )
67      private List<MavenProject> reactorProjects;
68  
69      /**
70       * Whether every project should be installed during its own install-phase or at the end of the multimodule build. If
71       * set to {@code true} and the build fails, none of the reactor projects is installed.
72       * <strong>(experimental)</strong>
73       * 
74       * @since 2.5
75       */
76      @Parameter( defaultValue = "false", property = "installAtEnd" )
77      private boolean installAtEnd;
78  
79      /**
80       * Set this to <code>true</code> to bypass artifact installation. Use this for artifacts that does not need to be
81       * installed in the local repository.
82       * 
83       * @since 2.4
84       */
85      @Parameter( property = "maven.install.skip", defaultValue = "false" )
86      private boolean skip;
87  
88      @Component
89      private ProjectInstaller installer;
90  
91      public void execute()
92          throws MojoExecutionException, MojoFailureException
93      {
94          boolean addedInstallRequest = false;
95          if ( skip )
96          {
97              getLog().info( "Skipping artifact installation" );
98          }
99          else
100         {
101             // CHECKSTYLE_OFF: LineLength
102             ProjectInstallerRequest projectInstallerRequest =
103                 new ProjectInstallerRequest().setProject( project );
104             // CHECKSTYLE_ON: LineLength
105 
106             if ( !installAtEnd )
107             {
108                 installProject( session.getProjectBuildingRequest(), projectInstallerRequest );
109             }
110             else
111             {
112                 INSTALLREQUESTS.add( projectInstallerRequest );
113                 addedInstallRequest = true;
114             }
115         }
116 
117         boolean projectsReady = READYPROJECTSCOUTNER.incrementAndGet() == reactorProjects.size();
118         if ( projectsReady )
119         {
120             synchronized ( INSTALLREQUESTS )
121             {
122                 while ( !INSTALLREQUESTS.isEmpty() )
123                 {
124                     installProject( session.getProjectBuildingRequest(), INSTALLREQUESTS.remove( 0 ) );
125                 }
126             }
127         }
128         else if ( addedInstallRequest )
129         {
130             getLog().info( "Installing " + project.getGroupId() + ":" + project.getArtifactId() + ":"
131                 + project.getVersion() + " at end" );
132         }
133     }
134 
135     private void installProject( ProjectBuildingRequest pbr, ProjectInstallerRequest pir )
136         throws MojoFailureException, MojoExecutionException
137     {
138         try
139         {
140             installer.install( session.getProjectBuildingRequest(), pir );
141         }
142         catch ( IOException e )
143         {
144             throw new MojoFailureException( "IOException", e );
145         }
146         catch ( ArtifactInstallerException e )
147         {
148             throw new MojoExecutionException( "ArtifactInstallerException", e );
149         }
150         catch ( NoFileAssignedException e )
151         {
152             throw new MojoExecutionException( "NoFileAssignedException", e );
153         }
154 
155     }
156 
157     public void setSkip( boolean skip )
158     {
159         this.skip = skip;
160     }
161 
162 }