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.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 @Parameter( defaultValue = "${localRepository}", readonly = true, required = true )
127 private ArtifactRepository localRepository;
128
129
130
131
132 @Parameter( defaultValue = "${project.remoteArtifactRepositories}", readonly = true, required = true )
133 private List<ArtifactRepository> remoteArtifactRepositories;
134
135
136
137
138 @Parameter( property = "interactiveMode", defaultValue = "${settings.interactiveMode}", required = true )
139 private Boolean interactiveMode;
140
141 @Parameter( defaultValue = "${basedir}" )
142 private File basedir;
143
144 @Parameter( defaultValue = "${session}", readonly = true )
145 private MavenSession session;
146
147
148
149
150 @Parameter( property = "goals" )
151 private String goals;
152
153
154
155
156
157
158
159
160
161
162
163 @Parameter( property = "filter" )
164 private String filter;
165
166 public void execute()
167 throws MojoExecutionException, MojoFailureException
168 {
169 Properties executionProperties = session.getUserProperties();
170
171 ArchetypeGenerationRequest request =
172 new ArchetypeGenerationRequest().setArchetypeGroupId( archetypeGroupId )
173 .setArchetypeArtifactId( archetypeArtifactId )
174 .setArchetypeVersion( archetypeVersion )
175 .setOutputDirectory( basedir.getAbsolutePath() )
176 .setLocalRepository( localRepository )
177 .setRemoteArtifactRepositories( remoteArtifactRepositories )
178 .setFilter( filter )
179 .setProjectBuildingRequest( session.getProjectBuildingRequest() );
180
181 try
182 {
183 if ( interactiveMode.booleanValue() )
184 {
185 getLog().info( "Generating project in Interactive mode" );
186 }
187 else
188 {
189 getLog().info( "Generating project in Batch mode" );
190 }
191
192 selector.selectArchetype( request, interactiveMode, archetypeCatalog );
193
194 if ( StringUtils.isBlank( request.getArchetypeArtifactId() ) )
195 {
196
197 return;
198 }
199
200 configurator.configureArchetype( request, interactiveMode, executionProperties );
201
202 ArchetypeGenerationResult generationResult = manager.generateProjectFromArchetype( request );
203
204 if ( generationResult.getCause() != null )
205 {
206 throw new MojoFailureException( generationResult.getCause(), generationResult.getCause().getMessage(),
207 generationResult.getCause().getMessage() );
208 }
209 }
210 catch ( MojoFailureException ex )
211 {
212 throw ex;
213 }
214 catch ( Exception ex )
215 {
216 throw (MojoFailureException) new MojoFailureException( ex.getMessage() ).initCause( ex );
217 }
218
219 String artifactId = request.getArtifactId();
220
221 String postArchetypeGenerationGoals = request.getArchetypeGoals();
222
223 if ( StringUtils.isEmpty( postArchetypeGenerationGoals ) )
224 {
225 postArchetypeGenerationGoals = goals;
226 }
227
228 if ( StringUtils.isNotEmpty( postArchetypeGenerationGoals ) )
229 {
230 invokePostArchetypeGenerationGoals( postArchetypeGenerationGoals, artifactId );
231 }
232 }
233
234 private void invokePostArchetypeGenerationGoals( String goals, String artifactId )
235 throws MojoExecutionException, MojoFailureException
236 {
237 getLog().info( "Invoking post-archetype-generation goals: " + goals );
238
239 File projectBasedir = new File( basedir, artifactId );
240
241 if ( projectBasedir.exists() )
242 {
243 InvocationRequest request = new DefaultInvocationRequest().setBaseDirectory( projectBasedir ).setGoals(
244 Arrays.asList( StringUtils.split( goals, "," ) ) );
245
246 try
247 {
248 InvocationResult result = invoker.execute( request );
249
250 if ( result.getExitCode() != 0 )
251 {
252 throw new MojoExecutionException( "Failed to invoke goals",
253 result.getExecutionException() );
254 }
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 }