1 package org.apache.maven.archetype.mojos;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.commons.lang3.StringUtils;
23 import org.apache.maven.archetype.ArchetypeGenerationRequest;
24 import org.apache.maven.archetype.ArchetypeGenerationResult;
25 import org.apache.maven.archetype.ArchetypeManager;
26 import org.apache.maven.archetype.ui.generation.ArchetypeGenerationConfigurator;
27 import org.apache.maven.archetype.ui.generation.ArchetypeSelector;
28 import org.apache.maven.artifact.repository.ArtifactRepository;
29 import org.apache.maven.execution.MavenSession;
30 import org.apache.maven.plugin.AbstractMojo;
31 import org.apache.maven.plugin.ContextEnabled;
32 import org.apache.maven.plugin.MojoExecutionException;
33 import org.apache.maven.plugin.MojoFailureException;
34 import org.apache.maven.plugins.annotations.Component;
35 import org.apache.maven.plugins.annotations.Execute;
36 import org.apache.maven.plugins.annotations.LifecyclePhase;
37 import org.apache.maven.plugins.annotations.Mojo;
38 import org.apache.maven.plugins.annotations.Parameter;
39 import org.apache.maven.shared.invoker.DefaultInvocationRequest;
40 import org.apache.maven.shared.invoker.InvocationRequest;
41 import org.apache.maven.shared.invoker.InvocationResult;
42 import org.apache.maven.shared.invoker.Invoker;
43 import org.apache.maven.shared.invoker.MavenInvocationException;
44
45 import java.io.File;
46 import java.util.Arrays;
47 import java.util.List;
48 import java.util.Properties;
49
50
51
52
53
54
55
56
57 @Mojo( name = "generate", requiresProject = false )
58 @Execute( phase = LifecyclePhase.GENERATE_SOURCES )
59 public class CreateProjectFromArchetypeMojo
60 extends AbstractMojo
61 implements ContextEnabled
62 {
63 @Component
64 private ArchetypeManager manager;
65
66 @Component
67 private ArchetypeSelector selector;
68
69 @Component
70 private ArchetypeGenerationConfigurator configurator;
71
72 @Component
73 private Invoker invoker;
74
75
76
77
78 @Parameter( property = "archetypeArtifactId" )
79 private String archetypeArtifactId;
80
81
82
83
84 @Parameter( property = "archetypeGroupId" )
85 private String archetypeGroupId;
86
87
88
89
90 @Parameter( property = "archetypeVersion" )
91 private String archetypeVersion;
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120 @Parameter( property = "archetypeCatalog", defaultValue = "remote,local" )
121 private String archetypeCatalog;
122
123
124
125
126
127 @Parameter( property = "askForDefaultPropertyValues", defaultValue = "false", required = true )
128 private Boolean askForDefaultPropertyValues;
129
130
131
132
133 @Parameter( defaultValue = "${localRepository}", readonly = true, required = true )
134 private ArtifactRepository localRepository;
135
136
137
138
139 @Parameter( defaultValue = "${project.remoteArtifactRepositories}", readonly = true, required = true )
140 private List<ArtifactRepository> remoteArtifactRepositories;
141
142
143
144
145 @Parameter( property = "interactiveMode", defaultValue = "${settings.interactiveMode}", required = true )
146 private Boolean interactiveMode;
147
148 @Parameter( defaultValue = "${basedir}", property = "outputDirectory" )
149 private File outputDirectory;
150
151 @Parameter( defaultValue = "${session}", readonly = true )
152 private MavenSession session;
153
154
155
156
157 @Parameter( property = "goals" )
158 private String goals;
159
160
161
162
163
164
165
166
167
168
169
170 @Parameter( property = "filter" )
171 private String filter;
172
173 @Override
174 public void execute()
175 throws MojoExecutionException, MojoFailureException
176 {
177 Properties executionProperties = session.getUserProperties();
178
179 ArchetypeGenerationRequest request =
180 new ArchetypeGenerationRequest().setArchetypeGroupId( archetypeGroupId )
181 .setArchetypeArtifactId( archetypeArtifactId )
182 .setArchetypeVersion( archetypeVersion )
183 .setOutputDirectory( outputDirectory.getAbsolutePath() )
184 .setLocalRepository( localRepository )
185 .setRemoteArtifactRepositories( remoteArtifactRepositories )
186 .setFilter( filter )
187 .setAskForDefaultPropertyValues( askForDefaultPropertyValues )
188 .setProjectBuildingRequest( session.getProjectBuildingRequest() );
189
190 try
191 {
192 if ( interactiveMode.booleanValue() )
193 {
194 getLog().info( "Generating project in Interactive mode" );
195 }
196 else
197 {
198 getLog().info( "Generating project in Batch mode" );
199 }
200
201 selector.selectArchetype( request, interactiveMode, archetypeCatalog );
202
203 if ( StringUtils.isBlank( request.getArchetypeArtifactId() ) )
204 {
205
206 return;
207 }
208
209 configurator.configureArchetype( request, interactiveMode, executionProperties );
210
211 ArchetypeGenerationResult generationResult = manager.generateProjectFromArchetype( request );
212
213 if ( generationResult.getCause() != null )
214 {
215 throw new MojoFailureException( generationResult.getCause(), generationResult.getCause().getMessage(),
216 generationResult.getCause().getMessage() );
217 }
218 }
219 catch ( MojoFailureException ex )
220 {
221 throw ex;
222 }
223 catch ( Exception ex )
224 {
225 throw (MojoFailureException) new MojoFailureException( ex.getMessage() ).initCause( ex );
226 }
227
228 String artifactId = request.getArtifactId();
229
230 String postArchetypeGenerationGoals = request.getArchetypeGoals();
231
232 if ( StringUtils.isEmpty( postArchetypeGenerationGoals ) )
233 {
234 postArchetypeGenerationGoals = goals;
235 }
236
237 if ( StringUtils.isNotEmpty( postArchetypeGenerationGoals ) )
238 {
239 invokePostArchetypeGenerationGoals( postArchetypeGenerationGoals, artifactId );
240 }
241 }
242
243 private void invokePostArchetypeGenerationGoals( String goals, String artifactId )
244 throws MojoExecutionException, MojoFailureException
245 {
246 getLog().info( "Invoking post-archetype-generation goals: " + goals );
247
248 File projectBasedir = new File( outputDirectory, artifactId );
249
250 if ( projectBasedir.exists() )
251 {
252 InvocationRequest request = new DefaultInvocationRequest().setBaseDirectory( projectBasedir ).setGoals(
253 Arrays.asList( StringUtils.split( goals, "," ) ) );
254
255 try
256 {
257 InvocationResult result = invoker.execute( request );
258
259 if ( result.getExitCode() != 0 )
260 {
261 throw new MojoExecutionException( "Failed to invoke goals",
262 result.getExecutionException() );
263 }
264 }
265 catch ( MavenInvocationException e )
266 {
267 throw new MojoExecutionException( "Cannot run additions goals.", e );
268 }
269 }
270 else
271 {
272 getLog().info( "Post-archetype-generation goals aborted: unavailable basedir " + projectBasedir );
273 }
274 }
275 }