55 * License, v. 2.0. If a copy of the MPL was not distributed with this
66 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
77 */
8+
89package org .seedstack .maven ;
910
1011import static org .twdata .maven .mojoexecutor .MojoExecutor .artifactId ;
@@ -94,21 +95,29 @@ public class GenerateMojo extends AbstractSeedStackMojo {
9495
9596 @ Override
9697 public void execute () throws MojoExecutionException , MojoFailureException {
97- this .basicMode = !mavenSession .getUserProperties ().getProperty ("basicPrompt" , "false" ).equals ("false" );
98+ if (mavenSession .getUserProperties ().getProperty ("basicPrompt" ) == null ) {
99+ this .basicMode = autodetectBasicMode ();
100+ } else {
101+ this .basicMode = !mavenSession .getUserProperties ().getProperty ("basicPrompt" , "false" ).equals ("false" );
102+ }
98103 if (!basicMode ) {
99- getLog ().info ("Hint: if enhanced prompt has issues on your system, try basic prompt by adding \" -DbasicPrompt\" to your command line" );
104+ getLog ().info (
105+ "Hint: if enhanced prompt has issues on your system, try basic prompt by adding \" -DbasicPrompt\" "
106+ + " to your command line" );
100107 }
101108
102109 String type = mavenSession .getUserProperties ().getProperty ("type" ),
103- distributionGroupId = mavenSession .getUserProperties ().getProperty ("distributionGroupId" , "org.seedstack" ),
104- distributionArtifactId = mavenSession .getUserProperties ().getProperty ("distributionArtifactId" , "distribution" ),
110+ distributionGroupId = mavenSession .getUserProperties ()
111+ .getProperty ("distributionGroupId" , "org.seedstack" ),
112+ distributionArtifactId = mavenSession .getUserProperties ()
113+ .getProperty ("distributionArtifactId" , "distribution" ),
105114 version = mavenSession .getUserProperties ().getProperty ("version" ),
106115 archetypeGroupId = mavenSession .getUserProperties ().getProperty ("archetypeGroupId" ),
107116 archetypeArtifactId = mavenSession .getUserProperties ().getProperty ("archetypeArtifactId" ),
108117 archetypeVersion = mavenSession .getUserProperties ().getProperty ("archetypeVersion" ),
109118 remoteCatalog = mavenSession .getUserProperties ().getProperty ("remoteCatalog" , SEEDSTACK_ORG );
110- boolean allowSnapshots = !mavenSession .getUserProperties ().getProperty ("allowSnapshots" , "false" ). equals ( "false" );
111-
119+ boolean allowSnapshots = !mavenSession .getUserProperties ().getProperty ("allowSnapshots" , "false" )
120+ . equals ( "false" );
112121
113122 if (StringUtils .isBlank (archetypeGroupId )) {
114123 archetypeGroupId = distributionGroupId ;
@@ -118,13 +127,19 @@ public void execute() throws MojoExecutionException, MojoFailureException {
118127 if (StringUtils .isBlank (type )) {
119128 // Resolve archetype version using SeedStack highest version
120129 if (StringUtils .isBlank (archetypeVersion )) {
121- getLog ().info ("Resolving latest " + (allowSnapshots ? "snapshot" : "release" ) + " of SeedStack (" + distributionGroupId + ")" );
122- archetypeVersion = artifactResolver .getHighestVersion (mavenProject , distributionGroupId , distributionArtifactId , allowSnapshots );
130+ getLog ().info (
131+ "Resolving latest " + (allowSnapshots ? "snapshot" : "release" ) + " of SeedStack (" +
132+ distributionGroupId + ")" );
133+ archetypeVersion = artifactResolver
134+ .getHighestVersion (mavenProject , distributionGroupId , distributionArtifactId ,
135+ allowSnapshots );
123136 getLog ().info ("Resolved version " + archetypeVersion );
124137 }
125138 try {
126- // We have a list of possible types, let the user choose (if a "web" choice exists, set it as default)
127- List <Value > list = new ArrayList <>(findProjectTypes (archetypeGroupId , archetypeVersion , remoteCatalog ));
139+ // We have a list of possible types, let the user choose (if a "web" choice exists, set it as
140+ // default)
141+ List <Value > list = new ArrayList <>(
142+ findProjectTypes (archetypeGroupId , archetypeVersion , remoteCatalog ));
128143 Collections .sort (list );
129144 list .add (new Value ("custom archetype" , "custom" ));
130145 type = getPrompter ().promptList ("Choose the project type" , list , "web" );
@@ -139,7 +154,9 @@ public void execute() throws MojoExecutionException, MojoFailureException {
139154 }
140155 // Ask for archetype version (defaults to latest)
141156 try {
142- archetypeVersion = artifactResolver .getHighestVersion (mavenProject , archetypeGroupId , archetypeArtifactId , allowSnapshots );
157+ archetypeVersion = artifactResolver
158+ .getHighestVersion (mavenProject , archetypeGroupId , archetypeArtifactId ,
159+ allowSnapshots );
143160 } catch (Exception e ) {
144161 archetypeVersion = null ;
145162 }
@@ -157,8 +174,11 @@ public void execute() throws MojoExecutionException, MojoFailureException {
157174
158175 // If needed, find the latest version of the archetype
159176 if (StringUtils .isBlank (archetypeVersion )) {
160- getLog ().info ("Resolving latest " + (allowSnapshots ? "snapshot" : "release" ) + " of archetype " + archetypeGroupId + ":" + archetypeArtifactId );
161- archetypeVersion = artifactResolver .getHighestVersion (mavenProject , archetypeGroupId , archetypeArtifactId , allowSnapshots );
177+ getLog ().info (
178+ "Resolving latest " + (allowSnapshots ? "snapshot" : "release" ) + " of archetype " +
179+ archetypeGroupId + ":" + archetypeArtifactId );
180+ archetypeVersion = artifactResolver
181+ .getHighestVersion (mavenProject , archetypeGroupId , archetypeArtifactId , allowSnapshots );
162182 getLog ().info ("Resolved version " + archetypeVersion );
163183 }
164184
@@ -178,7 +198,8 @@ public void execute() throws MojoExecutionException, MojoFailureException {
178198 throw new MojoExecutionException ("Generated project group id cannot be blank" );
179199 }
180200 if (StringUtils .isBlank (artifactId )) {
181- artifactId = getPrompter ().promptInput ("Generated project artifact id" , "my-" + (StringUtils .isBlank (type ) ? "" : type + "-" ) + "project" );
201+ artifactId = getPrompter ().promptInput ("Generated project artifact id" ,
202+ "my-" + (StringUtils .isBlank (type ) ? "" : type + "-" ) + "project" );
182203 }
183204 if (StringUtils .isBlank (artifactId )) {
184205 throw new MojoExecutionException ("Generated project artifact id cannot be blank" );
@@ -192,14 +213,16 @@ public void execute() throws MojoExecutionException, MojoFailureException {
192213
193214 String pluginVersion ;
194215 try {
195- pluginVersion = artifactResolver .getHighestVersion (mavenProject , ARCHETYPE_PLUGIN_GROUP_ID , ARCHETYPE_PLUGIN_ARTIFACT_ID , false );
216+ pluginVersion = artifactResolver
217+ .getHighestVersion (mavenProject , ARCHETYPE_PLUGIN_GROUP_ID , ARCHETYPE_PLUGIN_ARTIFACT_ID , false );
196218 getLog ().info ("Using the latest version of archetype plugin: " + pluginVersion );
197219 } catch (Exception e ) {
198220 getLog ().warn ("Unable to determine latest version of archetype plugin, falling back to 3.0.0" );
199221 pluginVersion = "3.0.0" ;
200222 }
201223
202- executeMojo (plugin (groupId (ARCHETYPE_PLUGIN_GROUP_ID ), artifactId (ARCHETYPE_PLUGIN_ARTIFACT_ID ), version (pluginVersion )),
224+ executeMojo (plugin (groupId (ARCHETYPE_PLUGIN_GROUP_ID ), artifactId (ARCHETYPE_PLUGIN_ARTIFACT_ID ),
225+ version (pluginVersion )),
203226
204227 goal ("generate" ),
205228
@@ -257,7 +280,8 @@ public void execute() throws MojoExecutionException, MojoFailureException {
257280 if (questionFile .exists () && questionFile .canRead ()) {
258281 varsWithAnswers .putAll (getInquirer ().inquire (questionFile .toURI ().toURL ()));
259282 if (!questionFile .delete ()) {
260- getLog ().warn ("Unable to delete question file, useless files may be still be present in project" );
283+ getLog ().warn (
284+ "Unable to delete question file, useless files may be still be present in project" );
261285 }
262286 }
263287 } catch (MalformedURLException | InquirerException e ) {
@@ -271,7 +295,8 @@ public void execute() throws MojoExecutionException, MojoFailureException {
271295 }
272296 }
273297
274- private Thread setupInquiryCancelHook (final File projectDir , final File questionFile , final HashMap <String , Object > vars ) {
298+ private Thread setupInquiryCancelHook (final File projectDir , final File questionFile ,
299+ final HashMap <String , Object > vars ) {
275300 Thread shutdownRender = new Thread (new Runnable () {
276301 @ Override
277302 public void run () {
@@ -312,7 +337,8 @@ private void renderTemplates(File file, Map<String, Object> vars) {
312337 template .evaluate (stringWriter , vars );
313338 String renderedContent = stringWriter .toString ();
314339 if (renderedContent .trim ().length () > 0 ) {
315- try (Writer writer = new OutputStreamWriter (new FileOutputStream (outputPath ), StandardCharsets .UTF_8 )) {
340+ try (Writer writer = new OutputStreamWriter (new FileOutputStream (outputPath ),
341+ StandardCharsets .UTF_8 )) {
316342 writer .write (renderedContent );
317343 }
318344 } else {
@@ -323,7 +349,8 @@ private void renderTemplates(File file, Map<String, Object> vars) {
323349 }
324350 if (!originalPath .equals (outputPath )) {
325351 if (!new File (originalPath ).delete ()) {
326- getLog ().warn ("Unable to delete original file, useless files may be still be present in project" );
352+ getLog ().warn (
353+ "Unable to delete original file, useless files may be still be present in project" );
327354 }
328355 }
329356 } catch (PebbleException | IOException e ) {
@@ -347,18 +374,23 @@ private String processPath(String absolutePath, Map<String, Object> vars) {
347374 private Set <Value > findProjectTypes (String archetypeGroupId , String archetypeVersion , String remoteCatalog ) {
348375 Set <Value > possibleTypes = new HashSet <>();
349376 getLog ().info ("Searching for " + archetypeVersion + " archetypes in remote catalog " + remoteCatalog );
350- possibleTypes .addAll (findArchetypes (archetypeGroupId , archetypeVersion , archetypeManager .getRemoteCatalog (remoteCatalog )));
377+ possibleTypes .addAll (findArchetypes (archetypeGroupId , archetypeVersion ,
378+ archetypeManager .getRemoteCatalog (remoteCatalog )));
351379
352380 if (possibleTypes .isEmpty ()) {
353381 getLog ().info ("No remote " + archetypeVersion + " archetype found, trying the central catalog" );
354- possibleTypes .addAll (findArchetypes (archetypeGroupId , archetypeVersion , archetypeManager .getRemoteCatalog ()));
382+ possibleTypes
383+ .addAll (findArchetypes (archetypeGroupId , archetypeVersion , archetypeManager .getRemoteCatalog ()));
355384 }
356385 if (possibleTypes .isEmpty ()) {
357386 getLog ().info ("No remote or central " + archetypeVersion + " archetype found, trying the local catalog" );
358- possibleTypes .addAll (findArchetypes (archetypeGroupId , archetypeVersion , archetypeManager .getDefaultLocalCatalog ()));
387+ possibleTypes .addAll (findArchetypes (archetypeGroupId , archetypeVersion ,
388+ archetypeManager .getDefaultLocalCatalog ()));
359389 }
360390 if (possibleTypes .isEmpty ()) {
361- getLog ().warn ("No " + archetypeVersion + " archetype found anywhere (check your Maven proxy settings), falling back to hard-coded list" );
391+ getLog ().warn (
392+ "No " + archetypeVersion + " archetype found anywhere (check your Maven proxy settings), falling "
393+ + "back to hard-coded list" );
362394 possibleTypes .add (new Value ("addon" ));
363395 possibleTypes .add (new Value ("batch" ));
364396 possibleTypes .add (new Value ("cli" ));
@@ -397,4 +429,15 @@ private Inquirer getInquirer() {
397429 return fancyInquirer ;
398430 }
399431 }
432+
433+ private boolean autodetectBasicMode () {
434+ String ostype = System .getenv ("OSTYPE" );
435+ if (ostype != null && !ostype .isEmpty ()) {
436+ // Fancy mode never works under cygwin
437+ if (ostype .startsWith ("cygwin" )) {
438+ return true ;
439+ }
440+ }
441+ return false ;
442+ }
400443}
0 commit comments