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