View Javadoc
1   package org.apache.maven.archetype.ui;
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.metadata.RequiredProperty;
24  import org.apache.maven.project.MavenProject;
25  import org.codehaus.plexus.component.annotations.Component;
26  import org.codehaus.plexus.logging.AbstractLogEnabled;
27  
28  import java.util.Iterator;
29  import java.util.Properties;
30  
31  @Component( role = ArchetypeFactory.class, hint = "default" )
32  public class DefaultArchetypeFactory
33      extends AbstractLogEnabled
34      implements ArchetypeFactory
35  {
36      public ArchetypeDefinition createArchetypeDefinition( Properties properties )
37      {
38          ArchetypeDefinition definition = new ArchetypeDefinition();
39  
40          definition.setGroupId( properties.getProperty( Constants.ARCHETYPE_GROUP_ID ) );
41  
42          definition.setArtifactId( properties.getProperty( Constants.ARCHETYPE_ARTIFACT_ID ) );
43  
44          definition.setVersion( properties.getProperty( Constants.ARCHETYPE_VERSION ) );
45  
46          definition.setRepository( properties.getProperty( Constants.ARCHETYPE_REPOSITORY ) );
47  
48          definition.setUrl( properties.getProperty( Constants.ARCHETYPE_URL ) );
49  
50          definition.setDescription( properties.getProperty( Constants.ARCHETYPE_DESCRIPTION ) );
51  
52          return definition;
53      }
54  
55      private void addOldRequiredProperty( ArchetypeConfiguration configuration, Properties properties, String key,
56                                           String defaultValue, boolean initPropertyWithDefault )
57      {
58          getLogger().debug( "Adding requiredProperty " + key );
59  
60          configuration.addRequiredProperty( key );
61  
62          String property = properties.getProperty( key );
63  
64          if ( property != null )
65          {
66              configuration.setProperty( key, property );
67              configuration.setDefaultProperty( key, property );
68          }
69          else if ( defaultValue != null )
70          {
71              if ( initPropertyWithDefault )
72              {
73                  configuration.setProperty( key, defaultValue );
74              }
75              configuration.setDefaultProperty( key, defaultValue );
76          }
77  
78          getLogger().debug( "Setting property " + key + "=" + configuration.getProperty( key ) );
79      }
80  
81      @SuppressWarnings( "checkstyle:linelength" )
82      public ArchetypeConfiguration createArchetypeConfiguration( org.apache.maven.archetype.old.descriptor.ArchetypeDescriptor archetypeDescriptor,
83                                                                  Properties properties )
84      {
85          getLogger().debug( "Creating ArchetypeConfiguration from legacy descriptor and Properties" );
86  
87          ArchetypeConfiguration configuration = createArchetypeConfiguration( properties );
88  
89          configuration.setName( archetypeDescriptor.getId() );
90  
91          addOldRequiredProperty( configuration, properties, Constants.GROUP_ID, null, false );
92  
93          addOldRequiredProperty( configuration, properties, Constants.ARTIFACT_ID, null, false );
94  
95          addOldRequiredProperty( configuration, properties, Constants.VERSION, "1.0-SNAPSHOT", false );
96  
97          addOldRequiredProperty( configuration, properties, Constants.PACKAGE,
98                                  configuration.getProperty( Constants.GROUP_ID ), true );
99  
100         return configuration;
101     }
102 
103     private void addRequiredProperty( ArchetypeConfiguration configuration, Properties properties, String key,
104                                       String defaultValue, boolean initPropertyWithDefault )
105     {
106         if ( !configuration.isConfigured( key ) && configuration.getDefaultValue( key ) == null )
107         {
108             addOldRequiredProperty( configuration, properties, key, defaultValue, initPropertyWithDefault );
109         }
110     }
111 
112     @SuppressWarnings( "checkstyle:linelength" )
113     public ArchetypeConfiguration createArchetypeConfiguration( org.apache.maven.archetype.metadata.ArchetypeDescriptor archetypeDescriptor,
114                                                                 Properties properties )
115     {
116         getLogger().debug( "Creating ArchetypeConfiguration from fileset descriptor and Properties" );
117 
118         ArchetypeConfiguration configuration = createArchetypeConfiguration( properties );
119 
120         configuration.setName( archetypeDescriptor.getName() );
121 
122         for ( RequiredProperty requiredProperty : archetypeDescriptor.getRequiredProperties() )
123         {
124             String key = requiredProperty.getKey();
125             getLogger().debug( "Adding requiredProperty " + key );
126 
127             configuration.addRequiredProperty( key );
128 
129             String defaultValue = requiredProperty.getDefaultValue();
130             String validationRegex = requiredProperty.getValidationRegex();
131 
132             if ( properties.getProperty( key ) != null )
133             {
134                 // using value defined in properties, which overrides any default
135                 String value = properties.getProperty( key );
136                 configuration.setProperty( key, value );
137                 getLogger().debug( "Setting property " + key + "=" + value );
138             }
139             else if ( ( defaultValue != null ) && !containsInnerProperty( defaultValue ) )
140             {
141                 // using default value
142                 configuration.setProperty( key, defaultValue );
143                 getLogger().debug( "Setting property " + key + "=" + defaultValue );
144             }
145 
146             if ( defaultValue != null )
147             {
148                 configuration.setDefaultProperty( key, defaultValue );
149                 getLogger().debug( "Setting defaultProperty " + key + "=" + defaultValue );
150             }
151 
152             if ( validationRegex != null )
153             {
154                 configuration.setPropertyValidationRegex( key, validationRegex );
155                 getLogger().debug( "Setting validation regular expression " + key + "=" + defaultValue );
156             }
157         }
158 
159         addRequiredProperty( configuration, properties, Constants.GROUP_ID, null, false );
160 
161         addRequiredProperty( configuration, properties, Constants.ARTIFACT_ID, null, false );
162 
163         addRequiredProperty( configuration, properties, Constants.VERSION, "1.0-SNAPSHOT", false );
164 
165         addRequiredProperty( configuration, properties, Constants.PACKAGE,
166                              configuration.getProperty( Constants.GROUP_ID ), true );
167 
168         String postGenerationGoals = properties.getProperty( Constants.ARCHETYPE_POST_GENERATION_GOALS );
169         if ( postGenerationGoals != null )
170         {
171             configuration.setProperty( Constants.ARCHETYPE_POST_GENERATION_GOALS, postGenerationGoals );
172         }
173 
174         return configuration;
175     }
176 
177     private void addRequiredProperty( ArchetypeConfiguration configuration, Properties properties, String key,
178                                       String defaultValue )
179     {
180         getLogger().debug( "Adding requiredProperty " + key );
181 
182         configuration.addRequiredProperty( key );
183 
184         if ( defaultValue != null )
185         {
186             configuration.setDefaultProperty( key, defaultValue );
187         }
188 
189         if ( properties.getProperty( key ) != null )
190         {
191             configuration.setProperty( key, properties.getProperty( key ) );
192 
193             getLogger().debug( "Setting property " + key + "=" + configuration.getProperty( Constants.GROUP_ID ) );
194         }
195     }
196 
197     private void setProperty( ArchetypeConfiguration configuration, Properties properties, String key )
198     {
199         String property = properties.getProperty( key );
200 
201         if ( property != null )
202         {
203             configuration.setProperty( key, property );
204         }
205     }
206 
207     public ArchetypeConfiguration createArchetypeConfiguration( MavenProject project,
208                                                                 ArchetypeDefinition archetypeDefinition,
209                                                                 Properties properties )
210     {
211         getLogger().debug( "Creating ArchetypeConfiguration from ArchetypeDefinition, MavenProject and Properties" );
212 
213         ArchetypeConfiguration configuration = createArchetypeConfiguration( properties );
214 
215         for ( Iterator<?> requiredProperties = properties.keySet().iterator(); requiredProperties.hasNext(); )
216         {
217             String requiredProperty = (String) requiredProperties.next();
218 
219             if ( !requiredProperty.contains( "." ) )
220             {
221                 getLogger().debug( "Adding requiredProperty " + requiredProperty );
222                 configuration.addRequiredProperty( requiredProperty );
223 
224                 configuration.setProperty( requiredProperty, properties.getProperty( requiredProperty ) );
225                 getLogger().debug( "Setting property " + requiredProperty + "="
226                                        + configuration.getProperty( requiredProperty ) );
227             }
228         }
229 
230         addRequiredProperty( configuration, properties, Constants.GROUP_ID, project.getGroupId() );
231 
232         addRequiredProperty( configuration, properties, Constants.ARTIFACT_ID, project.getArtifactId() );
233 
234         addRequiredProperty( configuration, properties, Constants.VERSION, project.getVersion() );
235 
236         addRequiredProperty( configuration, properties, Constants.PACKAGE, null );
237 
238         setProperty( configuration, properties, Constants.ARCHETYPE_GROUP_ID );
239 
240         setProperty( configuration, properties, Constants.ARCHETYPE_ARTIFACT_ID );
241 
242         setProperty( configuration, properties, Constants.ARCHETYPE_VERSION );
243 
244         setProperty( configuration, properties, Constants.ARCHETYPE_URL );
245 
246         setProperty( configuration, properties, Constants.ARCHETYPE_DESCRIPTION );
247 
248         return configuration;
249     }
250 
251     private ArchetypeConfiguration createArchetypeConfiguration( Properties properties )
252     {
253         ArchetypeConfiguration configuration = new ArchetypeConfiguration();
254 
255         configuration.setGroupId( properties.getProperty( Constants.ARCHETYPE_GROUP_ID ) );
256 
257         configuration.setArtifactId( properties.getProperty( Constants.ARCHETYPE_ARTIFACT_ID ) );
258 
259         configuration.setVersion( properties.getProperty( Constants.ARCHETYPE_VERSION ) );
260 
261         configuration.setUrl( properties.getProperty( Constants.ARCHETYPE_URL ) );
262 
263         configuration.setDescription( properties.getProperty( Constants.ARCHETYPE_DESCRIPTION ) );
264 
265         return configuration;
266     }
267 
268     public void updateArchetypeConfiguration( ArchetypeConfiguration archetypeConfiguration,
269                                               ArchetypeDefinition archetypeDefinition )
270     {
271         archetypeConfiguration.setGroupId( archetypeDefinition.getGroupId() );
272         archetypeConfiguration.setArtifactId( archetypeDefinition.getArtifactId() );
273         archetypeConfiguration.setVersion( archetypeDefinition.getVersion() );
274     }
275 
276     /**
277      * Check if the given value references a property, ie contains <code>${...}</code>.
278      *
279      * @param defaultValue the value to check
280      * @return <code>true</code> if the value contains <code>${</code> followed by <code>}</code>
281      */
282     private boolean containsInnerProperty( String defaultValue )
283     {
284         if ( defaultValue == null )
285         {
286             return false;
287         }
288         int start = defaultValue.indexOf( "${" );
289         return ( start >= 0 ) && ( defaultValue.indexOf( "}", start ) >= 0 );
290     }
291 }