1 package org.apache.maven.archetype.ui.creation;
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.common.Constants;
23 import org.apache.maven.archetype.ui.ArchetypeConfiguration;
24 import org.codehaus.plexus.component.annotations.Component;
25 import org.codehaus.plexus.component.annotations.Requirement;
26 import org.codehaus.plexus.components.interactivity.Prompter;
27 import org.codehaus.plexus.components.interactivity.PrompterException;
28 import org.codehaus.plexus.logging.AbstractLogEnabled;
29
30 import java.util.Iterator;
31
32 @Component( role = ArchetypeCreationQueryer.class, hint = "default" )
33 public class DefaultArchetypeCreationQueryer
34 extends AbstractLogEnabled
35 implements ArchetypeCreationQueryer
36 {
37 @Requirement
38 private Prompter prompter;
39
40 public String getArchetypeArtifactId( String defaultValue )
41 throws PrompterException
42 {
43 return getValue( Constants.ARCHETYPE_ARTIFACT_ID, defaultValue );
44 }
45
46 public String getArchetypeGroupId( String defaultValue )
47 throws PrompterException
48 {
49 return getValue( Constants.ARCHETYPE_GROUP_ID, defaultValue );
50 }
51
52 public String getArchetypeVersion( String defaultValue )
53 throws PrompterException
54 {
55 return getValue( Constants.ARCHETYPE_VERSION, defaultValue );
56 }
57
58 public String getArtifactId( String defaultValue )
59 throws PrompterException
60 {
61 return getValue( Constants.ARTIFACT_ID, defaultValue );
62 }
63
64 public boolean askAddAnotherProperty()
65 throws PrompterException
66 {
67 String query = "Add a new custom property";
68
69 String answer = prompter.prompt( query, "Y" );
70
71 return "Y".equalsIgnoreCase( answer );
72 }
73
74 public String askNewPropertyKey()
75 throws PrompterException
76 {
77 String query = "Define property key";
78
79 String answer = prompter.prompt( query );
80
81 return answer;
82 }
83
84 public String askReplacementValue( String propertyKey, String defaultValue )
85 throws PrompterException
86 {
87 return getValue( propertyKey, defaultValue );
88 }
89
90 public boolean confirmConfiguration( ArchetypeConfiguration archetypeConfiguration )
91 throws PrompterException
92 {
93 StringBuilder query =
94 new StringBuilder( "Confirm archetype configuration:\n" + Constants.ARCHETYPE_GROUP_ID + "="
95 + archetypeConfiguration.getGroupId() + "\n" + Constants.ARCHETYPE_ARTIFACT_ID + "="
96 + archetypeConfiguration.getArtifactId() + "\n" + Constants.ARCHETYPE_VERSION + "="
97 + archetypeConfiguration.getVersion() + "\n" );
98
99 for ( Iterator<?> propertiesIter = archetypeConfiguration.getProperties().keySet().iterator();
100 propertiesIter.hasNext(); )
101 {
102 String property = (String) propertiesIter.next();
103 query.append( property + "=" + archetypeConfiguration.getProperty( property ) + "\n" );
104 }
105
106 String answer = prompter.prompt( query.toString(), "Y" );
107
108 return "Y".equalsIgnoreCase( answer );
109 }
110
111 public String getGroupId( String defaultValue )
112 throws PrompterException
113 {
114 return getValue( Constants.GROUP_ID, defaultValue );
115 }
116
117 public String getPackage( String defaultValue )
118 throws PrompterException
119 {
120 return getValue( Constants.PACKAGE, defaultValue );
121 }
122
123 public String getVersion( String defaultValue )
124 throws PrompterException
125 {
126 return getValue( Constants.VERSION, defaultValue );
127 }
128
129 private String getValue( String requiredProperty, String defaultValue )
130 throws PrompterException
131 {
132 String query = "Define value for " + requiredProperty + ": ";
133 String answer;
134
135 if ( ( defaultValue != null ) && !defaultValue.equals( "null" ) )
136 {
137 answer = prompter.prompt( query, defaultValue );
138 }
139 else
140 {
141 answer = prompter.prompt( query );
142 }
143 return answer;
144 }
145 }