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