View Javadoc
1   package org.apache.maven.archetype.ui.creation;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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      @Override
41      public String getArchetypeArtifactId( String defaultValue )
42          throws PrompterException
43      {
44          return getValue( Constants.ARCHETYPE_ARTIFACT_ID, defaultValue );
45      }
46  
47      @Override
48      public String getArchetypeGroupId( String defaultValue )
49          throws PrompterException
50      {
51          return getValue( Constants.ARCHETYPE_GROUP_ID, defaultValue );
52      }
53  
54      @Override
55      public String getArchetypeVersion( String defaultValue )
56          throws PrompterException
57      {
58          return getValue( Constants.ARCHETYPE_VERSION, defaultValue );
59      }
60  
61      @Override
62      public String getArtifactId( String defaultValue )
63          throws PrompterException
64      {
65          return getValue( Constants.ARTIFACT_ID, defaultValue );
66      }
67  
68      @Override
69      public boolean askAddAnotherProperty()
70          throws PrompterException
71      {
72          String query = "Add a new custom property";
73  
74          String answer = prompter.prompt( query, "Y" );
75  
76          return "Y".equalsIgnoreCase( answer );
77      }
78  
79      @Override
80      public String askNewPropertyKey()
81          throws PrompterException
82      {
83          String query = "Define property key";
84  
85          String answer = prompter.prompt( query );
86  
87          return answer;
88      }
89  
90      @Override
91      public String askReplacementValue( String propertyKey, String defaultValue )
92          throws PrompterException
93      {
94          return getValue( propertyKey, defaultValue );
95      }
96  
97      @Override
98      public boolean confirmConfiguration( ArchetypeConfiguration archetypeConfiguration )
99          throws PrompterException
100     {
101         StringBuilder query =
102             new StringBuilder( "Confirm archetype configuration:\n" + Constants.ARCHETYPE_GROUP_ID + "="
103                 + archetypeConfiguration.getGroupId() + "\n" + Constants.ARCHETYPE_ARTIFACT_ID + "="
104                 + archetypeConfiguration.getArtifactId() + "\n" + Constants.ARCHETYPE_VERSION + "="
105                 + archetypeConfiguration.getVersion() + "\n" );
106 
107         for ( Iterator<?> propertiesIter = archetypeConfiguration.getProperties().keySet().iterator();
108             propertiesIter.hasNext(); )
109         {
110             String property = (String) propertiesIter.next();
111             query.append( property + "=" + archetypeConfiguration.getProperty( property ) + "\n" );
112         }
113 
114         String answer = prompter.prompt( query.toString(), "Y" );
115 
116         return "Y".equalsIgnoreCase( answer );
117     }
118 
119     @Override
120     public String getGroupId( String defaultValue )
121         throws PrompterException
122     {
123         return getValue( Constants.GROUP_ID, defaultValue );
124     }
125 
126     @Override
127     public String getPackage( String defaultValue )
128         throws PrompterException
129     {
130         return getValue( Constants.PACKAGE, defaultValue );
131     }
132 
133     @Override
134     public String getVersion( String defaultValue )
135         throws PrompterException
136     {
137         return getValue( Constants.VERSION, defaultValue );
138     }
139 
140     private String getValue( String requiredProperty, String defaultValue )
141         throws PrompterException
142     {
143         String query = "Define value for " + requiredProperty + ": ";
144         String answer;
145 
146         if ( ( defaultValue != null ) && !defaultValue.equals( "null" ) )
147         {
148             answer = prompter.prompt( query, defaultValue );
149         }
150         else
151         {
152             answer = prompter.prompt( query );
153         }
154         return answer;
155     }
156 }