1 package org.apache.maven.plugins.install;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.File;
23 import java.util.ArrayList;
24 import java.util.List;
25 import java.util.Map;
26
27 import org.apache.maven.RepositoryUtils;
28 import org.apache.maven.execution.MavenSession;
29 import org.apache.maven.model.Plugin;
30 import org.apache.maven.model.PluginExecution;
31 import org.apache.maven.plugin.AbstractMojo;
32 import org.apache.maven.plugin.MojoExecutionException;
33 import org.apache.maven.plugin.descriptor.PluginDescriptor;
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.ProjectArtifact;
40 import org.eclipse.aether.RepositorySystem;
41 import org.eclipse.aether.installation.InstallRequest;
42 import org.eclipse.aether.installation.InstallationException;
43
44
45
46
47
48
49
50 @Mojo( name = "install", defaultPhase = LifecyclePhase.INSTALL, threadSafe = true )
51 public class InstallMojo
52 extends AbstractMojo
53 {
54 @Component
55 private RepositorySystem repositorySystem;
56
57 @Parameter( defaultValue = "${session}", required = true, readonly = true )
58 private MavenSession session;
59
60 @Parameter( defaultValue = "${project}", readonly = true, required = true )
61 private MavenProject project;
62
63 @Parameter( defaultValue = "${reactorProjects}", required = true, readonly = true )
64 private List<MavenProject> reactorProjects;
65
66 @Parameter( defaultValue = "${plugin}", required = true, readonly = true )
67 private PluginDescriptor pluginDescriptor;
68
69
70
71
72
73
74
75
76 @Parameter( defaultValue = "false", property = "installAtEnd" )
77 private boolean installAtEnd;
78
79
80
81
82
83
84
85 @Parameter( property = "maven.install.skip", defaultValue = "false" )
86 private boolean skip;
87
88 private enum State
89 {
90 SKIPPED, INSTALLED, TO_BE_INSTALLED
91 }
92
93 private static final String INSTALL_PROCESSED_MARKER = InstallMojo.class.getName() + ".processed";
94
95 private void putState( State state )
96 {
97 getPluginContext().put( INSTALL_PROCESSED_MARKER, state.name() );
98 }
99
100 private State getState( MavenProject project )
101 {
102 Map<String, Object> pluginContext = session.getPluginContext( pluginDescriptor, project );
103 return State.valueOf( (String) pluginContext.get( INSTALL_PROCESSED_MARKER ) );
104 }
105
106 private boolean hasState( MavenProject project )
107 {
108 Map<String, Object> pluginContext = session.getPluginContext( pluginDescriptor, project );
109 return pluginContext.containsKey( INSTALL_PROCESSED_MARKER );
110 }
111
112 @Override
113 public void execute()
114 throws MojoExecutionException
115 {
116 if ( skip )
117 {
118 getLog().info( "Skipping artifact installation" );
119 putState( State.SKIPPED );
120 }
121 else
122 {
123 if ( !installAtEnd )
124 {
125 InstallRequest request = new InstallRequest();
126 processProject( project, request );
127 installProject( request );
128 putState( State.INSTALLED );
129 }
130 else
131 {
132 getLog().info( "Deferring install for " + project.getGroupId()
133 + ":" + project.getArtifactId() + ":" + project.getVersion() + " at end" );
134 putState( State.TO_BE_INSTALLED );
135 }
136 }
137
138 List<MavenProject> allProjectsUsingPlugin = getAllProjectsUsingPlugin();
139
140 if ( allProjectsMarked( allProjectsUsingPlugin ) )
141 {
142 InstallRequest request = new InstallRequest();
143 for ( MavenProject reactorProject : allProjectsUsingPlugin )
144 {
145 State state = getState( reactorProject );
146 if ( state == State.TO_BE_INSTALLED )
147 {
148 processProject( reactorProject, request );
149 }
150 }
151 installProject( request );
152 }
153 }
154
155 private boolean allProjectsMarked( List<MavenProject> allProjectsUsingPlugin )
156 {
157 for ( MavenProject reactorProject : allProjectsUsingPlugin )
158 {
159 if ( !hasState( reactorProject ) )
160 {
161 return false;
162 }
163 }
164 return true;
165 }
166
167 private List<MavenProject> getAllProjectsUsingPlugin()
168 {
169 ArrayList<MavenProject> result = new ArrayList<>();
170 for ( MavenProject reactorProject : reactorProjects )
171 {
172 if ( hasExecution( reactorProject.getPlugin( "org.apache.maven.plugins:maven-install-plugin" ) ) )
173 {
174 result.add( reactorProject );
175 }
176 }
177 return result;
178 }
179
180 private boolean hasExecution( Plugin plugin )
181 {
182 if ( plugin == null )
183 {
184 return false;
185 }
186
187 for ( PluginExecution execution : plugin.getExecutions() )
188 {
189 if ( !execution.getGoals().isEmpty() && !"none".equalsIgnoreCase( execution.getPhase() ) )
190 {
191 return true;
192 }
193 }
194 return false;
195 }
196
197 private void installProject( InstallRequest request ) throws MojoExecutionException
198 {
199 try
200 {
201 repositorySystem.install( session.getRepositorySession(), request );
202 }
203 catch ( InstallationException e )
204 {
205 throw new MojoExecutionException( e.getMessage(), e );
206 }
207 }
208
209
210
211
212
213
214 private void processProject( MavenProject project, InstallRequest request ) throws MojoExecutionException
215 {
216 if ( isFile( project.getFile() ) )
217 {
218 request.addArtifact( RepositoryUtils.toArtifact( new ProjectArtifact( project ) ) );
219 }
220 else
221 {
222 throw new MojoExecutionException( "The project POM could not be attached" );
223 }
224
225 if ( !"pom".equals( project.getPackaging() ) )
226 {
227 org.apache.maven.artifact.Artifact mavenMainArtifact = project.getArtifact();
228 if ( isFile( mavenMainArtifact.getFile() ) )
229 {
230 request.addArtifact( RepositoryUtils.toArtifact( mavenMainArtifact ) );
231 }
232 else if ( !project.getAttachedArtifacts().isEmpty() )
233 {
234 throw new MojoExecutionException( "The packaging plugin for this project did not assign "
235 + "a main file to the project but it has attachments. Change packaging to 'pom'." );
236 }
237 else
238 {
239 throw new MojoExecutionException( "The packaging for this project did not assign "
240 + "a file to the build artifact" );
241 }
242 }
243
244
245 for ( org.apache.maven.artifact.Artifact attached : project.getAttachedArtifacts() )
246 {
247 getLog().debug( "Attaching for install: " + attached.getId() );
248 request.addArtifact( RepositoryUtils.toArtifact( attached ) );
249 }
250 }
251
252 private boolean isFile( File file )
253 {
254 return file != null && file.isFile();
255 }
256 }