View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.shared.release.config;
20  
21  import javax.inject.Inject;
22  import javax.inject.Named;
23  import javax.inject.Singleton;
24  
25  import java.io.File;
26  import java.io.FileInputStream;
27  import java.io.FileNotFoundException;
28  import java.io.FileOutputStream;
29  import java.io.IOException;
30  import java.io.InputStream;
31  import java.io.OutputStream;
32  import java.util.Map;
33  import java.util.Properties;
34  
35  import org.apache.maven.model.Scm;
36  import org.apache.maven.shared.release.config.ReleaseDescriptorBuilder.BuilderReleaseDescriptor;
37  import org.apache.maven.shared.release.scm.IdentifiedScm;
38  import org.apache.maven.shared.release.util.MavenCrypto;
39  import org.apache.maven.shared.release.util.MavenCrypto.MavenCryptoException;
40  import org.codehaus.plexus.util.StringUtils;
41  import org.slf4j.Logger;
42  import org.slf4j.LoggerFactory;
43  
44  import static java.util.Objects.requireNonNull;
45  
46  /**
47   * Read and write release configuration and state from a properties file.
48   *
49   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
50   */
51  @Singleton
52  @Named("properties")
53  public class PropertiesReleaseDescriptorStore implements ReleaseDescriptorStore {
54      private final Logger logger = LoggerFactory.getLogger(getClass());
55  
56      private final MavenCrypto mavenCrypto;
57  
58      @Inject
59      public PropertiesReleaseDescriptorStore(MavenCrypto mavenCrypto) {
60          this.mavenCrypto = requireNonNull(mavenCrypto);
61      }
62  
63      @Override
64      public ReleaseDescriptorBuilder read(ReleaseDescriptorBuilder mergeDescriptor)
65              throws ReleaseDescriptorStoreException {
66          return read(mergeDescriptor, getDefaultReleasePropertiesFile(mergeDescriptor.build()));
67      }
68  
69      /**
70       * <p>read.</p>
71       *
72       * @param file a {@link java.io.File} object
73       * @return a {@link org.apache.maven.shared.release.config.ReleaseDescriptorBuilder} object
74       * @throws org.apache.maven.shared.release.config.ReleaseDescriptorStoreException if any.
75       */
76      public ReleaseDescriptorBuilder read(File file) throws ReleaseDescriptorStoreException {
77          return read(null, file);
78      }
79  
80      /**
81       * <p>read.</p>
82       *
83       * @param mergeDescriptor a {@link org.apache.maven.shared.release.config.ReleaseDescriptorBuilder} object
84       * @param file            a {@link java.io.File} object
85       * @return a {@link org.apache.maven.shared.release.config.ReleaseDescriptorBuilder} object
86       * @throws org.apache.maven.shared.release.config.ReleaseDescriptorStoreException if any.
87       */
88      public ReleaseDescriptorBuilder read(ReleaseDescriptorBuilder mergeDescriptor, File file)
89              throws ReleaseDescriptorStoreException {
90          Properties properties = new Properties();
91  
92          try (InputStream inStream = new FileInputStream(file)) {
93              properties.load(inStream);
94          } catch (FileNotFoundException e) {
95              logger.debug(file.getName() + " not found - using empty properties");
96          } catch (IOException e) {
97              throw new ReleaseDescriptorStoreException(
98                      "Error reading properties file '" + file.getName() + "': " + e.getMessage(), e);
99          }
100 
101         try {
102             mavenCrypto.decryptProperties(properties);
103         } catch (MavenCryptoException e) {
104             logger.debug(e.getMessage());
105         }
106 
107         ReleaseDescriptorBuilder builder;
108         if (mergeDescriptor != null) {
109             builder = mergeDescriptor;
110         } else {
111             builder = new ReleaseDescriptorBuilder();
112         }
113 
114         ReleaseUtils.copyPropertiesToReleaseDescriptor(properties, builder);
115 
116         return builder;
117     }
118 
119     @Override
120     public void write(ReleaseDescriptor config) throws ReleaseDescriptorStoreException {
121         write((BuilderReleaseDescriptor) config, getDefaultReleasePropertiesFile(config));
122     }
123 
124     @Override
125     public void delete(ReleaseDescriptor config) {
126         File file = getDefaultReleasePropertiesFile(config);
127         if (file.exists()) {
128             file.delete();
129         }
130     }
131 
132     /**
133      * <p>write.</p>
134      *
135      * @param config a {@link org.apache.maven.shared.release.config.ReleaseDescriptorBuilder.BuilderReleaseDescriptor}
136      *               object
137      * @param file   a {@link java.io.File} object
138      * @throws org.apache.maven.shared.release.config.ReleaseDescriptorStoreException if any.
139      */
140     public void write(BuilderReleaseDescriptor config, File file) throws ReleaseDescriptorStoreException {
141         Properties properties = new Properties();
142         properties.setProperty("completedPhase", config.getCompletedPhase());
143         if (config.isCommitByProject()) // default is false
144         {
145             properties.setProperty("commitByProject", "true");
146         }
147         properties.setProperty("scm.url", config.getScmSourceUrl());
148         if (config.getScmId() != null) {
149             properties.setProperty("scm.id", config.getScmId());
150         }
151         if (config.getScmUsername() != null) {
152             properties.setProperty("scm.username", config.getScmUsername());
153         }
154         if (config.getScmPassword() != null) {
155             String password = config.getScmPassword();
156             try {
157                 password = mavenCrypto.encryptAndDecorate(password);
158             } catch (MavenCryptoException e) {
159                 logger.debug(e.getMessage());
160             }
161             properties.setProperty("scm.password", password);
162         }
163         if (config.getScmPrivateKey() != null) {
164             properties.setProperty("scm.privateKey", config.getScmPrivateKey());
165         }
166         if (config.getScmPrivateKeyPassPhrase() != null) {
167             String passPhrase = config.getScmPrivateKeyPassPhrase();
168             try {
169                 passPhrase = mavenCrypto.encryptAndDecorate(passPhrase);
170             } catch (MavenCryptoException e) {
171                 logger.debug(e.getMessage());
172             }
173             properties.setProperty("scm.passphrase", passPhrase);
174         }
175         if (config.getScmTagBase() != null) {
176             properties.setProperty("scm.tagBase", config.getScmTagBase());
177         }
178         if (config.getScmBranchBase() != null) {
179             properties.setProperty("scm.branchBase", config.getScmBranchBase());
180         }
181         if (config.getScmReleaseLabel() != null) {
182             properties.setProperty("scm.tag", config.getScmReleaseLabel());
183         }
184         if (config.getScmTagNameFormat() != null) {
185             properties.setProperty("scm.tagNameFormat", config.getScmTagNameFormat());
186         }
187         if (config.getScmCommentPrefix() != null) {
188             properties.setProperty("scm.commentPrefix", config.getScmCommentPrefix());
189         }
190         if (config.getScmDevelopmentCommitComment() != null) {
191             properties.setProperty("scm.developmentCommitComment", config.getScmDevelopmentCommitComment());
192         }
193         if (config.getScmReleaseCommitComment() != null) {
194             properties.setProperty("scm.releaseCommitComment", config.getScmReleaseCommitComment());
195         }
196         if (config.getScmBranchCommitComment() != null) {
197             properties.setProperty("scm.branchCommitComment", config.getScmBranchCommitComment());
198         }
199         if (config.getScmRollbackCommitComment() != null) {
200             properties.setProperty("scm.rollbackCommitComment", config.getScmRollbackCommitComment());
201         }
202         if (config.getAdditionalArguments() != null) {
203             properties.setProperty("exec.additionalArguments", config.getAdditionalArguments());
204         }
205         if (config.getPomFileName() != null) {
206             properties.setProperty("exec.pomFileName", config.getPomFileName());
207         }
208         if (!config.getActivateProfiles().isEmpty()) {
209             properties.setProperty(
210                     "exec.activateProfiles",
211                     StringUtils.join(config.getActivateProfiles().iterator(), ","));
212         }
213         if (config.getPreparationGoals() != null) {
214             properties.setProperty("preparationGoals", config.getPreparationGoals());
215         }
216         if (config.getCompletionGoals() != null) {
217             properties.setProperty("completionGoals", config.getCompletionGoals());
218         }
219         if (config.getProjectVersionPolicyId() != null) {
220             properties.setProperty("projectVersionPolicyId", config.getProjectVersionPolicyId());
221         }
222         if (config.getProjectVersionPolicyConfig() != null) {
223             properties.setProperty(
224                     "projectVersionPolicyConfig",
225                     config.getProjectVersionPolicyConfig().toString());
226         }
227         if (config.getProjectNamingPolicyId() != null) {
228             properties.setProperty("projectNamingPolicyId", config.getProjectNamingPolicyId());
229         }
230         if (config.getReleaseStrategyId() != null) {
231             properties.setProperty("releaseStrategyId", config.getReleaseStrategyId());
232         }
233 
234         properties.setProperty(
235                 "exec.snapshotReleasePluginAllowed", Boolean.toString(config.isSnapshotReleasePluginAllowed()));
236 
237         properties.setProperty("remoteTagging", Boolean.toString(config.isRemoteTagging()));
238 
239         properties.setProperty("pinExternals", Boolean.toString(config.isPinExternals()));
240 
241         properties.setProperty("pushChanges", Boolean.toString(config.isPushChanges()));
242 
243         if (config.getWorkItem() != null) {
244             properties.setProperty("workItem", config.getWorkItem());
245         }
246 
247         if (config.getAutoResolveSnapshots() != null) {
248             properties.setProperty("autoResolveSnapshots", config.getAutoResolveSnapshots());
249         }
250 
251         // others boolean properties are not written to the properties file because the value from the caller is always
252         // used
253 
254         for (Map.Entry<String, ReleaseStageVersions> entry :
255                 config.getProjectVersions().entrySet()) {
256             if (entry.getValue().getRelease() != null) {
257                 properties.setProperty(
258                         "project.rel." + entry.getKey(), entry.getValue().getRelease());
259             }
260             if (entry.getValue().getDevelopment() != null) {
261                 properties.setProperty(
262                         "project.dev." + entry.getKey(), entry.getValue().getDevelopment());
263             }
264         }
265 
266         for (Map.Entry<String, Scm> entry : config.getOriginalScmInfo().entrySet()) {
267             Scm scm = entry.getValue();
268             String prefix = "project.scm." + entry.getKey();
269             if (scm != null) {
270                 if (scm.getConnection() != null) {
271                     properties.setProperty(prefix + ".connection", scm.getConnection());
272                 }
273                 if (scm.getDeveloperConnection() != null) {
274                     properties.setProperty(prefix + ".developerConnection", scm.getDeveloperConnection());
275                 }
276                 if (scm.getUrl() != null) {
277                     properties.setProperty(prefix + ".url", scm.getUrl());
278                 }
279                 if (scm.getTag() != null) {
280                     properties.setProperty(prefix + ".tag", scm.getTag());
281                 }
282                 if (scm instanceof IdentifiedScm) {
283                     IdentifiedScm identifiedScm = (IdentifiedScm) scm;
284                     if (identifiedScm.getId() != null) {
285                         properties.setProperty(prefix + ".id", identifiedScm.getId());
286                     }
287                 }
288             } else {
289                 properties.setProperty(prefix + ".empty", "true");
290             }
291         }
292 
293         if ((config.getResolvedSnapshotDependencies() != null)
294                 && (config.getResolvedSnapshotDependencies().size() > 0)) {
295             processResolvedDependencies(properties, config.getResolvedSnapshotDependencies());
296         }
297 
298         try (OutputStream outStream = new FileOutputStream(file)) {
299             properties.store(outStream, "release configuration");
300         } catch (IOException e) {
301             throw new ReleaseDescriptorStoreException(
302                     "Error writing properties file '" + file.getName() + "': " + e.getMessage(), e);
303         }
304     }
305 
306     private void processResolvedDependencies(Properties prop, Map<String, ReleaseStageVersions> resolvedDependencies) {
307         for (Map.Entry<String, ReleaseStageVersions> currentEntry : resolvedDependencies.entrySet()) {
308             ReleaseStageVersions versionMap = currentEntry.getValue();
309 
310             prop.setProperty("dependency." + currentEntry.getKey() + ".release", versionMap.getRelease());
311             prop.setProperty("dependency." + currentEntry.getKey() + ".development", versionMap.getDevelopment());
312         }
313     }
314 
315     private static File getDefaultReleasePropertiesFile(ReleaseDescriptor mergeDescriptor) {
316         return new File(mergeDescriptor.getWorkingDirectory(), "release.properties");
317     }
318 }