Skip to content

Commit 7f60b4e

Browse files
committed
Add "serverId" and "privateKey" SCM parameters
Separate abstract classes for read and write SCM operation This closes #1384
1 parent e88cec2 commit 7f60b4e

File tree

6 files changed

+151
-136
lines changed

6 files changed

+151
-136
lines changed

maven-release-plugin/src/main/java/org/apache/maven/plugins/release/AbstractScmReleaseMojo.java renamed to maven-release-plugin/src/main/java/org/apache/maven/plugins/release/AbstractScmReadReleaseMojo.java

Lines changed: 36 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@
1818
*/
1919
package org.apache.maven.plugins.release;
2020

21-
import javax.inject.Inject;
22-
21+
import java.io.File;
2322
import java.util.Map;
2423

2524
import org.apache.maven.artifact.ArtifactUtils;
@@ -33,83 +32,41 @@
3332
import org.apache.maven.shared.release.config.ReleaseDescriptorBuilder;
3433

3534
/**
36-
* Abstract Mojo containing SCM parameters
37-
*
38-
* @author Robert Scholte
35+
* Abstract Mojo containing SCM parameters for read operations.
3936
*/
40-
// Extra layer since 2.4. Don't use @since doclet, these would be inherited by the subclasses
41-
public abstract class AbstractScmReleaseMojo extends AbstractReleaseMojo {
42-
/**
43-
* The SCM username to use.
44-
*/
45-
@Parameter(property = "username")
46-
private String username;
47-
48-
/**
49-
* The SCM password to use.
50-
*/
51-
@Parameter(property = "password")
52-
private String password;
53-
54-
/**
55-
* The SCM tag to use.
56-
*/
57-
@Parameter(alias = "releaseLabel", property = "tag")
58-
private String tag;
59-
60-
/**
61-
* Format to use when generating the tag name if none is specified. Property interpolation is performed on the
62-
* tag, but in order to ensure that the interpolation occurs during release, you must use <code>@{...}</code>
63-
* to reference the properties rather than <code>${...}</code>. The following properties are available:
64-
* <ul>
65-
* <li><code>groupId</code> or <code>project.groupId</code> - The groupId of the root project.
66-
* <li><code>artifactId</code> or <code>project.artifactId</code> - The artifactId of the root project.
67-
* <li><code>version</code> or <code>project.version</code> - The release version of the root project.
68-
* </ul>
69-
*
70-
* @since 2.2.0
71-
*/
72-
@Parameter(defaultValue = "@{project.artifactId}-@{project.version}", property = "tagNameFormat")
73-
private String tagNameFormat;
74-
75-
/**
76-
* The tag base directory in SVN, you must define it if you don't use the standard svn layout (trunk/tags/branches).
77-
* For example, <code>http://svn.apache.org/repos/asf/maven/plugins/tags</code>. The URL is an SVN URL and does not
78-
* include the SCM provider and protocol.
79-
*/
80-
@Parameter(property = "tagBase")
81-
private String tagBase;
37+
public abstract class AbstractScmReadReleaseMojo extends AbstractReleaseMojo {
8238

8339
/**
84-
* The message prefix to use for all SCM changes.
85-
*
86-
* @since 2.0-beta-5
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
8748
*/
88-
@Parameter(defaultValue = "[maven-release-plugin] ", property = "scmCommentPrefix")
89-
private String scmCommentPrefix;
49+
@Parameter(property = "project.scm.id", defaultValue = "${project.scm.id}")
50+
private String serverId;
9051

9152
/**
92-
* When cloning a repository if it should be a shallow clone or a full clone.
53+
* The username to use for authentication with the SCM.
9354
*/
94-
@Parameter(defaultValue = "true", property = "scmShallowClone")
95-
private boolean scmShallowClone = true;
55+
@Parameter(property = "username")
56+
private String username;
9657

9758
/**
98-
* Implemented with git will or not push changes to the upstream repository.
99-
* <code>true</code> by default to preserve backward compatibility.
100-
* @since 2.1
59+
* The password to use for authentication with the SCM.
10160
*/
102-
@Parameter(defaultValue = "true", property = "pushChanges")
103-
private boolean pushChanges = true;
61+
@Parameter(property = "password")
62+
private String password;
10463

10564
/**
106-
* A workItem for SCMs like RTC, TFS etc, that may require additional
107-
* information to perform a pushChange operation.
108-
*
109-
* @since 3.0.0-M5
65+
* The path to the SSH private key to use for authentication with the SCM.
66+
* @since 3.2.0
11067
*/
111-
@Parameter(property = "workItem")
112-
private String workItem;
68+
@Parameter(property = "privateKey")
69+
private File privateKey;
11370

11471
/**
11572
* Add a new or overwrite the default implementation per provider.
@@ -122,13 +79,18 @@ public abstract class AbstractScmReleaseMojo extends AbstractReleaseMojo {
12279
@Parameter
12380
private Map<String, String> providerImplementations;
12481

82+
/**
83+
* When cloning a repository if it should be a shallow clone or a full clone.
84+
*/
85+
@Parameter(defaultValue = "true", property = "scmShallowClone")
86+
private boolean scmShallowClone = true;
87+
12588
/**
12689
* The SCM manager.
12790
*/
12891
private final ScmManager scmManager;
12992

130-
@Inject
131-
protected AbstractScmReleaseMojo(ReleaseManager releaseManager, ScmManager scmManager) {
93+
protected AbstractScmReadReleaseMojo(ReleaseManager releaseManager, ScmManager scmManager) {
13294
super(releaseManager);
13395
this.scmManager = scmManager;
13496
}
@@ -148,17 +110,13 @@ public void execute() throws MojoExecutionException, MojoFailureException {
148110
protected ReleaseDescriptorBuilder createReleaseDescriptor() {
149111
ReleaseDescriptorBuilder descriptor = super.createReleaseDescriptor();
150112

113+
if (privateKey != null) {
114+
descriptor.setScmPrivateKey(privateKey.getAbsolutePath());
115+
}
151116
descriptor.setScmPassword(password);
152-
descriptor.setScmReleaseLabel(tag);
153-
descriptor.setScmTagNameFormat(tagNameFormat);
154-
descriptor.setScmTagBase(tagBase);
155117
descriptor.setScmUsername(username);
156-
descriptor.setScmCommentPrefix(scmCommentPrefix);
157118
descriptor.setScmShallowClone(scmShallowClone);
158119

159-
descriptor.setPushChanges(pushChanges);
160-
descriptor.setWorkItem(workItem);
161-
162120
if (project.getScm() != null) {
163121
if (project.getScm().getDeveloperConnection() != null) {
164122
descriptor.setScmSourceUrl(project.getScm().getDeveloperConnection());
@@ -167,11 +125,11 @@ protected ReleaseDescriptorBuilder createReleaseDescriptor() {
167125
}
168126
}
169127

170-
// As long as Scm.getId() does not exist, read it as a property
171-
descriptor.setScmId(project.getProperties().getProperty("project.scm.id"));
128+
descriptor.setScmId(serverId);
172129

173130
for (MavenProject reactorProject : session.getProjects()) {
174-
if (reactorProject.getScm() != null) {
131+
if (reactorProject.getOriginalModel() != null
132+
&& reactorProject.getOriginalModel().getScm() != null) {
175133
String projectId =
176134
ArtifactUtils.versionlessKey(reactorProject.getGroupId(), reactorProject.getArtifactId());
177135

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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 javax.inject.Inject;
22+
23+
import org.apache.maven.plugins.annotations.Parameter;
24+
import org.apache.maven.scm.manager.ScmManager;
25+
import org.apache.maven.shared.release.ReleaseManager;
26+
import org.apache.maven.shared.release.config.ReleaseDescriptorBuilder;
27+
28+
/**
29+
* Abstract Mojo containing SCM parameters for read/write operations.
30+
*
31+
* @author Robert Scholte
32+
*/
33+
// Extra layer since 2.4. Don't use @since doclet, these would be inherited by the subclasses
34+
public abstract class AbstractScmReadWriteReleaseMojo extends AbstractScmReadReleaseMojo {
35+
/**
36+
* The SCM tag to use.
37+
*/
38+
@Parameter(alias = "releaseLabel", property = "tag")
39+
private String tag;
40+
41+
/**
42+
* Format to use when generating the tag name if none is specified. Property interpolation is performed on the
43+
* tag, but in order to ensure that the interpolation occurs during release, you must use <code>@{...}</code>
44+
* to reference the properties rather than <code>${...}</code>. The following properties are available:
45+
* <ul>
46+
* <li><code>groupId</code> or <code>project.groupId</code> - The groupId of the root project.
47+
* <li><code>artifactId</code> or <code>project.artifactId</code> - The artifactId of the root project.
48+
* <li><code>version</code> or <code>project.version</code> - The release version of the root project.
49+
* </ul>
50+
*
51+
* @since 2.2.0
52+
*/
53+
@Parameter(defaultValue = "@{project.artifactId}-@{project.version}", property = "tagNameFormat")
54+
private String tagNameFormat;
55+
56+
/**
57+
* The tag base directory in SVN, you must define it if you don't use the standard svn layout (trunk/tags/branches).
58+
* For example, <code>http://svn.apache.org/repos/asf/maven/plugins/tags</code>. The URL is an SVN URL and does not
59+
* include the SCM provider and protocol.
60+
*/
61+
@Parameter(property = "tagBase")
62+
private String tagBase;
63+
64+
/**
65+
* The message prefix to use for all SCM changes.
66+
*
67+
* @since 2.0-beta-5
68+
*/
69+
@Parameter(defaultValue = "[maven-release-plugin] ", property = "scmCommentPrefix")
70+
private String scmCommentPrefix;
71+
72+
/**
73+
* Implemented with git will or not push changes to the upstream repository.
74+
* <code>true</code> by default to preserve backward compatibility.
75+
* @since 2.1
76+
*/
77+
@Parameter(defaultValue = "true", property = "pushChanges")
78+
private boolean pushChanges = true;
79+
80+
/**
81+
* A workItem for SCMs like RTC, TFS etc, that may require additional
82+
* information to perform a pushChange operation.
83+
*
84+
* @since 3.0.0-M5
85+
*/
86+
@Parameter(property = "workItem")
87+
private String workItem;
88+
89+
@Inject
90+
protected AbstractScmReadWriteReleaseMojo(ReleaseManager releaseManager, ScmManager scmManager) {
91+
super(releaseManager, scmManager);
92+
}
93+
94+
@Override
95+
protected ReleaseDescriptorBuilder createReleaseDescriptor() {
96+
ReleaseDescriptorBuilder descriptor = super.createReleaseDescriptor();
97+
98+
// extend the descriptor with SCM parameter relevant for write operations
99+
descriptor.setScmReleaseLabel(tag);
100+
descriptor.setScmTagNameFormat(tagNameFormat);
101+
descriptor.setScmTagBase(tagBase);
102+
descriptor.setScmCommentPrefix(scmCommentPrefix);
103+
104+
descriptor.setPushChanges(pushChanges);
105+
descriptor.setWorkItem(workItem);
106+
107+
return descriptor;
108+
}
109+
}

maven-release-plugin/src/main/java/org/apache/maven/plugins/release/BranchReleaseMojo.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
* @since 2.0-beta-6
4545
*/
4646
@Mojo(name = "branch", aggregator = true)
47-
public class BranchReleaseMojo extends AbstractScmReleaseMojo {
47+
public class BranchReleaseMojo extends AbstractScmReadWriteReleaseMojo {
4848
/**
4949
* The branch name to use.
5050
*

maven-release-plugin/src/main/java/org/apache/maven/plugins/release/PerformReleaseMojo.java

Lines changed: 3 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import javax.inject.Inject;
2222

2323
import java.io.File;
24-
import java.util.Map;
2524

2625
import org.apache.maven.plugin.MojoExecutionException;
2726
import org.apache.maven.plugin.MojoFailureException;
@@ -46,7 +45,7 @@
4645
* @author <a href="mailto:brett@apache.org">Brett Porter</a>
4746
*/
4847
@Mojo(name = "perform", aggregator = true, requiresProject = false)
49-
public class PerformReleaseMojo extends AbstractReleaseMojo {
48+
public class PerformReleaseMojo extends AbstractScmReadReleaseMojo {
5049
/**
5150
* A space separated list of goals to execute on release perform. Default value is either <code>deploy</code> or
5251
* <code>deploy site-deploy</code>, if the project has a &lt;distributionManagement&gt;/&lt;site&gt; element.
@@ -87,24 +86,6 @@ public class PerformReleaseMojo extends AbstractReleaseMojo {
8786
@Parameter(defaultValue = "false", property = "localCheckout")
8887
private boolean localCheckout;
8988

90-
/**
91-
* The SCM username to use.
92-
*/
93-
@Parameter(property = "username")
94-
private String username;
95-
96-
/**
97-
* The SCM password to use.
98-
*/
99-
@Parameter(property = "password")
100-
private String password;
101-
102-
/**
103-
* When cloning a repository if it should be a shallow clone or a full clone.
104-
*/
105-
@Parameter(defaultValue = "true", property = "scmShallowClone")
106-
private boolean scmShallowClone = true;
107-
10889
/**
10990
* Whether to use the default release profile (Maven 2 and 3) that adds sources and javadocs to the released
11091
* artifact, if appropriate. If set to true, the release plugin sets the property "<code>performRelease</code>" to
@@ -124,26 +105,9 @@ public class PerformReleaseMojo extends AbstractReleaseMojo {
124105
@Parameter(defaultValue = "false", property = "dryRun")
125106
private boolean dryRun;
126107

127-
/**
128-
* Add a new or overwrite the default implementation per provider.
129-
* The key is the scm prefix and the value is the role hint of the
130-
* {@link org.apache.maven.scm.provider.ScmProvider}.
131-
*
132-
* @since 2.5.3
133-
* @see ScmManager#setScmProviderImplementation(String, String)
134-
*/
135-
@Parameter
136-
private Map<String, String> providerImplementations;
137-
138-
/**
139-
* The SCM manager.
140-
*/
141-
private final ScmManager scmManager;
142-
143108
@Inject
144109
public PerformReleaseMojo(ReleaseManager releaseManager, ScmManager scmManager) {
145-
super(releaseManager);
146-
this.scmManager = scmManager;
110+
super(releaseManager, scmManager);
147111
}
148112

149113
@Override
@@ -153,13 +117,7 @@ protected String getAdditionalProfiles() {
153117

154118
@Override
155119
public void execute() throws MojoExecutionException, MojoFailureException {
156-
if (providerImplementations != null) {
157-
for (Map.Entry<String, String> providerEntry : providerImplementations.entrySet()) {
158-
getLog().info("Change the default '" + providerEntry.getKey() + "' provider implementation to '"
159-
+ providerEntry.getValue() + "'.");
160-
scmManager.setScmProviderImplementation(providerEntry.getKey(), providerEntry.getValue());
161-
}
162-
}
120+
super.execute();
163121

164122
// goals may be split into multiple lines in configuration.
165123
// Let's build a single line command
@@ -175,16 +133,6 @@ public void execute() throws MojoExecutionException, MojoFailureException {
175133
releaseDescriptor.setScmSourceUrl(connectionUrl);
176134
}
177135

178-
if (username != null) {
179-
releaseDescriptor.setScmUsername(username);
180-
}
181-
182-
if (password != null) {
183-
releaseDescriptor.setScmPassword(password);
184-
}
185-
186-
releaseDescriptor.setScmShallowClone(scmShallowClone);
187-
188136
releaseDescriptor.setLocalCheckout(localCheckout);
189137

190138
releaseDescriptor.setCheckoutDirectory(workingDirectory.getAbsolutePath());

0 commit comments

Comments
 (0)