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