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