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.lang.StringUtils;
23 import org.apache.maven.archetype.ArchetypeManager;
24 import org.apache.maven.archetype.ArchetypeGenerationRequest;
25 import org.apache.maven.archetype.ArchetypeGenerationResult;
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.shared.invoker.DefaultInvocationRequest;
35 import org.apache.maven.shared.invoker.InvocationRequest;
36 import org.apache.maven.shared.invoker.Invoker;
37 import org.apache.maven.shared.invoker.MavenInvocationException;
38
39 import java.io.File;
40 import java.util.Arrays;
41 import java.util.List;
42 import java.util.Properties;
43
44
45
46
47
48
49
50
51
52
53
54 public class CreateProjectFromArchetypeMojo
55 extends AbstractMojo
56 implements ContextEnabled
57 {
58
59 private ArchetypeManager manager;
60
61
62 private ArchetypeSelector selector;
63
64
65 private ArchetypeGenerationConfigurator configurator;
66
67
68 private Invoker invoker;
69
70
71
72
73
74
75 private String archetypeArtifactId;
76
77
78
79
80
81
82 private String archetypeGroupId;
83
84
85
86
87
88
89 private String archetypeVersion;
90
91
92
93
94
95
96 private String archetypeRepository;
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115 private String archetypeCatalog;
116
117
118
119
120
121
122
123
124 private ArtifactRepository localRepository;
125
126
127
128
129
130
131
132
133 private List<ArtifactRepository> remoteArtifactRepositories;
134
135
136
137
138
139
140
141 private Boolean interactiveMode;
142
143
144 private File basedir;
145
146
147
148
149
150 private MavenSession session;
151
152
153
154
155
156
157 private String goals;
158
159
160
161
162
163
164
165
166
167
168
169 private String filter;
170
171 public void execute()
172 throws MojoExecutionException, MojoFailureException
173 {
174 Properties executionProperties = session.getExecutionProperties();
175
176 ArchetypeGenerationRequest request = new ArchetypeGenerationRequest()
177 .setArchetypeGroupId( archetypeGroupId )
178 .setArchetypeArtifactId( archetypeArtifactId )
179 .setArchetypeVersion( archetypeVersion )
180 .setOutputDirectory( basedir.getAbsolutePath() )
181 .setLocalRepository( localRepository )
182 .setArchetypeRepository( archetypeRepository )
183 .setRemoteArtifactRepositories( remoteArtifactRepositories )
184 .setFilter( filter );
185
186 try
187 {
188 if ( interactiveMode.booleanValue() )
189 {
190 getLog().info( "Generating project in Interactive mode" );
191 }
192 else
193 {
194 getLog().info( "Generating project in Batch mode" );
195 }
196
197 selector.selectArchetype( request, interactiveMode, archetypeCatalog );
198
199 if ( StringUtils.isBlank( request.getArchetypeArtifactId() ) )
200 {
201
202 return;
203 }
204
205 configurator.configureArchetype( request, interactiveMode, executionProperties );
206
207 ArchetypeGenerationResult generationResult = manager.generateProjectFromArchetype( request );
208
209 if ( generationResult.getCause() != null )
210 {
211 throw new MojoFailureException( generationResult.getCause(), generationResult.getCause().getMessage(),
212 generationResult.getCause().getMessage() );
213 }
214 }
215 catch ( MojoFailureException ex )
216 {
217 throw ex;
218 }
219 catch ( Exception ex )
220 {
221 throw (MojoFailureException) new MojoFailureException( ex.getMessage() ).initCause( ex );
222 }
223
224 String artifactId = request.getArtifactId();
225
226 String postArchetypeGenerationGoals = request.getArchetypeGoals();
227
228 if ( StringUtils.isEmpty( postArchetypeGenerationGoals ) )
229 {
230 postArchetypeGenerationGoals = goals;
231 }
232
233 if ( StringUtils.isNotEmpty( postArchetypeGenerationGoals ) )
234 {
235 invokePostArchetypeGenerationGoals( postArchetypeGenerationGoals, artifactId );
236 }
237 }
238
239 private void invokePostArchetypeGenerationGoals( String goals, String artifactId )
240 throws MojoExecutionException, MojoFailureException
241 {
242 getLog().info( "Invoking post-archetype-generation goals: " + goals );
243
244 File projectBasedir = new File( basedir, artifactId );
245
246 if ( projectBasedir.exists() )
247 {
248 InvocationRequest request = new DefaultInvocationRequest()
249 .setBaseDirectory( projectBasedir )
250 .setGoals( Arrays.asList( StringUtils.split( goals, "," ) ) );
251
252 try
253 {
254 invoker.execute( request );
255 }
256 catch ( MavenInvocationException e )
257 {
258 throw new MojoExecutionException( "Cannot run additions goals.", e );
259 }
260 }
261 else
262 {
263 getLog().info( "Post-archetype-generation goals aborted: unavailable basedir " + projectBasedir );
264 }
265 }
266 }