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