Skip to content

Commit 22661b4

Browse files
author
Sébastien Latre
committed
Create rule for SCM tag (used by maven-release plugin)
1 parent e994562 commit 22661b4

4 files changed

Lines changed: 126 additions & 1 deletion

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ target
33
.classpath
44
.project
55
dependency-reduced-pom.xml
6+
/.idea

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@
9595
<dependency>
9696
<groupId>org.codehaus.mojo</groupId>
9797
<artifactId>versions-maven-plugin</artifactId>
98-
<version>2.1</version>
98+
<version>2.7</version>
9999
<type>maven-plugin</type>
100100
<exclusions>
101101
<exclusion>
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package de.oppermann.pomutils.rules;
2+
3+
import de.oppermann.pomutils.select.SelectionStrategy;
4+
import de.oppermann.pomutils.util.POM;
5+
import de.oppermann.pomutils.util.VersionFieldType;
6+
import org.slf4j.Logger;
7+
import org.slf4j.LoggerFactory;
8+
9+
/*
10+
* Licensed to the Apache Software Foundation (ASF) under one
11+
* or more contributor license agreements. See the NOTICE file
12+
* distributed with this work for additional information
13+
* regarding copyright ownership. The ASF licenses this file
14+
* to you under the Apache License, Version 2.0 (the
15+
* "License"); you may not use this file except in compliance
16+
* with the License. You may obtain a copy of the License at
17+
*
18+
* http://www.apache.org/licenses/LICENSE-2.0
19+
*
20+
* Unless required by applicable law or agreed to in writing,
21+
* software distributed under the License is distributed on an
22+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
23+
* KIND, either express or implied. See the License for the
24+
* specific language governing permissions and limitations
25+
* under the License.
26+
*/
27+
28+
/**
29+
*
30+
* @author Sébastien Latre
31+
*
32+
*/
33+
34+
public class ScmTagRule extends AbstractRule {
35+
36+
private final Logger logger = LoggerFactory.getLogger(ScmTagRule.class);
37+
38+
public ScmTagRule() {
39+
// for creating this instance via snakeyaml
40+
}
41+
42+
public ScmTagRule(SelectionStrategy strategy) {
43+
super(strategy);
44+
logger.debug("Using ScmTagRule with strategy [{}]", strategy.toString());
45+
}
46+
47+
@Override
48+
public void evaluate(POM basePom, POM ourPom, POM theirPom) {
49+
String baseScmTag = basePom.getScmTag();
50+
String ourScmTag = ourPom.getScmTag();
51+
String theirScmTag = theirPom.getScmTag();
52+
if (baseScmTag != null && ourScmTag != null && theirScmTag != null && !ourScmTag.equals(theirScmTag)) {
53+
String newScmTag;
54+
if (baseScmTag.equals(ourScmTag)) {
55+
/*
56+
* Our ScmTag hasn't changed, so no conflict. Just use theirScmTag.
57+
*/
58+
newScmTag = theirScmTag;
59+
} else if (baseScmTag.equals(theirScmTag)) {
60+
/*
61+
* Their scmTag hasn't changed, so no conflict. Just use ourScmTag.
62+
*/
63+
newScmTag = ourScmTag;
64+
} else {
65+
/*
66+
* Both our scmTag and their scmTag have changed from the base, so conflict.
67+
*/
68+
switch (getStrategy()) {
69+
case OUR:
70+
newScmTag = ourScmTag;
71+
break;
72+
case THEIR:
73+
newScmTag = theirScmTag;
74+
break;
75+
default:
76+
throw new IllegalArgumentException("Strategy [" + getStrategy().toString() + "] not implemented.");
77+
}
78+
}
79+
80+
if (newScmTag != null) {
81+
/*
82+
* newScmTag can be null if the user wants to skip resolution.
83+
*/
84+
85+
POM pomToAdjust = newScmTag.equals(ourScmTag)
86+
? theirPom
87+
: ourPom;
88+
89+
pomToAdjust.setScmTag(newScmTag);
90+
}
91+
}
92+
}
93+
}

src/main/java/de/oppermann/pomutils/util/POM.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ public class POM {
6565
private String projectVersion;
6666
private String parentVersion;
6767
private String projectIdentifier;
68+
private String scmTag;
6869
private Model rawModel;
6970

7071
public POM(String pomFileAsString) throws IOException, XMLStreamException {
@@ -82,6 +83,7 @@ public POM(String pomFileAsString) throws IOException, XMLStreamException {
8283
projectIdentifier = null;
8384
projectVersion = "";
8485
parentVersion = "";
86+
scmTag = "";
8587
return;
8688
}
8789

@@ -104,6 +106,9 @@ private void initialize() throws IOException, XMLStreamException {
104106
parentVersion = rawModel.getParent() != null
105107
? rawModel.getParent().getVersion()
106108
: null;
109+
scmTag = rawModel.getScm() != null
110+
? rawModel.getScm().getTag()
111+
: null;
107112
}
108113

109114
private String calculateProjectIdentifier() {
@@ -161,6 +166,14 @@ public String getProjectIdentifier() {
161166
return projectIdentifier;
162167
}
163168

169+
/**
170+
*
171+
* @return the SCM tag or null if the SCM tag is unset
172+
*/
173+
public String getScmTag() {
174+
return scmTag;
175+
}
176+
164177
/**
165178
* Sets the parent version to the given one, if it exists
166179
* @param newVersion
@@ -188,6 +201,20 @@ public void setProjectVersion(String newVersion) {
188201

189202
}
190203

204+
/**
205+
* Sets the SCM tag to the given one, if it exists
206+
* @param newScmTag
207+
*/
208+
public void setScmTag(String newScmTag) {
209+
if (this.scmTag == null || this.scmTag.equals(newScmTag)) {
210+
return;
211+
}
212+
logger.debug("Adjusting scm tag from [{}] to [{}] of [{}] for [{}]", this.scmTag, newScmTag, getPath(), this.projectIdentifier);
213+
this.scmTag = newScmTag;
214+
this.changed = true;
215+
216+
}
217+
191218
/**
192219
* Saves the pom, if it was changed.
193220
*/
@@ -204,6 +231,10 @@ public void savePom() throws IOException, XMLStreamException {
204231
changed |= PomHelper.setProjectParentVersion(pom, this.parentVersion);
205232
}
206233

234+
if (this.scmTag != null) {
235+
changed |= PomHelper.setProjectValue(pom, "/project/scm/tag", this.scmTag);
236+
}
237+
207238
if (!changed) {
208239
return;
209240
}

0 commit comments

Comments
 (0)