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.plugins.release;
20  
21  import java.io.File;
22  import java.util.Map;
23  
24  import org.apache.maven.artifact.ArtifactUtils;
25  import org.apache.maven.model.Scm;
26  import org.apache.maven.plugin.MojoExecutionException;
27  import org.apache.maven.plugin.MojoFailureException;
28  import org.apache.maven.plugins.annotations.Parameter;
29  import org.apache.maven.project.MavenProject;
30  import org.apache.maven.scm.manager.ScmManager;
31  import org.apache.maven.shared.release.ReleaseManager;
32  import org.apache.maven.shared.release.config.ReleaseDescriptorBuilder;
33  
34  /**
35   * Abstract Mojo containing SCM parameters for read operations.
36   */
37  public abstract class AbstractScmReadReleaseMojo extends AbstractReleaseMojo {
38  
39      /**
40       * The server id of the server which provides the credentials for the SCM in the <a href="https://maven.apache.org/settings.html">settings.xml</a> file.
41       * If not set the default lookup uses the SCM URL to construct the server id like this:
42       * {@code server-id=scm-host[":"scm-port]}.
43       * <p>
44       * Currently the POM does not allow to specify a server id for the SCM section.
45       * <p>
46       * Explicit authentication information provided via {@link #username}, {@link #password} or {@link #privateKey} will take precedence.
47       * @since 3.2.0
48       * @see <a href="https://maven.apache.org/scm/authentication.html">SCM Authentication</a>
49       */
50      @Parameter(property = "project.scm.id", defaultValue = "${project.scm.id}")
51      private String serverId;
52  
53      /**
54       * The username to use for authentication with the SCM.
55       * @see <a href="https://maven.apache.org/scm/authentication.html">SCM Authentication</a>
56       */
57      @Parameter(property = "username")
58      private String username;
59  
60      /**
61       * The password to use for authentication with the SCM.
62       * @see <a href="https://maven.apache.org/scm/authentication.html">SCM Authentication</a>
63       */
64      @Parameter(property = "password")
65      private String password;
66  
67      /**
68       * The path to the SSH private key to use for authentication with the SCM.
69       * @since 3.2.0
70       * @see <a href="https://maven.apache.org/scm/authentication.html">SCM Authentication</a>
71       */
72      @Parameter(property = "privateKey")
73      private File privateKey;
74  
75      /**
76       * Add a new or overwrite the default implementation per provider.
77       * The key is the scm prefix and the value is the role hint/provider id of the
78       * {@link org.apache.maven.scm.provider.ScmProvider}.
79       *
80       * @since 2.0-beta-6
81       * @see ScmManager#setScmProviderImplementation(String, String)
82       * @see <a href="https://maven.apache.org/scm/scms-overview.html">SCM Providers</a>
83       */
84      @Parameter
85      private Map<String, String> providerImplementations;
86  
87      /**
88       * When cloning a repository if it should be a shallow clone or a full clone.
89       */
90      @Parameter(defaultValue = "true", property = "scmShallowClone")
91      private boolean scmShallowClone = true;
92  
93      /**
94       * The SCM manager.
95       */
96      private final ScmManager scmManager;
97  
98      protected AbstractScmReadReleaseMojo(ReleaseManager releaseManager, ScmManager scmManager) {
99          super(releaseManager);
100         this.scmManager = scmManager;
101     }
102 
103     @Override
104     public void execute() throws MojoExecutionException, MojoFailureException {
105         if (providerImplementations != null) {
106             for (Map.Entry<String, String> providerEntry : providerImplementations.entrySet()) {
107                 getLog().info("Change the default '" + providerEntry.getKey() + "' provider implementation to '"
108                         + providerEntry.getValue() + "'.");
109                 scmManager.setScmProviderImplementation(providerEntry.getKey(), providerEntry.getValue());
110             }
111         }
112     }
113 
114     @Override
115     protected ReleaseDescriptorBuilder createReleaseDescriptor() {
116         ReleaseDescriptorBuilder descriptor = super.createReleaseDescriptor();
117 
118         if (privateKey != null) {
119             descriptor.setScmPrivateKey(privateKey.getAbsolutePath());
120         }
121         descriptor.setScmPassword(password);
122         descriptor.setScmUsername(username);
123         descriptor.setScmShallowClone(scmShallowClone);
124 
125         if (project.getScm() != null) {
126             if (project.getScm().getDeveloperConnection() != null) {
127                 descriptor.setScmSourceUrl(project.getScm().getDeveloperConnection());
128             } else if (project.getScm().getConnection() != null) {
129                 descriptor.setScmSourceUrl(project.getScm().getConnection());
130             }
131         }
132 
133         descriptor.setScmId(serverId);
134 
135         for (MavenProject reactorProject : session.getProjects()) {
136             if (reactorProject.getOriginalModel() != null
137                     && reactorProject.getOriginalModel().getScm() != null) {
138                 String projectId =
139                         ArtifactUtils.versionlessKey(reactorProject.getGroupId(), reactorProject.getArtifactId());
140 
141                 descriptor.addOriginalScmInfo(projectId, buildScm(reactorProject));
142             }
143         }
144 
145         return descriptor;
146     }
147 
148     /**
149      * <p>buildScm.</p>
150      *
151      * @param project a {@link org.apache.maven.project.MavenProject} object
152      * @return a {@link org.apache.maven.model.Scm} object
153      */
154     protected Scm buildScm(MavenProject project) {
155         Scm scm;
156         if (project.getOriginalModel().getScm() == null) {
157             scm = null;
158         } else {
159             scm = new Scm();
160             scm.setConnection(project.getOriginalModel().getScm().getConnection());
161             scm.setDeveloperConnection(project.getOriginalModel().getScm().getDeveloperConnection());
162             scm.setTag(project.getOriginalModel().getScm().getTag());
163             scm.setUrl(project.getOriginalModel().getScm().getUrl());
164         }
165         return scm;
166     }
167 }