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.exec;
20  
21  import java.io.File;
22  import java.util.ArrayList;
23  import java.util.Collections;
24  import java.util.List;
25  
26  import org.apache.maven.settings.Proxy;
27  import org.apache.maven.settings.Server;
28  import org.apache.maven.settings.Settings;
29  import org.apache.maven.settings.SettingsUtils;
30  import org.apache.maven.settings.io.xpp3.SettingsXpp3Writer;
31  import org.apache.maven.shared.release.ReleaseResult;
32  import org.apache.maven.shared.release.env.ReleaseEnvironment;
33  import org.apache.maven.shared.release.util.MavenCrypto;
34  import org.apache.maven.shared.release.util.MavenCrypto.MavenCryptoException;
35  import org.codehaus.plexus.util.StringUtils;
36  import org.slf4j.Logger;
37  import org.slf4j.LoggerFactory;
38  
39  import static java.util.Objects.requireNonNull;
40  
41  /**
42   * <p>Abstract AbstractMavenExecutor class.</p>
43   */
44  public abstract class AbstractMavenExecutor implements MavenExecutor {
45      private final Logger logger = LoggerFactory.getLogger(getClass());
46  
47      private final MavenCrypto mavenCrypto;
48  
49      protected AbstractMavenExecutor(MavenCrypto mavenCrypto) {
50          this.mavenCrypto = requireNonNull(mavenCrypto);
51      }
52  
53      @Override
54      public void executeGoals(
55              File workingDirectory,
56              String goals,
57              ReleaseEnvironment releaseEnvironment,
58              boolean interactive,
59              String additionalArguments,
60              String pomFileName,
61              ReleaseResult result)
62              throws MavenExecutorException {
63          List<String> goalsList = new ArrayList<>();
64          if (goals != null) {
65              // accept both space and comma, so the old way still work
66              // also accept line separators, so that goal lists can be spread
67              // across multiple lines in the POM.
68              Collections.addAll(goalsList, StringUtils.split(goals, ", \n\r\t"));
69          }
70          executeGoals(
71                  workingDirectory, goalsList, releaseEnvironment, interactive, additionalArguments, pomFileName, result);
72      }
73  
74      protected abstract void executeGoals(
75              File workingDirectory,
76              List<String> goals,
77              ReleaseEnvironment releaseEnvironment,
78              boolean interactive,
79              String additionalArguments,
80              String pomFileName,
81              ReleaseResult result)
82              throws MavenExecutorException;
83  
84      /**
85       * <p>Getter for the field <code>logger</code>.</p>
86       *
87       * @return a {@link Logger} object
88       */
89      protected final Logger getLogger() {
90          return logger;
91      }
92  
93      /**
94       * <p>encryptSettings.</p>
95       *
96       * @param settings a {@link org.apache.maven.settings.Settings} object
97       * @return a {@link org.apache.maven.settings.Settings} object
98       */
99      protected Settings encryptSettings(Settings settings) {
100         Settings encryptedSettings = SettingsUtils.copySettings(settings);
101 
102         for (Server server : encryptedSettings.getServers()) {
103             String password = server.getPassword();
104             if (password != null && !mavenCrypto.isEncryptedString(password)) {
105                 try {
106                     server.setPassword(mavenCrypto.encryptAndDecorate(password));
107                 } catch (MavenCryptoException e) {
108                     // ignore
109                 }
110             }
111 
112             String passphrase = server.getPassphrase();
113             if (passphrase != null && !mavenCrypto.isEncryptedString(passphrase)) {
114                 try {
115                     server.setPassphrase(mavenCrypto.encryptAndDecorate(passphrase));
116                 } catch (MavenCryptoException e) {
117                     // ignore
118                 }
119             }
120         }
121 
122         for (Proxy proxy : encryptedSettings.getProxies()) {
123             String password = proxy.getPassword();
124             if (password != null && !mavenCrypto.isEncryptedString(password)) {
125                 try {
126                     proxy.setPassword(mavenCrypto.encryptAndDecorate(password));
127                 } catch (MavenCryptoException e) {
128                     // ignore
129                 }
130             }
131         }
132 
133         return encryptedSettings;
134     }
135 
136     /**
137      * <p>getSettingsWriter.</p>
138      *
139      * @return a {@link org.apache.maven.settings.io.xpp3.SettingsXpp3Writer} object
140      */
141     protected SettingsXpp3Writer getSettingsWriter() {
142         return new SettingsXpp3Writer();
143     }
144 }