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.maven.archetype.ArchetypeCreationRequest; |
23 | |
import org.apache.maven.archetype.ArchetypeCreationResult; |
24 | |
import org.apache.maven.archetype.ArchetypeManager; |
25 | |
import org.apache.maven.archetype.common.Constants; |
26 | |
import org.apache.maven.archetype.ui.creation.ArchetypeCreationConfigurator; |
27 | |
import org.apache.maven.artifact.repository.ArtifactRepository; |
28 | |
import org.apache.maven.execution.MavenSession; |
29 | |
import org.apache.maven.plugin.AbstractMojo; |
30 | |
import org.apache.maven.plugin.MojoExecutionException; |
31 | |
import org.apache.maven.plugin.MojoFailureException; |
32 | |
import org.apache.maven.project.MavenProject; |
33 | |
import org.codehaus.plexus.util.PropertyUtils; |
34 | |
import org.codehaus.plexus.util.StringUtils; |
35 | |
|
36 | |
import java.io.File; |
37 | |
import java.util.ArrayList; |
38 | |
import java.util.Arrays; |
39 | |
import java.util.List; |
40 | |
import java.util.Properties; |
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | 0 | public class CreateArchetypeFromProjectMojo |
52 | |
extends AbstractMojo |
53 | |
{ |
54 | |
|
55 | |
private ArchetypeCreationConfigurator configurator; |
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
private boolean interactive; |
63 | |
|
64 | |
|
65 | |
private ArchetypeManager manager; |
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
private String archetypeFilteredExtentions; |
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
private String archetypeLanguages; |
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
private File archetypeRegistryFile; |
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
private String defaultEncoding; |
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | 0 | private boolean partialArchetype = false; |
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | 0 | private boolean preserveCData = false; |
109 | |
|
110 | |
|
111 | |
private ArtifactRepository localRepository; |
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | 0 | private boolean keepParent = true; |
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
private MavenProject project; |
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
private File propertyFile; |
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | |
|
141 | |
|
142 | |
|
143 | |
private String archetypePostPhase; |
144 | |
|
145 | |
|
146 | |
|
147 | |
|
148 | |
|
149 | |
|
150 | |
private File outputDirectory; |
151 | |
|
152 | |
|
153 | |
private boolean testMode; |
154 | |
|
155 | |
|
156 | |
private String packageName; |
157 | |
|
158 | |
|
159 | |
|
160 | |
|
161 | |
|
162 | |
private MavenSession session; |
163 | |
|
164 | |
public void execute() |
165 | |
throws MojoExecutionException, MojoFailureException |
166 | |
{ |
167 | 0 | Properties executionProperties = session.getExecutionProperties(); |
168 | |
try |
169 | |
{ |
170 | 0 | if ( propertyFile != null ) |
171 | |
{ |
172 | 0 | propertyFile.getParentFile().mkdirs(); |
173 | |
} |
174 | |
|
175 | 0 | List<String> languages = getLanguages( archetypeLanguages, propertyFile ); |
176 | |
|
177 | 0 | Properties properties = |
178 | |
configurator.configureArchetypeCreation( project, Boolean.valueOf( interactive ), executionProperties, |
179 | |
propertyFile, languages ); |
180 | |
|
181 | 0 | List<String> filtereds = getFilteredExtensions( archetypeFilteredExtentions, propertyFile ); |
182 | |
|
183 | 0 | ArchetypeCreationRequest request = new ArchetypeCreationRequest() |
184 | |
.setProject( project ) |
185 | |
|
186 | |
.setProperties( properties ) |
187 | |
.setLanguages( languages ) |
188 | |
|
189 | |
.setFiltereds( filtereds ) |
190 | |
|
191 | |
.setPreserveCData( preserveCData ) |
192 | |
.setKeepParent( keepParent ) |
193 | |
.setPartialArchetype( partialArchetype ) |
194 | |
|
195 | |
.setArchetypeRegistryFile( archetypeRegistryFile ) |
196 | |
.setLocalRepository( localRepository ) |
197 | |
|
198 | |
.setPackageName( packageName ) |
199 | |
.setPostPhase( archetypePostPhase ) |
200 | |
.setOutputDirectory( outputDirectory ); |
201 | |
|
202 | 0 | ArchetypeCreationResult result = manager.createArchetypeFromProject( request ); |
203 | |
|
204 | 0 | if ( result.getCause() != null ) |
205 | |
{ |
206 | 0 | throw new MojoFailureException( result.getCause(), result.getCause().getMessage(), |
207 | |
result.getCause().getMessage() ); |
208 | |
} |
209 | |
|
210 | 0 | getLog().info( "Archetype created in " + outputDirectory ); |
211 | |
|
212 | 0 | if ( testMode ) |
213 | |
{ |
214 | |
|
215 | |
|
216 | |
|
217 | |
|
218 | |
|
219 | |
|
220 | |
|
221 | |
|
222 | |
|
223 | |
} |
224 | |
|
225 | |
} |
226 | 0 | catch ( MojoFailureException ex ) |
227 | |
{ |
228 | 0 | throw ex; |
229 | |
} |
230 | 0 | catch ( Exception ex ) |
231 | |
{ |
232 | 0 | throw new MojoFailureException( ex, ex.getMessage(), ex.getMessage() ); |
233 | 0 | } |
234 | 0 | } |
235 | |
|
236 | |
private List<String> getFilteredExtensions( String archetypeFilteredExtentions, File propertyFile ) |
237 | |
{ |
238 | 0 | List<String> filteredExtensions = new ArrayList<String>(); |
239 | |
|
240 | 0 | if ( StringUtils.isNotEmpty( archetypeFilteredExtentions ) ) |
241 | |
{ |
242 | 0 | filteredExtensions.addAll( Arrays.asList( StringUtils.split( archetypeFilteredExtentions, "," ) ) ); |
243 | |
|
244 | 0 | getLog().debug( "Found in command line extensions = " + filteredExtensions ); |
245 | |
} |
246 | |
|
247 | 0 | if ( filteredExtensions.isEmpty() && propertyFile != null && propertyFile.exists() ) |
248 | |
{ |
249 | 0 | Properties properties = PropertyUtils.loadProperties( propertyFile ); |
250 | |
|
251 | 0 | String extensions = properties.getProperty( Constants.ARCHETYPE_FILTERED_EXTENSIONS ); |
252 | 0 | if ( StringUtils.isNotEmpty( extensions ) ) |
253 | |
{ |
254 | 0 | filteredExtensions.addAll( Arrays.asList( StringUtils.split( extensions, "," ) ) ); |
255 | |
} |
256 | |
|
257 | 0 | getLog().debug( "Found in propertyFile " + propertyFile.getName() + " extensions = " + filteredExtensions ); |
258 | |
} |
259 | |
|
260 | 0 | if ( filteredExtensions.isEmpty() ) |
261 | |
{ |
262 | 0 | filteredExtensions.addAll( Constants.DEFAULT_FILTERED_EXTENSIONS ); |
263 | |
|
264 | 0 | getLog().debug( "Using default extensions = " + filteredExtensions ); |
265 | |
} |
266 | |
|
267 | 0 | return filteredExtensions; |
268 | |
} |
269 | |
|
270 | |
private List<String> getLanguages( String archetypeLanguages, File propertyFile ) |
271 | |
{ |
272 | 0 | List<String> resultingLanguages = new ArrayList<String>(); |
273 | |
|
274 | 0 | if ( StringUtils.isNotEmpty( archetypeLanguages ) ) |
275 | |
{ |
276 | 0 | resultingLanguages.addAll( Arrays.asList( StringUtils.split( archetypeLanguages, "," ) ) ); |
277 | |
|
278 | 0 | getLog().debug( "Found in command line languages = " + resultingLanguages ); |
279 | |
} |
280 | |
|
281 | 0 | if ( resultingLanguages.isEmpty() && propertyFile != null && propertyFile.exists() ) |
282 | |
{ |
283 | 0 | Properties properties = PropertyUtils.loadProperties( propertyFile ); |
284 | |
|
285 | 0 | String languages = properties.getProperty( Constants.ARCHETYPE_LANGUAGES ); |
286 | 0 | if ( StringUtils.isNotEmpty( languages ) ) |
287 | |
{ |
288 | 0 | resultingLanguages.addAll( Arrays.asList( StringUtils.split( languages, "," ) ) ); |
289 | |
} |
290 | |
|
291 | 0 | getLog().debug( "Found in propertyFile " + propertyFile.getName() + " languages = " + resultingLanguages ); |
292 | |
} |
293 | |
|
294 | 0 | if ( resultingLanguages.isEmpty() ) |
295 | |
{ |
296 | 0 | resultingLanguages.addAll( Constants.DEFAULT_LANGUAGES ); |
297 | |
|
298 | 0 | getLog().debug( "Using default languages = " + resultingLanguages ); |
299 | |
} |
300 | |
|
301 | 0 | return resultingLanguages; |
302 | |
} |
303 | |
} |