Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MRELEASE-1109] Snapshot detection and support for versions like ${revision}${sha1}${changelist} #202

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
MRELEASE-1109 Using scmRevision as sha1 value if the property defined…
… but nothing was provided by a user
  • Loading branch information
mkolesnikov committed Jun 5, 2024
commit 56fe3ed2d4d9df18d237cbecaef14365b00e1798
Original file line number Diff line number Diff line change
@@ -31,7 +31,7 @@
import org.apache.maven.model.Profile;
import org.apache.maven.model.Reporting;
import org.apache.maven.model.Scm;
import org.apache.maven.project.MavenProject;
import org.apache.maven.shared.release.config.ReleaseDescriptor;
import org.apache.maven.shared.release.util.CiFriendlyVersion;
import org.jdom2.Document;
import org.jdom2.Element;
@@ -49,27 +49,28 @@ public class JDomModel extends Model {
private final JDomModelBase modelBase;

/**
* The currently running MavenProject
* The ReleaseDescriptor after a commit performed
*
*/
private final MavenProject mavenProject;
private final ReleaseDescriptor releaseDescriptor;

/**
* <p>Constructor for JDomModel.</p>
*
* @param document a {@link org.jdom2.Document} object
*/
public JDomModel(Document document, MavenProject mavenProject) {
this(document.getRootElement(), mavenProject);
public JDomModel(Document document, ReleaseDescriptor releaseDescriptor) {
this(document.getRootElement(), releaseDescriptor);
}

/**
* <p>Constructor for JDomModel.</p>
*
* @param project a {@link org.jdom2.Element} object
*/
public JDomModel(Element project, MavenProject mavenProject) {
public JDomModel(Element project, ReleaseDescriptor releaseDescriptor) {
this.project = project;
this.mavenProject = mavenProject;
this.releaseDescriptor = releaseDescriptor;
this.modelBase = new JDomModelBase(project);
}

@@ -205,7 +206,7 @@ public void setVersion(String version) {
version,
versionElement.getTextNormalize(),
(JDomProperties) getProperties(),
mavenProject.getProperties());
releaseDescriptor);
} else {
JDomUtils.rewriteValue(versionElement, version);
}
Original file line number Diff line number Diff line change
@@ -159,7 +159,7 @@ public void load(File targetFile) throws ReleaseExecutionException {

@Override
public Model getModel() {
return new JDomModel(document, project);
return new JDomModel(document, releaseDescriptor);
}

private void normaliseLineEndings(Document document) {
Original file line number Diff line number Diff line change
@@ -20,12 +20,12 @@

import java.util.Arrays;
import java.util.HashSet;
import java.util.Properties;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.maven.artifact.ArtifactUtils;
import org.apache.maven.shared.release.config.ReleaseDescriptor;
import org.apache.maven.shared.release.transform.jdom2.JDomProperties;

public class CiFriendlyVersion {
@@ -72,11 +72,11 @@ public static boolean containsCiFriendlyProperties(String property) {
}

public static void rewriteVersionAndProperties(
String version, String versionElement, JDomProperties jDomProperties, Properties mavenProperties) {
String version, String versionElement, JDomProperties jDomProperties, ReleaseDescriptor releaseDescriptor) {
// try to rewrite property if CI friendly expression is used
michael-o marked this conversation as resolved.
Show resolved Hide resolved
String ciFriendlyPropertyName = extractPropertyFromExpression(versionElement);
if (jDomProperties != null) {
String sha1 = jDomProperties.getProperty(SHA1, mavenProperties.getProperty(SHA1, ""));
String sha1 = resolveSha1Property(jDomProperties, releaseDescriptor);
// assume that everybody follows the example and properties are simply chained
// and the changelist can only be '-SNAPSHOT'
if (ArtifactUtils.isSnapshot(version)) {
@@ -87,13 +87,29 @@ public static void rewriteVersionAndProperties(
} else {
jDomProperties.setProperty(ciFriendlyPropertyName, version.replace(sha1, ""));
}
if (jDomProperties.containsKey(SHA1)) {
// drop the value for the next version
jDomProperties.setProperty(SHA1, "");
}
} else {
jDomProperties.setProperty(
ciFriendlyPropertyName, version.replace(sha1, "").replace(SNAPSHOT, ""));
if (jDomProperties.containsKey(CHANGELIST)) {
jDomProperties.setProperty(CHANGELIST, "");
}
if (jDomProperties.containsKey(SHA1) && !sha1.isEmpty()) {
// we need this to restore the revision for the next development
// or release:prepare should provide sha1 after a commit
// or a user should provide it as an additional `arguments` in plugin configuration
// see maven-release-plugin/src/it/projects/prepare/ci-friendly-multi-module
jDomProperties.setProperty(SHA1, sha1);
}
}
}
}

private static String resolveSha1Property(JDomProperties jDomProperties, ReleaseDescriptor releaseDescriptor) {
String scmVersion = releaseDescriptor.getScmReleasedPomRevision();
return jDomProperties.getProperty(SHA1, System.getProperty(SHA1, scmVersion == null ? "" : scmVersion));
}
}
Original file line number Diff line number Diff line change
@@ -22,7 +22,8 @@

import org.apache.maven.model.Model;
import org.apache.maven.model.Scm;
import org.apache.maven.project.MavenProject;
import org.apache.maven.shared.release.config.ReleaseDescriptor;
import org.apache.maven.shared.release.config.ReleaseDescriptorBuilder;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.input.SAXBuilder;
@@ -34,20 +35,20 @@

public class JDomModelTest {
private SAXBuilder builder = new SAXBuilder();
private MavenProject mavenProject = new MavenProject();
private ReleaseDescriptor releaseDescriptor = new ReleaseDescriptorBuilder().build();

@Test
public void testGetScm() throws Exception {
String content = "<project></project>";
Document document = builder.build(new StringReader(content));
assertNull(new JDomModel(document, mavenProject).getScm());
assertNull(new JDomModel(document, releaseDescriptor).getScm());
}

@Test
public void testSetScm() throws Exception {
String content = "<project></project>";
Document document = builder.build(new StringReader(content));
Model model = new JDomModel(document, mavenProject);
Model model = new JDomModel(document, releaseDescriptor);
assertNull(model.getScm());

model.setScm(new Scm());
@@ -61,7 +62,7 @@ public void testSetScm() throws Exception {
public void testSetVersion() throws Exception {
String content = "<project></project>";
Element projectElm = builder.build(new StringReader(content)).getRootElement();
Model model = new JDomModel(projectElm, mavenProject);
Model model = new JDomModel(projectElm, releaseDescriptor);
assertNull(model.getVersion());

model.setVersion("VERSION");
@@ -73,15 +74,15 @@ public void testSetVersion() throws Exception {
// inherit from parent via CI friendly
content = "<project><parent><version>${revision}${changelist}</version></parent></project>";
projectElm = builder.build(new StringReader(content)).getRootElement();
model = new JDomModel(projectElm, mavenProject);
model = new JDomModel(projectElm, releaseDescriptor);
assertNull(model.getVersion());
model.setVersion("PARENT_VERSION");
assertNull(getVersion(projectElm));

// this business logic might need to moved.
content = "<project><parent><version>PARENT_VERSION</version></parent></project>";
projectElm = builder.build(new StringReader(content)).getRootElement();
model = new JDomModel(projectElm, mavenProject);
model = new JDomModel(projectElm, releaseDescriptor);
assertNull(model.getVersion());

model.setVersion("PARENT_VERSION");
Original file line number Diff line number Diff line change
@@ -16,4 +16,4 @@
# under the License.

invoker.goals=release:clean release:prepare
invoker.mavenOpts=-Dsha1=-123
invoker.mavenOpts=-Dsha1=-abcdef12 -agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=*:5005
Original file line number Diff line number Diff line change
@@ -26,7 +26,7 @@ assert buildLog.exists()
def projectRootTag = new XmlSlurper().parse( new File( basedir, 'pom.xml.tag' ) )
assert projectRootTag.version.text() == '${revision}${sha1}${changelist}'
assert projectRootTag.properties.revision.text() == "1.0"
assert projectRootTag.properties.sha1.text() == ""
assert projectRootTag.properties.sha1.text() == "-abcdef12"
assert projectRootTag.properties.changelist.text() == ""

def projectATag = new XmlSlurper().parse( new File( basedir, 'module-a/pom.xml.tag' ) )