1818import static org .twdata .maven .mojoexecutor .MojoExecutor .plugin ;
1919import static org .twdata .maven .mojoexecutor .MojoExecutor .version ;
2020
21- import com .google .common .base .CaseFormat ;
22- import com .mitchellbosecke .pebble .PebbleEngine ;
23- import com .mitchellbosecke .pebble .error .PebbleException ;
24- import com .mitchellbosecke .pebble .loader .FileLoader ;
25- import com .mitchellbosecke .pebble .loader .StringLoader ;
26- import com .mitchellbosecke .pebble .template .PebbleTemplate ;
2721import java .io .File ;
2822import java .io .FileOutputStream ;
2923import java .io .IOException ;
4034import java .util .List ;
4135import java .util .Map ;
4236import java .util .Set ;
37+
4338import org .apache .commons .lang .StringUtils ;
4439import org .apache .maven .archetype .ArchetypeManager ;
4540import org .apache .maven .archetype .catalog .Archetype ;
5853import org .seedstack .maven .components .resolver .ArtifactResolver ;
5954import org .seedstack .maven .components .templating .SeedStackExtension ;
6055
56+ import com .google .common .base .CaseFormat ;
57+ import com .mitchellbosecke .pebble .PebbleEngine ;
58+ import com .mitchellbosecke .pebble .error .PebbleException ;
59+ import com .mitchellbosecke .pebble .loader .FileLoader ;
60+ import com .mitchellbosecke .pebble .loader .StringLoader ;
61+ import com .mitchellbosecke .pebble .template .PebbleTemplate ;
62+
6163/**
6264 * Defines the generate goal. This goal generates a SeedStack project from existing archetypes.
6365 *
@@ -68,6 +70,7 @@ public class GenerateMojo extends AbstractSeedStackMojo {
6870 private static final String ARCHETYPE_PLUGIN_GROUP_ID = "org.apache.maven.plugins" ;
6971 private static final String ARCHETYPE_PLUGIN_ARTIFACT_ID = "maven-archetype-plugin" ;
7072 private static final String SEEDSTACK_ORG = "http://seedstack.org/maven/" ;
73+ private static final String CENTRAL_REMOTE_URL = "https://repo.maven.apache.org/maven2" ;
7174 private PebbleEngine stringTemplateEngine ;
7275 private PebbleEngine fileTemplateEngine ;
7376 @ Component
@@ -94,23 +97,18 @@ public void execute() throws MojoExecutionException, MojoFailureException {
9497 this .basicMode = !mavenSession .getUserProperties ().getProperty ("basicPrompt" , "false" ).equals ("false" );
9598 }
9699 if (!basicMode ) {
97- getLog ().info (
98- "If enhanced prompt has issues on your system, try basic prompt by adding \" -DbasicPrompt\" "
99- + " to your command line" );
100+ getLog ().info ("If enhanced prompt has issues on your system, try basic prompt by adding \" -DbasicPrompt\" " + " to your command line" );
100101 }
101102
102103 String type = mavenSession .getUserProperties ().getProperty ("type" ),
103- distributionGroupId = mavenSession .getUserProperties ()
104- .getProperty ("distributionGroupId" , "org.seedstack" ),
105- distributionArtifactId = mavenSession .getUserProperties ()
106- .getProperty ("distributionArtifactId" , "distribution" ),
104+ distributionGroupId = mavenSession .getUserProperties ().getProperty ("distributionGroupId" , "org.seedstack" ),
105+ distributionArtifactId = mavenSession .getUserProperties ().getProperty ("distributionArtifactId" , "distribution" ),
107106 version = mavenSession .getUserProperties ().getProperty ("version" ),
108107 archetypeGroupId = mavenSession .getUserProperties ().getProperty ("archetypeGroupId" ),
109108 archetypeArtifactId = mavenSession .getUserProperties ().getProperty ("archetypeArtifactId" ),
110109 archetypeVersion = mavenSession .getUserProperties ().getProperty ("archetypeVersion" ),
111110 remoteCatalog = mavenSession .getUserProperties ().getProperty ("remoteCatalog" , SEEDSTACK_ORG );
112- boolean allowSnapshots = !mavenSession .getUserProperties ().getProperty ("allowSnapshots" , "false" )
113- .equals ("false" );
111+ boolean allowSnapshots = !mavenSession .getUserProperties ().getProperty ("allowSnapshots" , "false" ).equals ("false" );
114112
115113 if (StringUtils .isBlank (archetypeGroupId )) {
116114 archetypeGroupId = distributionGroupId ;
@@ -120,19 +118,14 @@ public void execute() throws MojoExecutionException, MojoFailureException {
120118 if (StringUtils .isBlank (type )) {
121119 // Resolve archetype version using SeedStack highest version
122120 if (StringUtils .isBlank (archetypeVersion )) {
123- getLog ().info (
124- "Resolving latest " + (allowSnapshots ? "snapshot" : "release" ) + " of SeedStack (" +
125- distributionGroupId + ")" );
126- archetypeVersion = artifactResolver
127- .getHighestVersion (mavenProject , distributionGroupId , distributionArtifactId ,
128- allowSnapshots );
121+ getLog ().info ("Resolving latest " + (allowSnapshots ? "snapshot" : "release" ) + " of SeedStack (" + distributionGroupId + ")" );
122+ archetypeVersion = artifactResolver .getHighestVersion (mavenProject , distributionGroupId , distributionArtifactId , allowSnapshots );
129123 getLog ().info ("Resolved version " + archetypeVersion );
130124 }
131125 try {
132126 // We have a list of possible types, let the user choose (if a "web" choice exists, set it as
133127 // default)
134- List <Value > list = new ArrayList <>(
135- findProjectTypes (archetypeGroupId , archetypeVersion , remoteCatalog ));
128+ List <Value > list = new ArrayList <>(findProjectTypes (archetypeGroupId , archetypeVersion , remoteCatalog ));
136129 Collections .sort (list );
137130 list .add (new Value ("custom archetype" , "custom" ));
138131 type = getPrompter ().promptList ("Choose the project type" , list , "web" );
@@ -147,9 +140,8 @@ public void execute() throws MojoExecutionException, MojoFailureException {
147140 }
148141 // Ask for archetype version (defaults to latest)
149142 try {
150- archetypeVersion = artifactResolver
151- .getHighestVersion (mavenProject , archetypeGroupId , archetypeArtifactId ,
152- allowSnapshots );
143+ archetypeVersion = artifactResolver .getHighestVersion (mavenProject , archetypeGroupId , archetypeArtifactId ,
144+ allowSnapshots );
153145 } catch (Exception e ) {
154146 archetypeVersion = null ;
155147 }
@@ -167,11 +159,9 @@ public void execute() throws MojoExecutionException, MojoFailureException {
167159
168160 // If needed, find the latest version of the archetype
169161 if (StringUtils .isBlank (archetypeVersion )) {
170- getLog ().info (
171- "Resolving latest " + (allowSnapshots ? "snapshot" : "release" ) + " of archetype " +
172- archetypeGroupId + ":" + archetypeArtifactId );
173- archetypeVersion = artifactResolver
174- .getHighestVersion (mavenProject , archetypeGroupId , archetypeArtifactId , allowSnapshots );
162+ getLog ().info ("Resolving latest " + (allowSnapshots ? "snapshot" : "release" ) + " of archetype " + archetypeGroupId + ":"
163+ + archetypeArtifactId );
164+ archetypeVersion = artifactResolver .getHighestVersion (mavenProject , archetypeGroupId , archetypeArtifactId , allowSnapshots );
175165 getLog ().info ("Resolved version " + archetypeVersion );
176166 }
177167
@@ -206,25 +196,19 @@ public void execute() throws MojoExecutionException, MojoFailureException {
206196
207197 String pluginVersion ;
208198 try {
209- pluginVersion = artifactResolver
210- .getHighestVersion (mavenProject , ARCHETYPE_PLUGIN_GROUP_ID , ARCHETYPE_PLUGIN_ARTIFACT_ID , false );
199+ pluginVersion = artifactResolver .getHighestVersion (mavenProject , ARCHETYPE_PLUGIN_GROUP_ID , ARCHETYPE_PLUGIN_ARTIFACT_ID , false );
211200 getLog ().info ("Using the latest version of archetype plugin: " + pluginVersion );
212201 } catch (Exception e ) {
213202 getLog ().warn ("Unable to determine latest version of archetype plugin, falling back to 3.0.0" );
214203 pluginVersion = "3.0.0" ;
215204 }
216205
217- executeMojo (plugin (groupId (ARCHETYPE_PLUGIN_GROUP_ID ), artifactId (ARCHETYPE_PLUGIN_ARTIFACT_ID ),
218- version (pluginVersion )),
206+ executeMojo (plugin (groupId (ARCHETYPE_PLUGIN_GROUP_ID ), artifactId (ARCHETYPE_PLUGIN_ARTIFACT_ID ), version (pluginVersion )),
219207
220208 goal ("generate" ),
221209
222- configuration (
223- element (name ("interactiveMode" ), "false" ),
224- element (name ("archetypeGroupId" ), archetypeGroupId ),
225- element (name ("archetypeArtifactId" ), archetypeArtifactId ),
226- element (name ("archetypeVersion" ), archetypeVersion )
227- ),
210+ configuration (element (name ("interactiveMode" ), "false" ), element (name ("archetypeGroupId" ), archetypeGroupId ),
211+ element (name ("archetypeArtifactId" ), archetypeArtifactId ), element (name ("archetypeVersion" ), archetypeVersion )),
228212
229213 executionEnvironment (mavenProject , mavenSession , getContext ().getBuildPluginManager ()));
230214
@@ -233,13 +217,8 @@ public void execute() throws MojoExecutionException, MojoFailureException {
233217 final File questionFile = new File (projectDir , "questions.json" );
234218
235219 // Create template engines
236- stringTemplateEngine = new PebbleEngine .Builder ()
237- .loader (new StringLoader ())
238- .build ();
239- fileTemplateEngine = new PebbleEngine .Builder ()
240- .loader (new FileLoader ())
241- .extension (new SeedStackExtension (groupId ))
242- .build ();
220+ stringTemplateEngine = new PebbleEngine .Builder ().loader (new StringLoader ()).build ();
221+ fileTemplateEngine = new PebbleEngine .Builder ().loader (new FileLoader ()).extension (new SeedStackExtension (groupId )).build ();
243222
244223 // Create vars
245224 final HashMap <String , Object > vars = new HashMap <>();
@@ -273,8 +252,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {
273252 if (questionFile .exists () && questionFile .canRead ()) {
274253 varsWithAnswers .putAll (getInquirer ().inquire (questionFile .toURI ().toURL ()));
275254 if (!questionFile .delete ()) {
276- getLog ().warn (
277- "Unable to delete question file, useless files may be still be present in project" );
255+ getLog ().warn ("Unable to delete question file, useless files may be still be present in project" );
278256 }
279257 }
280258 } catch (MalformedURLException | InquirerException e ) {
@@ -288,8 +266,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {
288266 }
289267 }
290268
291- private Thread setupInquiryCancelHook (final File projectDir , final File questionFile ,
292- final HashMap <String , Object > vars ) {
269+ private Thread setupInquiryCancelHook (final File projectDir , final File questionFile , final HashMap <String , Object > vars ) {
293270 Thread shutdownRender = new Thread (new Runnable () {
294271 @ Override
295272 public void run () {
@@ -330,8 +307,7 @@ private void renderTemplates(File file, Map<String, Object> vars) {
330307 template .evaluate (stringWriter , vars );
331308 String renderedContent = stringWriter .toString ();
332309 if (renderedContent .trim ().length () > 0 ) {
333- try (Writer writer = new OutputStreamWriter (new FileOutputStream (outputPath ),
334- StandardCharsets .UTF_8 )) {
310+ try (Writer writer = new OutputStreamWriter (new FileOutputStream (outputPath ), StandardCharsets .UTF_8 )) {
335311 writer .write (renderedContent );
336312 }
337313 } else {
@@ -342,8 +318,7 @@ private void renderTemplates(File file, Map<String, Object> vars) {
342318 }
343319 if (!originalPath .equals (outputPath )) {
344320 if (!new File (originalPath ).delete ()) {
345- getLog ().warn (
346- "Unable to delete original file, useless files may be still be present in project" );
321+ getLog ().warn ("Unable to delete original file, useless files may be still be present in project" );
347322 }
348323 }
349324 } catch (PebbleException | IOException e ) {
@@ -367,23 +342,19 @@ private String processPath(String absolutePath, Map<String, Object> vars) {
367342 private Set <Value > findProjectTypes (String archetypeGroupId , String archetypeVersion , String remoteCatalog ) {
368343 Set <Value > possibleTypes = new HashSet <>();
369344 getLog ().info ("Searching for " + archetypeVersion + " archetypes in remote catalog " + remoteCatalog );
370- possibleTypes .addAll (findArchetypes (archetypeGroupId , archetypeVersion ,
371- archetypeManager .getRemoteCatalog (remoteCatalog )));
345+ possibleTypes .addAll (findArchetypes (archetypeGroupId , archetypeVersion , archetypeManager .getRemoteCatalog (remoteCatalog )));
372346
373347 if (possibleTypes .isEmpty ()) {
374348 getLog ().info ("No remote " + archetypeVersion + " archetype found, trying the central catalog" );
375- possibleTypes
376- .addAll (findArchetypes (archetypeGroupId , archetypeVersion , archetypeManager .getRemoteCatalog ()));
349+ possibleTypes .addAll (findArchetypes (archetypeGroupId , archetypeVersion , archetypeManager .getRemoteCatalog (CENTRAL_REMOTE_URL )));
377350 }
378351 if (possibleTypes .isEmpty ()) {
379352 getLog ().info ("No remote or central " + archetypeVersion + " archetype found, trying the local catalog" );
380- possibleTypes .addAll (findArchetypes (archetypeGroupId , archetypeVersion ,
381- archetypeManager .getDefaultLocalCatalog ()));
353+ possibleTypes .addAll (findArchetypes (archetypeGroupId , archetypeVersion , archetypeManager .getDefaultLocalCatalog ()));
382354 }
383355 if (possibleTypes .isEmpty ()) {
384356 getLog ().warn (
385- "No " + archetypeVersion + " archetype found anywhere (check your Maven proxy settings), falling "
386- + "back to hard-coded list" );
357+ "No " + archetypeVersion + " archetype found anywhere (check your Maven proxy settings), falling " + "back to hard-coded list" );
387358 possibleTypes .add (new Value ("addon" ));
388359 possibleTypes .add (new Value ("batch" ));
389360 possibleTypes .add (new Value ("cli" ));
0 commit comments