View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.archetype.ui.creation;
20  
21  import javax.inject.Inject;
22  import javax.inject.Named;
23  import javax.inject.Singleton;
24  
25  import java.util.Iterator;
26  
27  import org.apache.maven.archetype.common.Constants;
28  import org.apache.maven.archetype.ui.ArchetypeConfiguration;
29  import org.codehaus.plexus.components.interactivity.Prompter;
30  import org.codehaus.plexus.components.interactivity.PrompterException;
31  import org.slf4j.Logger;
32  import org.slf4j.LoggerFactory;
33  
34  @Named("default")
35  @Singleton
36  public class DefaultArchetypeCreationQueryer implements ArchetypeCreationQueryer {
37      private static final Logger LOGGER = LoggerFactory.getLogger(DefaultArchetypeCreationQueryer.class);
38  
39      @Inject
40      private Prompter prompter;
41  
42      @Override
43      public String getArchetypeArtifactId(String defaultValue) throws PrompterException {
44          return getValue(Constants.ARCHETYPE_ARTIFACT_ID, defaultValue);
45      }
46  
47      @Override
48      public String getArchetypeGroupId(String defaultValue) throws PrompterException {
49          return getValue(Constants.ARCHETYPE_GROUP_ID, defaultValue);
50      }
51  
52      @Override
53      public String getArchetypeVersion(String defaultValue) throws PrompterException {
54          return getValue(Constants.ARCHETYPE_VERSION, defaultValue);
55      }
56  
57      @Override
58      public String getArtifactId(String defaultValue) throws PrompterException {
59          return getValue(Constants.ARTIFACT_ID, defaultValue);
60      }
61  
62      @Override
63      public boolean askAddAnotherProperty() throws PrompterException {
64          String query = "Add a new custom property";
65  
66          String answer = prompter.prompt(query, "Y");
67  
68          return "Y".equalsIgnoreCase(answer);
69      }
70  
71      @Override
72      public String askNewPropertyKey() throws PrompterException {
73          String query = "Define property key";
74  
75          return prompter.prompt(query);
76      }
77  
78      @Override
79      public String askReplacementValue(String propertyKey, String defaultValue) throws PrompterException {
80          return getValue(propertyKey, defaultValue);
81      }
82  
83      @Override
84      public boolean confirmConfiguration(ArchetypeConfiguration archetypeConfiguration) throws PrompterException {
85          StringBuilder query =
86                  new StringBuilder("Confirm archetype configuration:\n" + Constants.ARCHETYPE_GROUP_ID + "="
87                          + archetypeConfiguration.getGroupId() + "\n" + Constants.ARCHETYPE_ARTIFACT_ID + "="
88                          + archetypeConfiguration.getArtifactId() + "\n" + Constants.ARCHETYPE_VERSION + "="
89                          + archetypeConfiguration.getVersion() + "\n");
90  
91          for (Iterator<?> propertiesIter =
92                          archetypeConfiguration.getProperties().keySet().iterator();
93                  propertiesIter.hasNext(); ) {
94              String property = (String) propertiesIter.next();
95              query.append(property + "=" + archetypeConfiguration.getProperty(property) + "\n");
96          }
97  
98          String answer = prompter.prompt(query.toString(), "Y");
99  
100         return "Y".equalsIgnoreCase(answer);
101     }
102 
103     @Override
104     public String getGroupId(String defaultValue) throws PrompterException {
105         return getValue(Constants.GROUP_ID, defaultValue);
106     }
107 
108     @Override
109     public String getPackage(String defaultValue) throws PrompterException {
110         return getValue(Constants.PACKAGE, defaultValue);
111     }
112 
113     @Override
114     public String getVersion(String defaultValue) throws PrompterException {
115         return getValue(Constants.VERSION, defaultValue);
116     }
117 
118     private String getValue(String requiredProperty, String defaultValue) throws PrompterException {
119         String query = "Define value for " + requiredProperty + ": ";
120         String answer;
121 
122         if ((defaultValue != null) && !defaultValue.equals("null")) {
123             answer = prompter.prompt(query, defaultValue);
124         } else {
125             answer = prompter.prompt(query);
126         }
127         return answer;
128     }
129 }