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 | 0 | 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 | 0 | Properties executionProperties = session.getExecutionProperties(); |
159 | |
|
160 | 0 | 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 | 0 | if( interactiveMode.booleanValue() ) |
172 | |
{ |
173 | 0 | getLog().info( "Generating project in Interactive mode" ); |
174 | |
} |
175 | |
else |
176 | |
{ |
177 | 0 | getLog().info( "Generating project in Batch mode" ); |
178 | |
} |
179 | |
|
180 | 0 | selector.selectArchetype( request, interactiveMode, archetypeCatalog ); |
181 | |
|
182 | 0 | configurator.configureArchetype( request, interactiveMode, executionProperties ); |
183 | |
|
184 | 0 | ArchetypeGenerationResult generationResult = |
185 | |
archetype.generateProjectFromArchetype( request ); |
186 | 0 | if( generationResult.getCause() != null ) |
187 | |
{ |
188 | 0 | throw new MojoFailureException( |
189 | |
generationResult.getCause(), |
190 | |
generationResult.getCause().getMessage(), |
191 | |
generationResult.getCause().getMessage() |
192 | |
); |
193 | |
} |
194 | |
} |
195 | 0 | catch ( MojoFailureException ex ) |
196 | |
{ |
197 | 0 | throw ex; |
198 | |
} |
199 | 0 | catch ( Exception ex ) |
200 | |
{ |
201 | 0 | throw new MojoFailureException( ex.getMessage() ); |
202 | 0 | } |
203 | |
|
204 | 0 | String artifactId = request.getArtifactId(); |
205 | |
|
206 | 0 | String postArchetypeGenerationGoals = request.getArchetypeGoals(); |
207 | |
|
208 | 0 | if ( StringUtils.isEmpty( postArchetypeGenerationGoals ) ) |
209 | |
{ |
210 | 0 | postArchetypeGenerationGoals = goals; |
211 | |
} |
212 | |
|
213 | 0 | if ( StringUtils.isNotEmpty( postArchetypeGenerationGoals ) ) |
214 | |
{ |
215 | 0 | invokePostArchetypeGenerationGoals( postArchetypeGenerationGoals, artifactId ); |
216 | |
} |
217 | 0 | } |
218 | |
|
219 | |
private void invokePostArchetypeGenerationGoals( String goals, String artifactId ) |
220 | |
throws MojoExecutionException, MojoFailureException |
221 | |
{ |
222 | 0 | File projectBasedir = new File( basedir, artifactId ); |
223 | |
|
224 | 0 | if ( projectBasedir.exists() ) |
225 | |
{ |
226 | 0 | InvocationRequest request = new DefaultInvocationRequest() |
227 | |
.setBaseDirectory( projectBasedir ) |
228 | |
.setGoals( Arrays.asList( StringUtils.split( goals, "," ) ) ); |
229 | |
|
230 | |
try |
231 | |
{ |
232 | 0 | invoker.execute( request ); |
233 | |
} |
234 | 0 | catch ( MavenInvocationException e ) |
235 | |
{ |
236 | 0 | throw new MojoExecutionException( "Cannot run additions goals." ); |
237 | 0 | } |
238 | |
} |
239 | 0 | } |
240 | |
} |