View Javadoc
1   package org.apache.maven.shared.release.config;
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 java.util.Arrays;
23  import java.util.Iterator;
24  import java.util.Map.Entry;
25  import java.util.Properties;
26  import java.util.Set;
27  
28  import org.apache.maven.shared.release.config.ReleaseDescriptorBuilder.BuilderReleaseDescriptor;
29  import org.apache.maven.shared.release.scm.IdentifiedScm;
30  
31  /**
32   * Class providing utility methods used during the release process
33   *
34   * @author <a href="mailto:jwhitlock@apache.org">Jeremy Whitlock</a>
35   */
36  public class ReleaseUtils
37  {
38      private static final String DEVELOPMENT_KEY = "dev";
39  
40      private static final String RELEASE_KEY = "rel";
41  
42      private ReleaseUtils()
43      {
44          // nothing to see here
45      }
46  
47      /**
48       * <p>buildReleaseDescriptor.</p>
49       *
50       * @param builder a {@link org.apache.maven.shared.release.config.ReleaseDescriptorBuilder} object
51       * @return a {@link org.apache.maven.shared.release.config.ReleaseDescriptorBuilder.BuilderReleaseDescriptor} object
52       */
53      public static BuilderReleaseDescriptor buildReleaseDescriptor( ReleaseDescriptorBuilder builder )
54      {
55          return builder.build();
56      }
57  
58      /**
59       * <p>copyPropertiesToReleaseDescriptor.</p>
60       *
61       * @param properties a {@link java.util.Properties} object
62       * @param builder a {@link org.apache.maven.shared.release.config.ReleaseDescriptorBuilder} object
63       */
64      public static void copyPropertiesToReleaseDescriptor( Properties properties, ReleaseDescriptorBuilder builder )
65      {
66          if ( properties.containsKey( "completedPhase" ) )
67          {
68              builder.setCompletedPhase( properties.getProperty( "completedPhase" ) );
69          }
70          if ( properties.containsKey( "commitByProject" ) )
71          {
72              builder.setCommitByProject( Boolean.parseBoolean( properties.getProperty( "commitByProject" ) ) );
73          }
74          if ( properties.containsKey( "scm.id" ) )
75          {
76              builder.setScmId( properties.getProperty( "scm.id" ) );
77          }
78          if ( properties.containsKey( "scm.url" ) )
79          {
80              builder.setScmSourceUrl( properties.getProperty( "scm.url" ) );
81          }
82          if ( properties.containsKey( "scm.username" ) )
83          {
84              builder.setScmUsername( properties.getProperty( "scm.username" ) );
85          }
86          if ( properties.containsKey( "scm.password" ) )
87          {
88              builder.setScmPassword( properties.getProperty( "scm.password" ) );
89          }
90          if ( properties.containsKey( "scm.privateKey" ) )
91          {
92              builder.setScmPrivateKey( properties.getProperty( "scm.privateKey" ) );
93          }
94          if ( properties.containsKey( "scm.passphrase" ) )
95          {
96              builder.setScmPrivateKeyPassPhrase( properties.getProperty( "scm.passphrase" ) );
97          }
98          if ( properties.containsKey( "scm.tagBase" ) )
99          {
100             builder.setScmTagBase( properties.getProperty( "scm.tagBase" ) );
101         }
102         if ( properties.containsKey( "scm.tagNameFormat" ) )
103         {
104             builder.setScmTagNameFormat( properties.getProperty( "scm.tagNameFormat" ) );
105         }
106         if ( properties.containsKey( "scm.branchBase" ) )
107         {
108             builder.setScmBranchBase( properties.getProperty( "scm.branchBase" ) );
109         }
110         if ( properties.containsKey( "scm.tag" ) )
111         {
112             builder.setScmReleaseLabel( properties.getProperty( "scm.tag" ) );
113         }
114         if ( properties.containsKey( "scm.commentPrefix" ) )
115         {
116             builder.setScmCommentPrefix( properties.getProperty( "scm.commentPrefix" ) );
117         }
118         if ( properties.containsKey( "scm.developmentCommitComment" ) )
119         {
120             builder.setScmDevelopmentCommitComment( properties.getProperty( "scm.developmentCommitComment" ) );
121         }
122         if ( properties.containsKey( "scm.releaseCommitComment" ) )
123         {
124             builder.setScmReleaseCommitComment( properties.getProperty( "scm.releaseCommitComment" ) );
125         }
126         if ( properties.containsKey( "scm.branchCommitComment" ) )
127         {
128             builder.setScmBranchCommitComment( properties.getProperty( "scm.branchCommitComment" ) );
129         }
130         if ( properties.containsKey( "scm.rollbackCommitComment" ) )
131         {
132             builder.setScmRollbackCommitComment( properties.getProperty( "scm.rollbackCommitComment" ) );
133         }
134         if ( properties.containsKey( "exec.additionalArguments" ) )
135         {
136             builder.setAdditionalArguments( properties.getProperty( "exec.additionalArguments" ) );
137         }
138         if ( properties.containsKey( "exec.pomFileName" ) )
139         {
140             builder.setPomFileName( properties.getProperty( "exec.pomFileName" ) );
141         }
142         if ( properties.containsKey( "exec.activateProfiles" ) )
143         {
144             builder.setActivateProfiles( 
145                          Arrays.asList( properties.getProperty( "exec.activateProfiles" ).split( "," ) ) );
146         }
147         if ( properties.containsKey( "preparationGoals" ) )
148         {
149             builder.setPreparationGoals( properties.getProperty( "preparationGoals" ) );
150         }
151         if ( properties.containsKey( "completionGoals" ) )
152         {
153             builder.setCompletionGoals( properties.getProperty( "completionGoals" ) );
154         }
155         if ( properties.containsKey( "projectVersionPolicyId" ) )
156         {
157             builder.setProjectVersionPolicyId( properties.getProperty( "projectVersionPolicyId" ) );
158         }
159         if ( properties.containsKey( "projectNamingPolicyId" ) )
160         {
161             builder.setProjectNamingPolicyId( properties.getProperty( "projectNamingPolicyId" ) );
162         }
163         if ( properties.containsKey( "releaseStrategyId" ) )
164         {
165             builder.setReleaseStrategyId( properties.getProperty( "releaseStrategyId" ) );
166         }
167         if ( properties.containsKey( "exec.snapshotReleasePluginAllowed" ) )
168         {
169             String snapshotReleasePluginAllowedStr = properties.getProperty( "exec.snapshotReleasePluginAllowed" );
170             builder.setSnapshotReleasePluginAllowed( Boolean.valueOf( snapshotReleasePluginAllowedStr ) );
171         }
172         if ( properties.containsKey( "remoteTagging" ) )
173         {
174             String remoteTaggingStr = properties.getProperty( "remoteTagging" );
175             builder.setRemoteTagging( Boolean.valueOf( remoteTaggingStr ) );
176         }
177         if ( properties.containsKey( "pinExternals" ) )
178         {
179             String pinExternals = properties.getProperty( "pinExternals" );
180             builder.setPinExternals( Boolean.valueOf( pinExternals ) );
181         }
182         if ( properties.containsKey( "pushChanges" ) )
183         {
184             String pushChanges = properties.getProperty( "pushChanges" );
185             builder.setPushChanges( Boolean.valueOf( pushChanges ) );
186         }
187         if ( properties.containsKey( "workItem" ) )
188         {
189             builder.setWorkItem( properties.getProperty( "workItem" ) );
190         }
191         if ( properties.containsKey( "autoResolveSnapshots" ) )
192         {
193             String resolve = properties.getProperty( "autoResolveSnapshots" );
194             builder.setAutoResolveSnapshots( resolve );
195         }
196 
197         loadResolvedDependencies( properties, builder );
198 
199         // boolean properties are not written to the properties file because the value from the caller is always used
200 
201         for ( Iterator<?> i = properties.keySet().iterator(); i.hasNext(); )
202         {
203             String property = (String) i.next();
204             if ( property.startsWith( "project.rel." ) )
205             {
206                 builder.addReleaseVersion( property.substring( "project.rel.".length() ),
207                                                      properties.getProperty( property ) );
208             }
209             else if ( property.startsWith( "project.dev." ) )
210             {
211                 builder.addDevelopmentVersion( property.substring( "project.dev.".length() ),
212                                                          properties.getProperty( property ) );
213             }
214             else if ( property.startsWith( "dependency.rel." ) )
215             {
216                 builder.addDependencyReleaseVersion( property.substring( "dependency.rel.".length() ),
217                                                      properties.getProperty( property ) );
218             }
219             else if ( property.startsWith( "dependency.dev." ) )
220             {
221                 builder.addDependencyDevelopmentVersion( property.substring( "dependency.dev.".length() ),
222                                                          properties.getProperty( property ) );
223             }
224             else if ( property.startsWith( "project.scm." ) )
225             {
226                 int index = property.lastIndexOf( '.' );
227                 if ( index > "project.scm.".length() )
228                 {
229                     String key = property.substring( "project.scm.".length(), index );
230 
231                     if ( builder.build().getOriginalScmInfo( key ) == null )
232                     {
233                         if ( properties.getProperty( "project.scm." + key + ".empty" ) != null )
234                         {
235                             builder.addOriginalScmInfo( key, null );
236                         }
237                         else
238                         {
239                             IdentifiedScm scm = new IdentifiedScm();
240                             scm.setConnection( properties.getProperty( "project.scm." + key + ".connection" ) );
241                             scm.setDeveloperConnection(
242                                 properties.getProperty( "project.scm." + key + ".developerConnection" ) );
243                             scm.setUrl( properties.getProperty( "project.scm." + key + ".url" ) );
244                             scm.setTag( properties.getProperty( "project.scm." + key + ".tag" ) );
245                             scm.setId( properties.getProperty( "project.scm." + key + ".id" ) );
246 
247                             builder.addOriginalScmInfo( key, scm );
248                         }
249                     }
250                 }
251             }
252         }
253     }
254 
255     private static void loadResolvedDependencies( Properties prop, ReleaseDescriptorBuilder builder )
256     {
257         Set entries = prop.entrySet();
258         Iterator<Entry<String, String>> iterator = entries.iterator();
259         String propertyName;
260         Entry<String, String> currentEntry;
261 
262         while ( iterator.hasNext() )
263         {
264             currentEntry = iterator.next();
265             propertyName = currentEntry.getKey();
266 
267             if ( propertyName.startsWith( "dependency." ) )
268             {
269                 String artifactVersionlessKey;
270                 int startIndex = "dependency.".length();
271                 int endIndex;
272                 String versionType;
273 
274                 if ( propertyName.indexOf( ".development" ) != -1 )
275                 {
276                     endIndex = propertyName.lastIndexOf( ".development" );
277                     versionType = DEVELOPMENT_KEY;
278                 }
279                 else if ( propertyName.indexOf( ".release" ) != -1 )
280                 {
281                     endIndex = propertyName.lastIndexOf( ".release" );
282                     versionType = RELEASE_KEY;
283                 }
284                 else
285                 {
286                     // MRELEASE-834, probably a maven-dependency-plugin property
287                     continue;
288                 }
289 
290                 artifactVersionlessKey = propertyName.substring( startIndex, endIndex );
291 
292                 if ( RELEASE_KEY.equals( versionType ) )
293                 {
294                     builder.addDependencyReleaseVersion( artifactVersionlessKey, currentEntry.getValue() );
295                 }
296                 else if ( DEVELOPMENT_KEY.equals( versionType ) )
297                 {
298                     builder.addDependencyDevelopmentVersion( artifactVersionlessKey, currentEntry.getValue() );
299                 }
300             }
301         }
302     }
303 
304 }