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 org.apache.maven.model.Scm;
23  import org.apache.maven.shared.release.scm.IdentifiedScm;
24  import org.codehaus.plexus.logging.AbstractLogEnabled;
25  import org.codehaus.plexus.util.IOUtil;
26  
27  import java.io.File;
28  import java.io.FileInputStream;
29  import java.io.FileNotFoundException;
30  import java.io.FileOutputStream;
31  import java.io.IOException;
32  import java.io.InputStream;
33  import java.io.OutputStream;
34  import java.util.Iterator;
35  import java.util.Map;
36  import java.util.Map.Entry;
37  import java.util.Properties;
38  import java.util.Set;
39  
40  /**
41   * Read and write release configuration and state from a properties file.
42   *
43   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
44   * @plexus.component role="org.apache.maven.shared.release.config.ReleaseDescriptorStore" role-hint="properties"
45   */
46  public class PropertiesReleaseDescriptorStore
47      extends AbstractLogEnabled
48      implements ReleaseDescriptorStore
49  {
50      public ReleaseDescriptor read( ReleaseDescriptor mergeDescriptor )
51          throws ReleaseDescriptorStoreException
52      {
53          return read( mergeDescriptor, getDefaultReleasePropertiesFile( mergeDescriptor ) );
54      }
55  
56      public ReleaseDescriptor read( File file )
57          throws ReleaseDescriptorStoreException
58      {
59          return read( null, file );
60      }
61  
62      public ReleaseDescriptor read( ReleaseDescriptor mergeDescriptor, File file )
63          throws ReleaseDescriptorStoreException
64      {
65          Properties properties = new Properties();
66  
67          InputStream inStream = null;
68          try
69          {
70              inStream = new FileInputStream( file );
71  
72              properties.load( inStream );
73          }
74          catch ( FileNotFoundException e )
75          {
76              getLogger().debug( file.getName() + " not found - using empty properties" );
77          }
78          catch ( IOException e )
79          {
80              throw new ReleaseDescriptorStoreException(
81                  "Error reading properties file '" + file.getName() + "': " + e.getMessage(), e );
82          }
83          finally
84          {
85              IOUtil.close( inStream );
86          }
87  
88          ReleaseDescriptor releaseDescriptor = ReleaseUtils.copyPropertiesToReleaseDescriptor( properties );
89  
90          if ( mergeDescriptor != null )
91          {
92              releaseDescriptor = ReleaseUtils.merge( releaseDescriptor, mergeDescriptor );
93          }
94  
95          return releaseDescriptor;
96      }
97  
98      public void write( ReleaseDescriptor config )
99          throws ReleaseDescriptorStoreException
100     {
101         write( config, getDefaultReleasePropertiesFile( config ) );
102     }
103 
104     public void delete( ReleaseDescriptor config )
105     {
106         File file = getDefaultReleasePropertiesFile( config );
107         if ( file.exists() )
108         {
109             file.delete();
110         }
111     }
112 
113     public void write( ReleaseDescriptor config, File file )
114         throws ReleaseDescriptorStoreException
115     {
116         Properties properties = new Properties();
117         properties.setProperty( "completedPhase", config.getCompletedPhase() );
118         if ( config.isCommitByProject() ) //default is false
119         {
120             properties.setProperty( "commitByProject", "true" );
121         }
122         properties.setProperty( "scm.url", config.getScmSourceUrl() );
123         if ( config.getScmId() != null )
124         {
125             properties.setProperty( "scm.id", config.getScmId() );
126         }
127         if ( config.getScmUsername() != null )
128         {
129             properties.setProperty( "scm.username", config.getScmUsername() );
130         }
131         if ( config.getScmPassword() != null )
132         {
133             properties.setProperty( "scm.password", config.getScmPassword() );
134         }
135         if ( config.getScmPrivateKey() != null )
136         {
137             properties.setProperty( "scm.privateKey", config.getScmPrivateKey() );
138         }
139         if ( config.getScmPrivateKeyPassPhrase() != null )
140         {
141             properties.setProperty( "scm.passphrase", config.getScmPrivateKeyPassPhrase() );
142         }
143         if ( config.getScmTagBase() != null )
144         {
145             properties.setProperty( "scm.tagBase", config.getScmTagBase() );
146         }
147         if ( config.getScmBranchBase() != null )
148         {
149             properties.setProperty( "scm.branchBase", config.getScmBranchBase() );
150         }
151         if ( config.getScmReleaseLabel() != null )
152         {
153             properties.setProperty( "scm.tag", config.getScmReleaseLabel() );
154         }
155         if ( config.getScmTagNameFormat() != null )
156         {
157             properties.setProperty( "scm.tagNameFormat", config.getScmTagNameFormat() );
158         }
159         if ( config.getScmCommentPrefix() != null )
160         {
161             properties.setProperty( "scm.commentPrefix", config.getScmCommentPrefix() );
162         }
163         if ( config.getAdditionalArguments() != null )
164         {
165             properties.setProperty( "exec.additionalArguments", config.getAdditionalArguments() );
166         }
167         if ( config.getPomFileName() != null )
168         {
169             properties.setProperty( "exec.pomFileName", config.getPomFileName() );
170         }
171         if ( config.getPreparationGoals() != null )
172         {
173             properties.setProperty( "preparationGoals", config.getPreparationGoals() );
174         }
175         if ( config.getCompletionGoals() != null )
176         {
177             properties.setProperty( "completionGoals", config.getCompletionGoals() );
178         }
179 
180         properties.setProperty( "exec.snapshotReleasePluginAllowed",
181                                 Boolean.toString( config.isSnapshotReleasePluginAllowed() ) );
182 
183         properties.setProperty( "remoteTagging", Boolean.toString( config.isRemoteTagging() ) );
184 
185         properties.setProperty( "pushChanges", Boolean.toString( config.isPushChanges() ) );
186 
187         // others boolean properties are not written to the properties file because the value from the caller is always
188         // used
189 
190         for ( Iterator<?> i = config.getReleaseVersions().entrySet().iterator(); i.hasNext(); )
191         {
192             Map.Entry<?, ?> entry = (Map.Entry<?, ?>) i.next();
193             properties.setProperty( "project.rel." + entry.getKey(), (String) entry.getValue() );
194         }
195 
196         for ( Iterator<?> i = config.getDevelopmentVersions().entrySet().iterator(); i.hasNext(); )
197         {
198             Map.Entry<?, ?> entry = (Map.Entry<?, ?>) i.next();
199             properties.setProperty( "project.dev." + entry.getKey(), (String) entry.getValue() );
200         }
201 
202         for ( Iterator<?> i = config.getOriginalScmInfo().entrySet().iterator(); i.hasNext(); )
203         {
204             Map.Entry<?, ?> entry = (Map.Entry<?, ?>) i.next();
205             Scm scm = (Scm) entry.getValue();
206             String prefix = "project.scm." + entry.getKey();
207             if ( scm != null )
208             {
209                 if ( scm.getConnection() != null )
210                 {
211                     properties.setProperty( prefix + ".connection", scm.getConnection() );
212                 }
213                 if ( scm.getDeveloperConnection() != null )
214                 {
215                     properties.setProperty( prefix + ".developerConnection", scm.getDeveloperConnection() );
216                 }
217                 if ( scm.getUrl() != null )
218                 {
219                     properties.setProperty( prefix + ".url", scm.getUrl() );
220                 }
221                 if ( scm.getTag() != null )
222                 {
223                     properties.setProperty( prefix + ".tag", scm.getTag() );
224                 }
225                 if ( scm instanceof IdentifiedScm )
226                 {
227                     IdentifiedScm identifiedScm = (IdentifiedScm) scm;
228                     if ( identifiedScm.getId() != null )
229                     {
230                         properties.setProperty( prefix + ".id", identifiedScm.getId() );
231                     }
232                 }
233             }
234             else
235             {
236                 properties.setProperty( prefix + ".empty", "true" );
237             }
238         }
239 
240         if ( ( config.getResolvedSnapshotDependencies() != null )
241             && ( config.getResolvedSnapshotDependencies().size() > 0 ) )
242         {
243             processResolvedDependencies( properties, config.getResolvedSnapshotDependencies() );
244         }
245 
246         OutputStream outStream = null;
247         //noinspection OverlyBroadCatchBlock
248         try
249         {
250             outStream = new FileOutputStream( file );
251 
252             properties.store( outStream, "release configuration" );
253         }
254         catch ( IOException e )
255         {
256             throw new ReleaseDescriptorStoreException(
257                 "Error writing properties file '" + file.getName() + "': " + e.getMessage(), e );
258         }
259         finally
260         {
261             IOUtil.close( outStream );
262         }
263 
264     }
265 
266     private void processResolvedDependencies( Properties prop, Map<?, ?> resolvedDependencies )
267     {
268         Set<?> entries = resolvedDependencies.entrySet();
269         Iterator<?> iterator = entries.iterator();
270         Entry<?, ?> currentEntry;
271 
272         while ( iterator.hasNext() )
273         {
274             currentEntry = (Entry<?, ?>) iterator.next();
275 
276             Map<?, ?> versionMap = (Map<?, ?>) currentEntry.getValue();
277 
278             prop.setProperty( "dependency." + currentEntry.getKey() + ".release",
279                               (String) versionMap.get( ReleaseDescriptor.RELEASE_KEY ) );
280             prop.setProperty( "dependency." + currentEntry.getKey() + ".development",
281                               (String) versionMap.get( ReleaseDescriptor.DEVELOPMENT_KEY ) );
282         }
283     }
284 
285     private static File getDefaultReleasePropertiesFile( ReleaseDescriptor mergeDescriptor )
286     {
287         return new File( mergeDescriptor.getWorkingDirectory(), "release.properties" );
288     }
289 
290 }