From 21df8de6142d56393073957dcb1296f27924f29c Mon Sep 17 00:00:00 2001 From: Mikhail Kolesnikov Date: Wed, 29 Nov 2023 19:47:06 +0300 Subject: [PATCH 01/15] MRELEASE-1109 full support of CI Friendly expression --- .../release/phase/AbstractCheckPomPhase.java | 4 +- .../phase/AbstractRewritePomsPhase.java | 11 ++- .../release/transform/jdom2/JDomModel.java | 2 +- .../shared/release/util/MavenExpression.java | 44 ++++++++++++ .../RewritePomsForDevelopmentPhaseTest.java | 15 ++++ .../transform/jdom2/JDomModelTest.java | 2 +- .../release/util/MavenExpressionTest.java | 61 ++++++++++++++++ .../expected-pom.xml | 40 +++++++++++ .../pom.xml | 40 +++++++++++ .../subproject1/expected-pom.xml | 28 ++++++++ .../subproject1/pom.xml | 28 ++++++++ .../expected-pom.xml | 5 +- .../pom.xml | 5 +- .../subproject1/expected-pom.xml | 2 +- .../subproject1/pom.xml | 2 +- maven-release-plugin/pom.xml | 8 +-- .../ci-friendly-multi-module/module-a/pom.xml | 39 ++++++++++ .../maven/plugin/release/module/a/App.java | 32 +++++++++ .../plugin/release/module/a/AppTest.java | 57 +++++++++++++++ .../ci-friendly-multi-module/module-b/pom.xml | 39 ++++++++++ .../maven/plugin/release/module/b/App.java | 32 +++++++++ .../plugin/release/module/b/AppTest.java | 57 +++++++++++++++ .../prepare/ci-friendly-multi-module/pom.xml | 72 +++++++++++++++++++ .../ci-friendly-multi-module/verify.groovy | 45 ++++++++++++ .../it/projects/prepare/invoker.properties | 4 +- 25 files changed, 655 insertions(+), 19 deletions(-) create mode 100644 maven-release-manager/src/main/java/org/apache/maven/shared/release/util/MavenExpression.java create mode 100644 maven-release-manager/src/test/java/org/apache/maven/shared/release/util/MavenExpressionTest.java create mode 100644 maven-release-manager/src/test/resources/projects/rewrite-for-development/pom-with-parent-and-cifriendly-expressions/expected-pom.xml create mode 100644 maven-release-manager/src/test/resources/projects/rewrite-for-development/pom-with-parent-and-cifriendly-expressions/pom.xml create mode 100644 maven-release-manager/src/test/resources/projects/rewrite-for-development/pom-with-parent-and-cifriendly-expressions/subproject1/expected-pom.xml create mode 100644 maven-release-manager/src/test/resources/projects/rewrite-for-development/pom-with-parent-and-cifriendly-expressions/subproject1/pom.xml create mode 100644 maven-release-plugin/src/it/projects/prepare/ci-friendly-multi-module/module-a/pom.xml create mode 100644 maven-release-plugin/src/it/projects/prepare/ci-friendly-multi-module/module-a/src/main/java/org/apache/maven/plugin/release/module/a/App.java create mode 100644 maven-release-plugin/src/it/projects/prepare/ci-friendly-multi-module/module-a/src/test/java/org/apache/maven/plugin/release/module/a/AppTest.java create mode 100644 maven-release-plugin/src/it/projects/prepare/ci-friendly-multi-module/module-b/pom.xml create mode 100644 maven-release-plugin/src/it/projects/prepare/ci-friendly-multi-module/module-b/src/main/java/org/apache/maven/plugin/release/module/b/App.java create mode 100644 maven-release-plugin/src/it/projects/prepare/ci-friendly-multi-module/module-b/src/test/java/org/apache/maven/plugin/release/module/b/AppTest.java create mode 100644 maven-release-plugin/src/it/projects/prepare/ci-friendly-multi-module/pom.xml create mode 100644 maven-release-plugin/src/it/projects/prepare/ci-friendly-multi-module/verify.groovy diff --git a/maven-release-manager/src/main/java/org/apache/maven/shared/release/phase/AbstractCheckPomPhase.java b/maven-release-manager/src/main/java/org/apache/maven/shared/release/phase/AbstractCheckPomPhase.java index fed385048..7b24d39a4 100644 --- a/maven-release-manager/src/main/java/org/apache/maven/shared/release/phase/AbstractCheckPomPhase.java +++ b/maven-release-manager/src/main/java/org/apache/maven/shared/release/phase/AbstractCheckPomPhase.java @@ -31,6 +31,7 @@ import org.apache.maven.shared.release.env.ReleaseEnvironment; import org.apache.maven.shared.release.scm.ReleaseScmRepositoryException; import org.apache.maven.shared.release.scm.ScmRepositoryConfigurator; +import org.apache.maven.shared.release.util.MavenExpression; import org.codehaus.plexus.util.StringUtils; import static java.util.Objects.requireNonNull; @@ -89,7 +90,8 @@ public ReleaseResult execute( boolean containsSnapshotProjects = false; for (MavenProject project : reactorProjects) { - if (ArtifactUtils.isSnapshot(project.getVersion())) { + String projectVersion = MavenExpression.evaluate(project.getVersion(), project.getProperties()); + if (ArtifactUtils.isSnapshot(projectVersion)) { containsSnapshotProjects = true; break; diff --git a/maven-release-manager/src/main/java/org/apache/maven/shared/release/phase/AbstractRewritePomsPhase.java b/maven-release-manager/src/main/java/org/apache/maven/shared/release/phase/AbstractRewritePomsPhase.java index 76747c563..3065ccc62 100644 --- a/maven-release-manager/src/main/java/org/apache/maven/shared/release/phase/AbstractRewritePomsPhase.java +++ b/maven-release-manager/src/main/java/org/apache/maven/shared/release/phase/AbstractRewritePomsPhase.java @@ -96,7 +96,7 @@ public abstract class AbstractRewritePomsPhase extends AbstractReleasePhase impl * Regular expression pattern matching Maven expressions (i.e. references to Maven properties). * The first group selects the property name the expression refers to. */ - private static final Pattern EXPRESSION_PATTERN = Pattern.compile("\\$\\{(.+)\\}"); + private static final Pattern EXPRESSION_PATTERN = Pattern.compile("\\$\\{(.+?)\\}"); /** * All Maven properties allowed to be referenced in parent versions via expressions @@ -462,7 +462,7 @@ private void rewriteVersion( */ public static String extractPropertyFromExpression(String expression) { Matcher matcher = EXPRESSION_PATTERN.matcher(expression); - if (!matcher.matches()) { + if (!matcher.find()) { return null; } return matcher.group(1); @@ -565,6 +565,7 @@ private void rewriteArtifactVersions( coordinate.setVersion(mappedVersion); } else { String property = extractPropertyFromExpression(rawVersion); + logInfo(result, "CI Friendly property " + property + " and rawVersion is " + rawVersion); if (property != null) { if (property.startsWith("project.") || property.startsWith("pom.") @@ -610,6 +611,8 @@ private void rewriteArtifactVersions( } } else { if (CI_FRIENDLY_PROPERTIES.contains(property)) { + // the parent's pom revision is set inside + // org.apache.maven.shared.release.transform.jdom2.JDomModel.setVersion logInfo( result, " Ignoring artifact version update for CI friendly expression " @@ -629,6 +632,10 @@ private void rewriteArtifactVersions( } } else { // different/previous version not related to current release + // this is the only place where the returned null from `extractPropertyFromExpression` is + // supposed to be handled. + // And the unit test RewritePomsForBranchPhaseTest depends on null result from + // `extractPropertyFromExpression`. } } } else if (resolvedSnapshotVersion != null) { diff --git a/maven-release-manager/src/main/java/org/apache/maven/shared/release/transform/jdom2/JDomModel.java b/maven-release-manager/src/main/java/org/apache/maven/shared/release/transform/jdom2/JDomModel.java index 6198ee73d..20d972bd9 100644 --- a/maven-release-manager/src/main/java/org/apache/maven/shared/release/transform/jdom2/JDomModel.java +++ b/maven-release-manager/src/main/java/org/apache/maven/shared/release/transform/jdom2/JDomModel.java @@ -198,7 +198,7 @@ public void setVersion(String version) { AbstractRewritePomsPhase.extractPropertyFromExpression(versionElement.getTextNormalize()); Properties properties = getProperties(); if (properties != null) { - properties.computeIfPresent(ciFriendlyPropertyName, (k, v) -> version); + properties.setProperty(ciFriendlyPropertyName, version); } } else { JDomUtils.rewriteValue(versionElement, version); diff --git a/maven-release-manager/src/main/java/org/apache/maven/shared/release/util/MavenExpression.java b/maven-release-manager/src/main/java/org/apache/maven/shared/release/util/MavenExpression.java new file mode 100644 index 000000000..6986cdb86 --- /dev/null +++ b/maven-release-manager/src/main/java/org/apache/maven/shared/release/util/MavenExpression.java @@ -0,0 +1,44 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.maven.shared.release.util; + +import java.util.Properties; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/** + * @author Mikhail Kolesnikov + */ +public class MavenExpression { + public static final Pattern EXPRESSION_PATTERN = Pattern.compile("\\$\\{(.+?)\\}"); + + private MavenExpression() {} + + public static String evaluate(String expression, Properties properties) { + StringBuilder result = new StringBuilder(expression); + Matcher matcher = EXPRESSION_PATTERN.matcher(result); + while (matcher.find()) { + String propertyName = matcher.group(1); + String propertyValue = properties.getProperty(propertyName); + result.replace(matcher.start(), matcher.end(), String.valueOf(propertyValue)); + matcher.reset(); + } + return result.toString(); + } +} diff --git a/maven-release-manager/src/test/java/org/apache/maven/shared/release/phase/RewritePomsForDevelopmentPhaseTest.java b/maven-release-manager/src/test/java/org/apache/maven/shared/release/phase/RewritePomsForDevelopmentPhaseTest.java index b303a514f..b1bebb81c 100644 --- a/maven-release-manager/src/test/java/org/apache/maven/shared/release/phase/RewritePomsForDevelopmentPhaseTest.java +++ b/maven-release-manager/src/test/java/org/apache/maven/shared/release/phase/RewritePomsForDevelopmentPhaseTest.java @@ -398,4 +398,19 @@ public void testRewritePomDependenciesWithoutDependenciesVersionUpdate() throws assertTrue(comparePomFiles(reactorProjects)); } + + @Test + public void testRewritePomWithCiFriendlyReactor() throws Exception { + List reactorProjects = createReactorProjects("pom-with-parent-and-cifriendly-expressions"); + + ReleaseDescriptorBuilder builder = + createDescriptorFromProjects(reactorProjects, "pom-with-parent-and-cifriendly-expressions"); + builder.addReleaseVersion("groupId:artifactId", RELEASE_VERSION); + builder.addDevelopmentVersion("groupId:artifactId", NEXT_VERSION); + builder.addReleaseVersion("groupId:subproject1", RELEASE_VERSION); + builder.addDevelopmentVersion("groupId:subproject1", NEXT_VERSION); + phase.execute(ReleaseUtils.buildReleaseDescriptor(builder), new DefaultReleaseEnvironment(), reactorProjects); + + assertTrue(comparePomFiles(reactorProjects)); + } } diff --git a/maven-release-manager/src/test/java/org/apache/maven/shared/release/transform/jdom2/JDomModelTest.java b/maven-release-manager/src/test/java/org/apache/maven/shared/release/transform/jdom2/JDomModelTest.java index ebd0ec282..abe03d6cc 100644 --- a/maven-release-manager/src/test/java/org/apache/maven/shared/release/transform/jdom2/JDomModelTest.java +++ b/maven-release-manager/src/test/java/org/apache/maven/shared/release/transform/jdom2/JDomModelTest.java @@ -69,7 +69,7 @@ public void testSetVersion() throws Exception { assertNull(model.getVersion()); // inherit from parent via CI friendly - content = "${revision}"; + content = "${revision}${changelist}"; projectElm = builder.build(new StringReader(content)).getRootElement(); model = new JDomModel(projectElm); assertNull(model.getVersion()); diff --git a/maven-release-manager/src/test/java/org/apache/maven/shared/release/util/MavenExpressionTest.java b/maven-release-manager/src/test/java/org/apache/maven/shared/release/util/MavenExpressionTest.java new file mode 100644 index 000000000..12c1b2d4b --- /dev/null +++ b/maven-release-manager/src/test/java/org/apache/maven/shared/release/util/MavenExpressionTest.java @@ -0,0 +1,61 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.maven.shared.release.util; + +import java.util.Arrays; +import java.util.Collection; +import java.util.Properties; + +import junit.framework.TestCase; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.junit.runners.Parameterized.Parameters; + +/** + * @author Mikhail Kolesnikov + */ +@RunWith(Parameterized.class) +public class MavenExpressionTest extends TestCase { + + private final String expected; + private final String expression; + private final Properties properties = new Properties(); + + public MavenExpressionTest(String expected, String expression) { + this.expected = expected; + this.expression = expression; + properties.setProperty("revision", "12"); + properties.setProperty("sha1", "34"); + properties.setProperty("changelist", "56"); + } + + @Parameters(name = "expected result {0} for expression {1}") + public static Collection parameters() { + return Arrays.asList( + new Object[] {"123456", "${revision}${sha1}${changelist}"}, + new Object[] {"12-34-56", "${revision}-${sha1}-${changelist}"}, + new Object[] {"12-null-56", "${revision}-${unknown}-${changelist}"}); + } + + @Test + public void testEvaluate() { + assertEquals(expected, MavenExpression.evaluate(expression, properties)); + } +} diff --git a/maven-release-manager/src/test/resources/projects/rewrite-for-development/pom-with-parent-and-cifriendly-expressions/expected-pom.xml b/maven-release-manager/src/test/resources/projects/rewrite-for-development/pom-with-parent-and-cifriendly-expressions/expected-pom.xml new file mode 100644 index 000000000..b30f8a7ee --- /dev/null +++ b/maven-release-manager/src/test/resources/projects/rewrite-for-development/pom-with-parent-and-cifriendly-expressions/expected-pom.xml @@ -0,0 +1,40 @@ + + + + + + 4.0.0 + groupId + artifactId + ${revision}${changelist} + pom + + + scm:svn:file://localhost/tmp/scm-repo/trunk + scm:svn:file://localhost/tmp/scm-repo/trunk + file://localhost/tmp/scm-repo/trunk + + + + 1.1 + -SNAPSHOT + + + + subproject1 + + diff --git a/maven-release-manager/src/test/resources/projects/rewrite-for-development/pom-with-parent-and-cifriendly-expressions/pom.xml b/maven-release-manager/src/test/resources/projects/rewrite-for-development/pom-with-parent-and-cifriendly-expressions/pom.xml new file mode 100644 index 000000000..cf04d4b75 --- /dev/null +++ b/maven-release-manager/src/test/resources/projects/rewrite-for-development/pom-with-parent-and-cifriendly-expressions/pom.xml @@ -0,0 +1,40 @@ + + + + + + 4.0.0 + groupId + artifactId + ${revision}${changelist} + pom + + + scm:svn:file://localhost/tmp/scm-repo/tags/release-label + scm:svn:file://localhost/tmp/scm-repo/tags/release-label + file://localhost/tmp/scm-repo/tags/release-label + + + + 1.0 + -SNAPSHOT + + + + subproject1 + + diff --git a/maven-release-manager/src/test/resources/projects/rewrite-for-development/pom-with-parent-and-cifriendly-expressions/subproject1/expected-pom.xml b/maven-release-manager/src/test/resources/projects/rewrite-for-development/pom-with-parent-and-cifriendly-expressions/subproject1/expected-pom.xml new file mode 100644 index 000000000..db70f7398 --- /dev/null +++ b/maven-release-manager/src/test/resources/projects/rewrite-for-development/pom-with-parent-and-cifriendly-expressions/subproject1/expected-pom.xml @@ -0,0 +1,28 @@ + + + + + + 4.0.0 + + groupId + artifactId + ${revision}${changelist} + + + subproject1 + diff --git a/maven-release-manager/src/test/resources/projects/rewrite-for-development/pom-with-parent-and-cifriendly-expressions/subproject1/pom.xml b/maven-release-manager/src/test/resources/projects/rewrite-for-development/pom-with-parent-and-cifriendly-expressions/subproject1/pom.xml new file mode 100644 index 000000000..db70f7398 --- /dev/null +++ b/maven-release-manager/src/test/resources/projects/rewrite-for-development/pom-with-parent-and-cifriendly-expressions/subproject1/pom.xml @@ -0,0 +1,28 @@ + + + + + + 4.0.0 + + groupId + artifactId + ${revision}${changelist} + + + subproject1 + diff --git a/maven-release-manager/src/test/resources/projects/rewrite-for-release/pom-with-parent-and-cifriendly-expressions/expected-pom.xml b/maven-release-manager/src/test/resources/projects/rewrite-for-release/pom-with-parent-and-cifriendly-expressions/expected-pom.xml index c75040633..2f68ea267 100644 --- a/maven-release-manager/src/test/resources/projects/rewrite-for-release/pom-with-parent-and-cifriendly-expressions/expected-pom.xml +++ b/maven-release-manager/src/test/resources/projects/rewrite-for-release/pom-with-parent-and-cifriendly-expressions/expected-pom.xml @@ -20,7 +20,7 @@ 4.0.0 groupId artifactId - ${revision} + ${revision}${changelist} pom @@ -30,7 +30,8 @@ - 1.0-SNAPSHOT + 1.0 + diff --git a/maven-release-manager/src/test/resources/projects/rewrite-for-release/pom-with-parent-and-cifriendly-expressions/pom.xml b/maven-release-manager/src/test/resources/projects/rewrite-for-release/pom-with-parent-and-cifriendly-expressions/pom.xml index 0c9bd8b28..5910661ee 100644 --- a/maven-release-manager/src/test/resources/projects/rewrite-for-release/pom-with-parent-and-cifriendly-expressions/pom.xml +++ b/maven-release-manager/src/test/resources/projects/rewrite-for-release/pom-with-parent-and-cifriendly-expressions/pom.xml @@ -20,7 +20,7 @@ 4.0.0 groupId artifactId - ${revision} + ${revision}${changelist} pom @@ -30,7 +30,8 @@ - 1.0-SNAPSHOT + 1.0 + -SNAPSHOT diff --git a/maven-release-manager/src/test/resources/projects/rewrite-for-release/pom-with-parent-and-cifriendly-expressions/subproject1/expected-pom.xml b/maven-release-manager/src/test/resources/projects/rewrite-for-release/pom-with-parent-and-cifriendly-expressions/subproject1/expected-pom.xml index 7b2fa88c1..db70f7398 100644 --- a/maven-release-manager/src/test/resources/projects/rewrite-for-release/pom-with-parent-and-cifriendly-expressions/subproject1/expected-pom.xml +++ b/maven-release-manager/src/test/resources/projects/rewrite-for-release/pom-with-parent-and-cifriendly-expressions/subproject1/expected-pom.xml @@ -21,7 +21,7 @@ groupId artifactId - ${revision} + ${revision}${changelist} subproject1 diff --git a/maven-release-manager/src/test/resources/projects/rewrite-for-release/pom-with-parent-and-cifriendly-expressions/subproject1/pom.xml b/maven-release-manager/src/test/resources/projects/rewrite-for-release/pom-with-parent-and-cifriendly-expressions/subproject1/pom.xml index 7b2fa88c1..db70f7398 100644 --- a/maven-release-manager/src/test/resources/projects/rewrite-for-release/pom-with-parent-and-cifriendly-expressions/subproject1/pom.xml +++ b/maven-release-manager/src/test/resources/projects/rewrite-for-release/pom-with-parent-and-cifriendly-expressions/subproject1/pom.xml @@ -21,7 +21,7 @@ groupId artifactId - ${revision} + ${revision}${changelist} subproject1 diff --git a/maven-release-plugin/pom.xml b/maven-release-plugin/pom.xml index ecd57eb03..cba2582ae 100644 --- a/maven-release-plugin/pom.xml +++ b/maven-release-plugin/pom.xml @@ -225,13 +225,7 @@ setup/*/pom.xml - projects/prepare/*/*pom.xml - projects/prepare/flat-multi-module/parent-project/pom.xml - projects/prepare-with-pom/*/*pom.xml - projects/branch/*/pom.xml - projects/perform/*/pom.xml - projects/update-versions/*/pom.xml - projects/stage/*/pom.xml + projects/prepare/ci-friendly-multi-module/*pom.xml projects/prepare/MRELEASE-966/pom.xml diff --git a/maven-release-plugin/src/it/projects/prepare/ci-friendly-multi-module/module-a/pom.xml b/maven-release-plugin/src/it/projects/prepare/ci-friendly-multi-module/module-a/pom.xml new file mode 100644 index 000000000..1a042b0ca --- /dev/null +++ b/maven-release-plugin/src/it/projects/prepare/ci-friendly-multi-module/module-a/pom.xml @@ -0,0 +1,39 @@ + + + + + ci-friendly-multi-module-project + org.apache.maven.plugin.release + ${revision}${changelist} + + 4.0.0 + org.apache.maven.plugin.release + module-a + module-a + http://maven.apache.org + + + junit + junit + @junitVersion@ + test + + + diff --git a/maven-release-plugin/src/it/projects/prepare/ci-friendly-multi-module/module-a/src/main/java/org/apache/maven/plugin/release/module/a/App.java b/maven-release-plugin/src/it/projects/prepare/ci-friendly-multi-module/module-a/src/main/java/org/apache/maven/plugin/release/module/a/App.java new file mode 100644 index 000000000..05ca6ecab --- /dev/null +++ b/maven-release-plugin/src/it/projects/prepare/ci-friendly-multi-module/module-a/src/main/java/org/apache/maven/plugin/release/module/a/App.java @@ -0,0 +1,32 @@ +package org.apache.maven.plugin.release.module.a; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +/** + * Hello world! + * + */ +public class App +{ + public static void main( String[] args ) + { + System.out.println( "Hello World!" ); + } +} diff --git a/maven-release-plugin/src/it/projects/prepare/ci-friendly-multi-module/module-a/src/test/java/org/apache/maven/plugin/release/module/a/AppTest.java b/maven-release-plugin/src/it/projects/prepare/ci-friendly-multi-module/module-a/src/test/java/org/apache/maven/plugin/release/module/a/AppTest.java new file mode 100644 index 000000000..ca41b5f53 --- /dev/null +++ b/maven-release-plugin/src/it/projects/prepare/ci-friendly-multi-module/module-a/src/test/java/org/apache/maven/plugin/release/module/a/AppTest.java @@ -0,0 +1,57 @@ +package org.apache.maven.plugin.release.module.a; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + +/** + * Unit test for simple App. + */ +public class AppTest + extends TestCase +{ + /** + * Create the test case + * + * @param testName name of the test case + */ + public AppTest( String testName ) + { + super( testName ); + } + + /** + * @return the suite of tests being tested + */ + public static Test suite() + { + return new TestSuite( AppTest.class ); + } + + /** + * Rigourous Test :-) + */ + public void testApp() + { + assertTrue( true ); + } +} diff --git a/maven-release-plugin/src/it/projects/prepare/ci-friendly-multi-module/module-b/pom.xml b/maven-release-plugin/src/it/projects/prepare/ci-friendly-multi-module/module-b/pom.xml new file mode 100644 index 000000000..f7fec33f1 --- /dev/null +++ b/maven-release-plugin/src/it/projects/prepare/ci-friendly-multi-module/module-b/pom.xml @@ -0,0 +1,39 @@ + + + + + ci-friendly-multi-module-project + org.apache.maven.plugin.release + ${revision}${changelist} + + 4.0.0 + org.apache.maven.plugin.release + module-b + module-b + http://maven.apache.org + + + junit + junit + @junitVersion@ + test + + + diff --git a/maven-release-plugin/src/it/projects/prepare/ci-friendly-multi-module/module-b/src/main/java/org/apache/maven/plugin/release/module/b/App.java b/maven-release-plugin/src/it/projects/prepare/ci-friendly-multi-module/module-b/src/main/java/org/apache/maven/plugin/release/module/b/App.java new file mode 100644 index 000000000..8d4d841d5 --- /dev/null +++ b/maven-release-plugin/src/it/projects/prepare/ci-friendly-multi-module/module-b/src/main/java/org/apache/maven/plugin/release/module/b/App.java @@ -0,0 +1,32 @@ +package org.apache.maven.plugin.release.module.b; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +/** + * Hello world! + * + */ +public class App +{ + public static void main( String[] args ) + { + System.out.println( "Hello World!" ); + } +} diff --git a/maven-release-plugin/src/it/projects/prepare/ci-friendly-multi-module/module-b/src/test/java/org/apache/maven/plugin/release/module/b/AppTest.java b/maven-release-plugin/src/it/projects/prepare/ci-friendly-multi-module/module-b/src/test/java/org/apache/maven/plugin/release/module/b/AppTest.java new file mode 100644 index 000000000..c1a903ec4 --- /dev/null +++ b/maven-release-plugin/src/it/projects/prepare/ci-friendly-multi-module/module-b/src/test/java/org/apache/maven/plugin/release/module/b/AppTest.java @@ -0,0 +1,57 @@ +package org.apache.maven.plugin.release.module.b; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + +/** + * Unit test for simple App. + */ +public class AppTest + extends TestCase +{ + /** + * Create the test case + * + * @param testName name of the test case + */ + public AppTest( String testName ) + { + super( testName ); + } + + /** + * @return the suite of tests being tested + */ + public static Test suite() + { + return new TestSuite( AppTest.class ); + } + + /** + * Rigourous Test :-) + */ + public void testApp() + { + assertTrue( true ); + } +} diff --git a/maven-release-plugin/src/it/projects/prepare/ci-friendly-multi-module/pom.xml b/maven-release-plugin/src/it/projects/prepare/ci-friendly-multi-module/pom.xml new file mode 100644 index 000000000..ff2ffc91a --- /dev/null +++ b/maven-release-plugin/src/it/projects/prepare/ci-friendly-multi-module/pom.xml @@ -0,0 +1,72 @@ + + + + 4.0.0 + org.apache.maven.plugin.release + ci-friendly-multi-module-project + pom + ${revision}${changelist} + parent-project + http://maven.apache.org + + scm:stub| + scm:stub| + HEAD + + + + 1.0 + -SNAPSHOT + @maven.compiler.source@ + @maven.compiler.target@ + + + + + + + org.apache.maven.plugins + maven-release-plugin + @project.version@ + + + org.apache.maven.its.release + maven-scm-provider-stub + 1.0 + + + + + + + + + + junit + junit + @junitVersion@ + test + + + + module-a + module-b + + diff --git a/maven-release-plugin/src/it/projects/prepare/ci-friendly-multi-module/verify.groovy b/maven-release-plugin/src/it/projects/prepare/ci-friendly-multi-module/verify.groovy new file mode 100644 index 000000000..5104bc5e0 --- /dev/null +++ b/maven-release-plugin/src/it/projects/prepare/ci-friendly-multi-module/verify.groovy @@ -0,0 +1,45 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import groovy.xml.XmlSlurper + +File buildLog = new File( basedir, 'build.log' ) +assert buildLog.exists() + +// next development versions +def projectRootTag = new XmlSlurper().parse( new File( basedir, 'pom.xml.tag' ) ) +assert projectRootTag.version.text() == '${revision}${changelist}' +// updating of the revision property is not implemented +assert projectRootTag.properties.revision.text() == "1.0" +assert projectRootTag.properties.changelist.text() == "" + +// next development versions +def projectRoot = new XmlSlurper().parse( new File( basedir, 'pom.xml.next' ) ) +assert projectRoot.version.text() == '${revision}${changelist}' +// updating of the revision property is not implemented +assert projectRoot.properties.revision.text() == "1.1" +assert projectRoot.properties.changelist.text() == "-SNAPSHOT" + +def projectA = new XmlSlurper().parse( new File( basedir, 'module-a/pom.xml.next' ) ) +assert projectA.parent.version.text() == '${revision}${changelist}' + +def projectB = new XmlSlurper().parse( new File( basedir, 'module-b/pom.xml.next' ) ) +assert projectB.parent.version.text() == '${revision}${changelist}' + +return true \ No newline at end of file diff --git a/maven-release-plugin/src/it/projects/prepare/invoker.properties b/maven-release-plugin/src/it/projects/prepare/invoker.properties index 43b6c5fcb..2ac395677 100644 --- a/maven-release-plugin/src/it/projects/prepare/invoker.properties +++ b/maven-release-plugin/src/it/projects/prepare/invoker.properties @@ -15,4 +15,6 @@ # specific language governing permissions and limitations # under the License. -invoker.goals = release:clean release:prepare \ No newline at end of file +invoker.goals = release:clean release:prepare + +invoker.mavenOpts = -agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=*:5005 \ No newline at end of file From ab5c8d516e84a45be144d0a85feb7011563519fd Mon Sep 17 00:00:00 2001 From: Mikhail Kolesnikov Date: Wed, 29 Nov 2023 20:20:32 +0300 Subject: [PATCH 02/15] MRELEASE-1109 patch JDomModel --- .../maven/shared/release/phase/AbstractRewritePomsPhase.java | 4 ++-- .../maven/shared/release/transform/jdom2/JDomModel.java | 2 +- .../expected-pom.xml | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/maven-release-manager/src/main/java/org/apache/maven/shared/release/phase/AbstractRewritePomsPhase.java b/maven-release-manager/src/main/java/org/apache/maven/shared/release/phase/AbstractRewritePomsPhase.java index 76747c563..71972df19 100644 --- a/maven-release-manager/src/main/java/org/apache/maven/shared/release/phase/AbstractRewritePomsPhase.java +++ b/maven-release-manager/src/main/java/org/apache/maven/shared/release/phase/AbstractRewritePomsPhase.java @@ -96,7 +96,7 @@ public abstract class AbstractRewritePomsPhase extends AbstractReleasePhase impl * Regular expression pattern matching Maven expressions (i.e. references to Maven properties). * The first group selects the property name the expression refers to. */ - private static final Pattern EXPRESSION_PATTERN = Pattern.compile("\\$\\{(.+)\\}"); + private static final Pattern EXPRESSION_PATTERN = Pattern.compile("\\$\\{(.+?)\\}"); /** * All Maven properties allowed to be referenced in parent versions via expressions @@ -462,7 +462,7 @@ private void rewriteVersion( */ public static String extractPropertyFromExpression(String expression) { Matcher matcher = EXPRESSION_PATTERN.matcher(expression); - if (!matcher.matches()) { + if (!matcher.find()) { return null; } return matcher.group(1); diff --git a/maven-release-manager/src/main/java/org/apache/maven/shared/release/transform/jdom2/JDomModel.java b/maven-release-manager/src/main/java/org/apache/maven/shared/release/transform/jdom2/JDomModel.java index 6198ee73d..20d972bd9 100644 --- a/maven-release-manager/src/main/java/org/apache/maven/shared/release/transform/jdom2/JDomModel.java +++ b/maven-release-manager/src/main/java/org/apache/maven/shared/release/transform/jdom2/JDomModel.java @@ -198,7 +198,7 @@ public void setVersion(String version) { AbstractRewritePomsPhase.extractPropertyFromExpression(versionElement.getTextNormalize()); Properties properties = getProperties(); if (properties != null) { - properties.computeIfPresent(ciFriendlyPropertyName, (k, v) -> version); + properties.setProperty(ciFriendlyPropertyName, version); } } else { JDomUtils.rewriteValue(versionElement, version); diff --git a/maven-release-manager/src/test/resources/projects/rewrite-for-release/pom-with-parent-and-cifriendly-expressions/expected-pom.xml b/maven-release-manager/src/test/resources/projects/rewrite-for-release/pom-with-parent-and-cifriendly-expressions/expected-pom.xml index c75040633..b4326d54a 100644 --- a/maven-release-manager/src/test/resources/projects/rewrite-for-release/pom-with-parent-and-cifriendly-expressions/expected-pom.xml +++ b/maven-release-manager/src/test/resources/projects/rewrite-for-release/pom-with-parent-and-cifriendly-expressions/expected-pom.xml @@ -30,7 +30,7 @@ - 1.0-SNAPSHOT + 1.0 From 0ad7bb6b0b9d31a9e2339b3fdeddc3b065afed0e Mon Sep 17 00:00:00 2001 From: mkolesnikov Date: Wed, 29 Nov 2023 23:21:59 +0300 Subject: [PATCH 03/15] MRELEASE-1109 Full support for revision sha1 and changelist properties. --- .../release/phase/RewritePomsForReleasePhase.java | 12 ++++++++++++ .../shared/release/transform/jdom2/JDomModel.java | 12 +++++++++++- .../release/transform/jdom2/JDomProperties.java | 4 +++- .../phase/RewritePomsForDevelopmentPhaseTest.java | 5 ++++- .../release/transform/jdom2/JDomPropertiesTest.java | 9 ++++++--- .../expected-pom.xml | 3 ++- .../pom.xml | 3 ++- .../subproject1/expected-pom.xml | 2 +- .../subproject1/pom.xml | 2 +- .../expected-pom.xml | 4 +++- .../pom.xml | 3 ++- .../subproject1/expected-pom.xml | 2 +- .../subproject1/pom.xml | 2 +- maven-release-plugin/pom.xml | 8 +++++++- .../src/it/projects/prepare/invoker.properties | 4 +--- 15 files changed, 57 insertions(+), 18 deletions(-) diff --git a/maven-release-manager/src/main/java/org/apache/maven/shared/release/phase/RewritePomsForReleasePhase.java b/maven-release-manager/src/main/java/org/apache/maven/shared/release/phase/RewritePomsForReleasePhase.java index 67b9f18c0..ac10bfd33 100644 --- a/maven-release-manager/src/main/java/org/apache/maven/shared/release/phase/RewritePomsForReleasePhase.java +++ b/maven-release-manager/src/main/java/org/apache/maven/shared/release/phase/RewritePomsForReleasePhase.java @@ -26,6 +26,7 @@ import java.nio.file.LinkOption; import java.nio.file.Path; import java.nio.file.Paths; +import java.util.List; import java.util.Map; import org.apache.maven.artifact.ArtifactUtils; @@ -34,8 +35,10 @@ import org.apache.maven.project.MavenProject; import org.apache.maven.scm.repository.ScmRepository; import org.apache.maven.shared.release.ReleaseExecutionException; +import org.apache.maven.shared.release.ReleaseFailureException; import org.apache.maven.shared.release.ReleaseResult; import org.apache.maven.shared.release.config.ReleaseDescriptor; +import org.apache.maven.shared.release.env.ReleaseEnvironment; import org.apache.maven.shared.release.scm.ScmRepositoryConfigurator; import org.apache.maven.shared.release.scm.ScmTranslator; import org.apache.maven.shared.release.transform.ModelETLFactory; @@ -214,6 +217,15 @@ private boolean translateScm( return result; } + @Override + public ReleaseResult execute( + ReleaseDescriptor releaseDescriptor, + ReleaseEnvironment releaseEnvironment, + List reactorProjects) + throws ReleaseExecutionException, ReleaseFailureException { + return super.execute(releaseDescriptor, releaseEnvironment, reactorProjects); + } + @Override protected String getOriginalVersion(ReleaseDescriptor releaseDescriptor, String projectKey, boolean simulate) { return releaseDescriptor.getProjectOriginalVersion(projectKey); diff --git a/maven-release-manager/src/main/java/org/apache/maven/shared/release/transform/jdom2/JDomModel.java b/maven-release-manager/src/main/java/org/apache/maven/shared/release/transform/jdom2/JDomModel.java index 20d972bd9..949ce5943 100644 --- a/maven-release-manager/src/main/java/org/apache/maven/shared/release/transform/jdom2/JDomModel.java +++ b/maven-release-manager/src/main/java/org/apache/maven/shared/release/transform/jdom2/JDomModel.java @@ -23,6 +23,7 @@ import java.util.List; import java.util.Properties; +import org.apache.maven.artifact.ArtifactUtils; import org.apache.maven.model.Build; import org.apache.maven.model.Dependency; import org.apache.maven.model.DependencyManagement; @@ -198,7 +199,16 @@ public void setVersion(String version) { AbstractRewritePomsPhase.extractPropertyFromExpression(versionElement.getTextNormalize()); Properties properties = getProperties(); if (properties != null) { - properties.setProperty(ciFriendlyPropertyName, version); + String sha1 = properties.getProperty("sha1", ""); + String changelist = properties.getProperty("changelist", ""); + properties.setProperty( + ciFriendlyPropertyName, version.replaceAll(sha1, "").replaceAll(changelist, "")); + if (ArtifactUtils.isSnapshot(version)) { + properties.setProperty("changelist", changelist); + } else { + properties.setProperty(ciFriendlyPropertyName, version); + properties.setProperty("changelist", ""); + } } } else { JDomUtils.rewriteValue(versionElement, version); diff --git a/maven-release-manager/src/main/java/org/apache/maven/shared/release/transform/jdom2/JDomProperties.java b/maven-release-manager/src/main/java/org/apache/maven/shared/release/transform/jdom2/JDomProperties.java index b2c314f34..6a560649b 100644 --- a/maven-release-manager/src/main/java/org/apache/maven/shared/release/transform/jdom2/JDomProperties.java +++ b/maven-release-manager/src/main/java/org/apache/maven/shared/release/transform/jdom2/JDomProperties.java @@ -114,7 +114,9 @@ public String getProperty(String key) { @Override public String getProperty(String key, String defaultValue) { - throw new UnsupportedOperationException(); + String property = getProperty(key); + + return property == null ? defaultValue : property; } @Override diff --git a/maven-release-manager/src/test/java/org/apache/maven/shared/release/phase/RewritePomsForDevelopmentPhaseTest.java b/maven-release-manager/src/test/java/org/apache/maven/shared/release/phase/RewritePomsForDevelopmentPhaseTest.java index b1bebb81c..59cc47c3d 100644 --- a/maven-release-manager/src/test/java/org/apache/maven/shared/release/phase/RewritePomsForDevelopmentPhaseTest.java +++ b/maven-release-manager/src/test/java/org/apache/maven/shared/release/phase/RewritePomsForDevelopmentPhaseTest.java @@ -404,11 +404,14 @@ public void testRewritePomWithCiFriendlyReactor() throws Exception { List reactorProjects = createReactorProjects("pom-with-parent-and-cifriendly-expressions"); ReleaseDescriptorBuilder builder = - createDescriptorFromProjects(reactorProjects, "pom-with-parent-and-cifriendly-expressions"); + createDescriptorFromProjects(reactorProjects, "pom-with-parent-and-cifriendly-expressions"); builder.addReleaseVersion("groupId:artifactId", RELEASE_VERSION); builder.addDevelopmentVersion("groupId:artifactId", NEXT_VERSION); builder.addReleaseVersion("groupId:subproject1", RELEASE_VERSION); builder.addDevelopmentVersion("groupId:subproject1", NEXT_VERSION); + + mapScm(builder); + phase.execute(ReleaseUtils.buildReleaseDescriptor(builder), new DefaultReleaseEnvironment(), reactorProjects); assertTrue(comparePomFiles(reactorProjects)); diff --git a/maven-release-manager/src/test/java/org/apache/maven/shared/release/transform/jdom2/JDomPropertiesTest.java b/maven-release-manager/src/test/java/org/apache/maven/shared/release/transform/jdom2/JDomPropertiesTest.java index fa9f48d99..0335a1160 100644 --- a/maven-release-manager/src/test/java/org/apache/maven/shared/release/transform/jdom2/JDomPropertiesTest.java +++ b/maven-release-manager/src/test/java/org/apache/maven/shared/release/transform/jdom2/JDomPropertiesTest.java @@ -101,9 +101,12 @@ public void testGetProperty() throws Exception { assertEquals("VALUE", new JDomProperties(propertiesElm).getProperty("KEY")); } - @Test(expected = UnsupportedOperationException.class) - public void testGetPropertyDefault() { - new JDomProperties(null).getProperty(null, null); + @Test + public void testGetPropertyDefault() throws Exception { + String content = ""; + Element propertiesElm = builder.build(new StringReader(content)).getRootElement(); + assertNull(new JDomProperties(propertiesElm).getProperty("KEY", null)); + assertEquals("", new JDomProperties(propertiesElm).getProperty("KEY", "")); } @Test(expected = UnsupportedOperationException.class) diff --git a/maven-release-manager/src/test/resources/projects/rewrite-for-development/pom-with-parent-and-cifriendly-expressions/expected-pom.xml b/maven-release-manager/src/test/resources/projects/rewrite-for-development/pom-with-parent-and-cifriendly-expressions/expected-pom.xml index b30f8a7ee..d2fe2b7a2 100644 --- a/maven-release-manager/src/test/resources/projects/rewrite-for-development/pom-with-parent-and-cifriendly-expressions/expected-pom.xml +++ b/maven-release-manager/src/test/resources/projects/rewrite-for-development/pom-with-parent-and-cifriendly-expressions/expected-pom.xml @@ -20,7 +20,7 @@ 4.0.0 groupId artifactId - ${revision}${changelist} + ${revision}${sha1}${changelist} pom @@ -31,6 +31,7 @@ 1.1 + .123 -SNAPSHOT diff --git a/maven-release-manager/src/test/resources/projects/rewrite-for-development/pom-with-parent-and-cifriendly-expressions/pom.xml b/maven-release-manager/src/test/resources/projects/rewrite-for-development/pom-with-parent-and-cifriendly-expressions/pom.xml index cf04d4b75..e42e87c9f 100644 --- a/maven-release-manager/src/test/resources/projects/rewrite-for-development/pom-with-parent-and-cifriendly-expressions/pom.xml +++ b/maven-release-manager/src/test/resources/projects/rewrite-for-development/pom-with-parent-and-cifriendly-expressions/pom.xml @@ -20,7 +20,7 @@ 4.0.0 groupId artifactId - ${revision}${changelist} + ${revision}${sha1}${changelist} pom @@ -31,6 +31,7 @@ 1.0 + .123 -SNAPSHOT diff --git a/maven-release-manager/src/test/resources/projects/rewrite-for-development/pom-with-parent-and-cifriendly-expressions/subproject1/expected-pom.xml b/maven-release-manager/src/test/resources/projects/rewrite-for-development/pom-with-parent-and-cifriendly-expressions/subproject1/expected-pom.xml index db70f7398..a025619d9 100644 --- a/maven-release-manager/src/test/resources/projects/rewrite-for-development/pom-with-parent-and-cifriendly-expressions/subproject1/expected-pom.xml +++ b/maven-release-manager/src/test/resources/projects/rewrite-for-development/pom-with-parent-and-cifriendly-expressions/subproject1/expected-pom.xml @@ -21,7 +21,7 @@ groupId artifactId - ${revision}${changelist} + ${revision}${sha1}${changelist} subproject1 diff --git a/maven-release-manager/src/test/resources/projects/rewrite-for-development/pom-with-parent-and-cifriendly-expressions/subproject1/pom.xml b/maven-release-manager/src/test/resources/projects/rewrite-for-development/pom-with-parent-and-cifriendly-expressions/subproject1/pom.xml index db70f7398..a025619d9 100644 --- a/maven-release-manager/src/test/resources/projects/rewrite-for-development/pom-with-parent-and-cifriendly-expressions/subproject1/pom.xml +++ b/maven-release-manager/src/test/resources/projects/rewrite-for-development/pom-with-parent-and-cifriendly-expressions/subproject1/pom.xml @@ -21,7 +21,7 @@ groupId artifactId - ${revision}${changelist} + ${revision}${sha1}${changelist} subproject1 diff --git a/maven-release-manager/src/test/resources/projects/rewrite-for-release/pom-with-parent-and-cifriendly-expressions/expected-pom.xml b/maven-release-manager/src/test/resources/projects/rewrite-for-release/pom-with-parent-and-cifriendly-expressions/expected-pom.xml index 4f3e9f5e8..30e536cda 100644 --- a/maven-release-manager/src/test/resources/projects/rewrite-for-release/pom-with-parent-and-cifriendly-expressions/expected-pom.xml +++ b/maven-release-manager/src/test/resources/projects/rewrite-for-release/pom-with-parent-and-cifriendly-expressions/expected-pom.xml @@ -20,7 +20,7 @@ 4.0.0 groupId artifactId - ${revision}${changelist} + ${revision}${sha1}${changelist} pom @@ -31,6 +31,8 @@ 1.0 + .123 + diff --git a/maven-release-manager/src/test/resources/projects/rewrite-for-release/pom-with-parent-and-cifriendly-expressions/pom.xml b/maven-release-manager/src/test/resources/projects/rewrite-for-release/pom-with-parent-and-cifriendly-expressions/pom.xml index 5910661ee..d428097d1 100644 --- a/maven-release-manager/src/test/resources/projects/rewrite-for-release/pom-with-parent-and-cifriendly-expressions/pom.xml +++ b/maven-release-manager/src/test/resources/projects/rewrite-for-release/pom-with-parent-and-cifriendly-expressions/pom.xml @@ -20,7 +20,7 @@ 4.0.0 groupId artifactId - ${revision}${changelist} + ${revision}${sha1}${changelist} pom @@ -31,6 +31,7 @@ 1.0 + .123 -SNAPSHOT diff --git a/maven-release-manager/src/test/resources/projects/rewrite-for-release/pom-with-parent-and-cifriendly-expressions/subproject1/expected-pom.xml b/maven-release-manager/src/test/resources/projects/rewrite-for-release/pom-with-parent-and-cifriendly-expressions/subproject1/expected-pom.xml index db70f7398..a025619d9 100644 --- a/maven-release-manager/src/test/resources/projects/rewrite-for-release/pom-with-parent-and-cifriendly-expressions/subproject1/expected-pom.xml +++ b/maven-release-manager/src/test/resources/projects/rewrite-for-release/pom-with-parent-and-cifriendly-expressions/subproject1/expected-pom.xml @@ -21,7 +21,7 @@ groupId artifactId - ${revision}${changelist} + ${revision}${sha1}${changelist} subproject1 diff --git a/maven-release-manager/src/test/resources/projects/rewrite-for-release/pom-with-parent-and-cifriendly-expressions/subproject1/pom.xml b/maven-release-manager/src/test/resources/projects/rewrite-for-release/pom-with-parent-and-cifriendly-expressions/subproject1/pom.xml index db70f7398..a025619d9 100644 --- a/maven-release-manager/src/test/resources/projects/rewrite-for-release/pom-with-parent-and-cifriendly-expressions/subproject1/pom.xml +++ b/maven-release-manager/src/test/resources/projects/rewrite-for-release/pom-with-parent-and-cifriendly-expressions/subproject1/pom.xml @@ -21,7 +21,7 @@ groupId artifactId - ${revision}${changelist} + ${revision}${sha1}${changelist} subproject1 diff --git a/maven-release-plugin/pom.xml b/maven-release-plugin/pom.xml index cba2582ae..ecd57eb03 100644 --- a/maven-release-plugin/pom.xml +++ b/maven-release-plugin/pom.xml @@ -225,7 +225,13 @@ setup/*/pom.xml - projects/prepare/ci-friendly-multi-module/*pom.xml + projects/prepare/*/*pom.xml + projects/prepare/flat-multi-module/parent-project/pom.xml + projects/prepare-with-pom/*/*pom.xml + projects/branch/*/pom.xml + projects/perform/*/pom.xml + projects/update-versions/*/pom.xml + projects/stage/*/pom.xml projects/prepare/MRELEASE-966/pom.xml diff --git a/maven-release-plugin/src/it/projects/prepare/invoker.properties b/maven-release-plugin/src/it/projects/prepare/invoker.properties index 2ac395677..43b6c5fcb 100644 --- a/maven-release-plugin/src/it/projects/prepare/invoker.properties +++ b/maven-release-plugin/src/it/projects/prepare/invoker.properties @@ -15,6 +15,4 @@ # specific language governing permissions and limitations # under the License. -invoker.goals = release:clean release:prepare - -invoker.mavenOpts = -agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=*:5005 \ No newline at end of file +invoker.goals = release:clean release:prepare \ No newline at end of file From ef93eecd7348517318efae904c488ef82aebfdb7 Mon Sep 17 00:00:00 2001 From: mkolesnikov Date: Thu, 30 Nov 2023 18:22:51 +0300 Subject: [PATCH 04/15] MRELEASE-1109 single revision property --- .../release/transform/jdom2/JDomModel.java | 5 ++- .../transform/jdom2/JDomProperties.java | 3 ++ .../shared/release/util/MavenExpression.java | 6 +-- .../phase/RewritePomsForReleasePhaseTest.java | 13 +++++++ .../expected-pom.xml | 39 +++++++++++++++++++ .../pom.xml | 39 +++++++++++++++++++ .../subproject1/expected-pom.xml | 28 +++++++++++++ .../subproject1/pom.xml | 28 +++++++++++++ 8 files changed, 156 insertions(+), 5 deletions(-) create mode 100644 maven-release-manager/src/test/resources/projects/rewrite-for-release/pom-with-parent-and-cifriendly-revision/expected-pom.xml create mode 100644 maven-release-manager/src/test/resources/projects/rewrite-for-release/pom-with-parent-and-cifriendly-revision/pom.xml create mode 100644 maven-release-manager/src/test/resources/projects/rewrite-for-release/pom-with-parent-and-cifriendly-revision/subproject1/expected-pom.xml create mode 100644 maven-release-manager/src/test/resources/projects/rewrite-for-release/pom-with-parent-and-cifriendly-revision/subproject1/pom.xml diff --git a/maven-release-manager/src/main/java/org/apache/maven/shared/release/transform/jdom2/JDomModel.java b/maven-release-manager/src/main/java/org/apache/maven/shared/release/transform/jdom2/JDomModel.java index 949ce5943..4c131d21c 100644 --- a/maven-release-manager/src/main/java/org/apache/maven/shared/release/transform/jdom2/JDomModel.java +++ b/maven-release-manager/src/main/java/org/apache/maven/shared/release/transform/jdom2/JDomModel.java @@ -202,11 +202,12 @@ public void setVersion(String version) { String sha1 = properties.getProperty("sha1", ""); String changelist = properties.getProperty("changelist", ""); properties.setProperty( - ciFriendlyPropertyName, version.replaceAll(sha1, "").replaceAll(changelist, "")); + ciFriendlyPropertyName, + // assume that everybody follows the example and properties are simply chained + version.replaceAll(sha1, "").replaceAll(changelist, "")); if (ArtifactUtils.isSnapshot(version)) { properties.setProperty("changelist", changelist); } else { - properties.setProperty(ciFriendlyPropertyName, version); properties.setProperty("changelist", ""); } } diff --git a/maven-release-manager/src/main/java/org/apache/maven/shared/release/transform/jdom2/JDomProperties.java b/maven-release-manager/src/main/java/org/apache/maven/shared/release/transform/jdom2/JDomProperties.java index 6a560649b..0ffac3186 100644 --- a/maven-release-manager/src/main/java/org/apache/maven/shared/release/transform/jdom2/JDomProperties.java +++ b/maven-release-manager/src/main/java/org/apache/maven/shared/release/transform/jdom2/JDomProperties.java @@ -54,6 +54,9 @@ public JDomProperties(Element properties) { public synchronized Object setProperty(String key, String value) { Element property = properties.getChild(key, properties.getNamespace()); + if (property == null) { + property = new Element(key, properties.getNamespace()); + } JDomUtils.rewriteValue(property, value); // todo follow specs of Hashtable.put diff --git a/maven-release-manager/src/main/java/org/apache/maven/shared/release/util/MavenExpression.java b/maven-release-manager/src/main/java/org/apache/maven/shared/release/util/MavenExpression.java index 6986cdb86..4976efb11 100644 --- a/maven-release-manager/src/main/java/org/apache/maven/shared/release/util/MavenExpression.java +++ b/maven-release-manager/src/main/java/org/apache/maven/shared/release/util/MavenExpression.java @@ -18,7 +18,7 @@ */ package org.apache.maven.shared.release.util; -import java.util.Properties; +import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -30,12 +30,12 @@ public class MavenExpression { private MavenExpression() {} - public static String evaluate(String expression, Properties properties) { + public static String evaluate(String expression, Map properties) { StringBuilder result = new StringBuilder(expression); Matcher matcher = EXPRESSION_PATTERN.matcher(result); while (matcher.find()) { String propertyName = matcher.group(1); - String propertyValue = properties.getProperty(propertyName); + Object propertyValue = properties.get(propertyName); result.replace(matcher.start(), matcher.end(), String.valueOf(propertyValue)); matcher.reset(); } diff --git a/maven-release-manager/src/test/java/org/apache/maven/shared/release/phase/RewritePomsForReleasePhaseTest.java b/maven-release-manager/src/test/java/org/apache/maven/shared/release/phase/RewritePomsForReleasePhaseTest.java index 71515aed7..547acb5a8 100644 --- a/maven-release-manager/src/test/java/org/apache/maven/shared/release/phase/RewritePomsForReleasePhaseTest.java +++ b/maven-release-manager/src/test/java/org/apache/maven/shared/release/phase/RewritePomsForReleasePhaseTest.java @@ -334,6 +334,19 @@ public void testRewritePomWithCiFriendlyReactor() throws Exception { assertTrue(comparePomFiles(reactorProjects)); } + @Test + public void testRewritePomWithCiFriendlyReactorWithOnlyRevision() throws Exception { + List reactorProjects = createReactorProjects("pom-with-parent-and-cifriendly-revision"); + + ReleaseDescriptorBuilder builder = + createDescriptorFromProjects(reactorProjects, "pom-with-parent-and-cifriendly-revision"); + builder.addReleaseVersion("groupId:artifactId", NEXT_VERSION); + builder.addReleaseVersion("groupId:subproject1", NEXT_VERSION); + phase.execute(ReleaseUtils.buildReleaseDescriptor(builder), new DefaultReleaseEnvironment(), reactorProjects); + + assertTrue(comparePomFiles(reactorProjects)); + } + // MRELEASE-311 @Test public void testRewritePomWithDependencyPropertyCoordinate() throws Exception { diff --git a/maven-release-manager/src/test/resources/projects/rewrite-for-release/pom-with-parent-and-cifriendly-revision/expected-pom.xml b/maven-release-manager/src/test/resources/projects/rewrite-for-release/pom-with-parent-and-cifriendly-revision/expected-pom.xml new file mode 100644 index 000000000..b4326d54a --- /dev/null +++ b/maven-release-manager/src/test/resources/projects/rewrite-for-release/pom-with-parent-and-cifriendly-revision/expected-pom.xml @@ -0,0 +1,39 @@ + + + + + + 4.0.0 + groupId + artifactId + ${revision} + pom + + + scm:svn:file://localhost/tmp/scm-repo/tags/release-label + scm:svn:file://localhost/tmp/scm-repo/tags/release-label + file://localhost/tmp/scm-repo/tags/release-label + + + + 1.0 + + + + subproject1 + + diff --git a/maven-release-manager/src/test/resources/projects/rewrite-for-release/pom-with-parent-and-cifriendly-revision/pom.xml b/maven-release-manager/src/test/resources/projects/rewrite-for-release/pom-with-parent-and-cifriendly-revision/pom.xml new file mode 100644 index 000000000..0c9bd8b28 --- /dev/null +++ b/maven-release-manager/src/test/resources/projects/rewrite-for-release/pom-with-parent-and-cifriendly-revision/pom.xml @@ -0,0 +1,39 @@ + + + + + + 4.0.0 + groupId + artifactId + ${revision} + pom + + + scm:svn:file://localhost/tmp/scm-repo/trunk + scm:svn:file://localhost/tmp/scm-repo/trunk + file://localhost/tmp/scm-repo/trunk + + + + 1.0-SNAPSHOT + + + + subproject1 + + diff --git a/maven-release-manager/src/test/resources/projects/rewrite-for-release/pom-with-parent-and-cifriendly-revision/subproject1/expected-pom.xml b/maven-release-manager/src/test/resources/projects/rewrite-for-release/pom-with-parent-and-cifriendly-revision/subproject1/expected-pom.xml new file mode 100644 index 000000000..7b2fa88c1 --- /dev/null +++ b/maven-release-manager/src/test/resources/projects/rewrite-for-release/pom-with-parent-and-cifriendly-revision/subproject1/expected-pom.xml @@ -0,0 +1,28 @@ + + + + + + 4.0.0 + + groupId + artifactId + ${revision} + + + subproject1 + diff --git a/maven-release-manager/src/test/resources/projects/rewrite-for-release/pom-with-parent-and-cifriendly-revision/subproject1/pom.xml b/maven-release-manager/src/test/resources/projects/rewrite-for-release/pom-with-parent-and-cifriendly-revision/subproject1/pom.xml new file mode 100644 index 000000000..7b2fa88c1 --- /dev/null +++ b/maven-release-manager/src/test/resources/projects/rewrite-for-release/pom-with-parent-and-cifriendly-revision/subproject1/pom.xml @@ -0,0 +1,28 @@ + + + + + + 4.0.0 + + groupId + artifactId + ${revision} + + + subproject1 + From 27791ef4df774d4e9262a4f7029d381d426bbd48 Mon Sep 17 00:00:00 2001 From: mkolesnikov Date: Thu, 30 Nov 2023 23:32:32 +0300 Subject: [PATCH 05/15] MRELEASE-1109 reverted redundant changes --- .../shared/release/phase/RewritePomsForReleasePhase.java | 9 --------- 1 file changed, 9 deletions(-) diff --git a/maven-release-manager/src/main/java/org/apache/maven/shared/release/phase/RewritePomsForReleasePhase.java b/maven-release-manager/src/main/java/org/apache/maven/shared/release/phase/RewritePomsForReleasePhase.java index ac10bfd33..ac1eca179 100644 --- a/maven-release-manager/src/main/java/org/apache/maven/shared/release/phase/RewritePomsForReleasePhase.java +++ b/maven-release-manager/src/main/java/org/apache/maven/shared/release/phase/RewritePomsForReleasePhase.java @@ -217,15 +217,6 @@ private boolean translateScm( return result; } - @Override - public ReleaseResult execute( - ReleaseDescriptor releaseDescriptor, - ReleaseEnvironment releaseEnvironment, - List reactorProjects) - throws ReleaseExecutionException, ReleaseFailureException { - return super.execute(releaseDescriptor, releaseEnvironment, reactorProjects); - } - @Override protected String getOriginalVersion(ReleaseDescriptor releaseDescriptor, String projectKey, boolean simulate) { return releaseDescriptor.getProjectOriginalVersion(projectKey); From 66f51ced96e4797a1e69cd1684257d3944221ddd Mon Sep 17 00:00:00 2001 From: mkolesnikov Date: Fri, 1 Dec 2023 09:16:41 +0300 Subject: [PATCH 06/15] MRELEASE-1109 fixed format --- .../maven/shared/release/phase/RewritePomsForReleasePhase.java | 3 --- 1 file changed, 3 deletions(-) diff --git a/maven-release-manager/src/main/java/org/apache/maven/shared/release/phase/RewritePomsForReleasePhase.java b/maven-release-manager/src/main/java/org/apache/maven/shared/release/phase/RewritePomsForReleasePhase.java index ac1eca179..67b9f18c0 100644 --- a/maven-release-manager/src/main/java/org/apache/maven/shared/release/phase/RewritePomsForReleasePhase.java +++ b/maven-release-manager/src/main/java/org/apache/maven/shared/release/phase/RewritePomsForReleasePhase.java @@ -26,7 +26,6 @@ import java.nio.file.LinkOption; import java.nio.file.Path; import java.nio.file.Paths; -import java.util.List; import java.util.Map; import org.apache.maven.artifact.ArtifactUtils; @@ -35,10 +34,8 @@ import org.apache.maven.project.MavenProject; import org.apache.maven.scm.repository.ScmRepository; import org.apache.maven.shared.release.ReleaseExecutionException; -import org.apache.maven.shared.release.ReleaseFailureException; import org.apache.maven.shared.release.ReleaseResult; import org.apache.maven.shared.release.config.ReleaseDescriptor; -import org.apache.maven.shared.release.env.ReleaseEnvironment; import org.apache.maven.shared.release.scm.ScmRepositoryConfigurator; import org.apache.maven.shared.release.scm.ScmTranslator; import org.apache.maven.shared.release.transform.ModelETLFactory; From dbcbb089d5a2143bd96ed325867a4cf3b08a4a73 Mon Sep 17 00:00:00 2001 From: mkolesnikov Date: Fri, 1 Dec 2023 16:24:30 +0300 Subject: [PATCH 07/15] MRELEASE-1109 added asserts for pom.xml.tag files and removed misleading comments. --- .../prepare/ci-friendly-multi-module/verify.groovy | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/maven-release-plugin/src/it/projects/prepare/ci-friendly-multi-module/verify.groovy b/maven-release-plugin/src/it/projects/prepare/ci-friendly-multi-module/verify.groovy index 5104bc5e0..ed53638ed 100644 --- a/maven-release-plugin/src/it/projects/prepare/ci-friendly-multi-module/verify.groovy +++ b/maven-release-plugin/src/it/projects/prepare/ci-friendly-multi-module/verify.groovy @@ -22,17 +22,22 @@ import groovy.xml.XmlSlurper File buildLog = new File( basedir, 'build.log' ) assert buildLog.exists() -// next development versions +// tag versions def projectRootTag = new XmlSlurper().parse( new File( basedir, 'pom.xml.tag' ) ) assert projectRootTag.version.text() == '${revision}${changelist}' -// updating of the revision property is not implemented assert projectRootTag.properties.revision.text() == "1.0" assert projectRootTag.properties.changelist.text() == "" +def projectATag = new XmlSlurper().parse( new File( basedir, 'module-a/pom.xml.tag' ) ) +assert projectATag.parent.version.text() == '${revision}${changelist}' + +def projectBTag = new XmlSlurper().parse( new File( basedir, 'module-b/pom.xml.tag' ) ) +assert projectBTag.parent.version.text() == '${revision}${changelist}' + + // next development versions def projectRoot = new XmlSlurper().parse( new File( basedir, 'pom.xml.next' ) ) assert projectRoot.version.text() == '${revision}${changelist}' -// updating of the revision property is not implemented assert projectRoot.properties.revision.text() == "1.1" assert projectRoot.properties.changelist.text() == "-SNAPSHOT" @@ -42,4 +47,4 @@ assert projectA.parent.version.text() == '${revision}${changelist}' def projectB = new XmlSlurper().parse( new File( basedir, 'module-b/pom.xml.next' ) ) assert projectB.parent.version.text() == '${revision}${changelist}' -return true \ No newline at end of file +return true From 4032941519f5c0e45a21f4a104bec45fa1c0d2a2 Mon Sep 17 00:00:00 2001 From: mkolesnikov Date: Mon, 3 Jun 2024 17:31:15 +0300 Subject: [PATCH 08/15] MRELEASE-1109 addressed review comments and replaced `replaceAll` with `replace`. Added more tests after using this version for a while in a real project. --- .../phase/AbstractRewritePomsPhase.java | 40 +------- .../release/transform/jdom2/JDomModel.java | 25 +---- .../transform/jdom2/JDomProperties.java | 17 +++- .../release/util/CiFriendlyVersion.java | 96 +++++++++++++++++++ .../RewritePomsForDevelopmentPhaseTest.java | 15 +++ .../expected-pom.xml | 3 +- .../pom.xml | 5 +- .../expected-pom.xml | 39 ++++++++ .../pom.xml | 39 ++++++++ .../subproject1/expected-pom.xml | 28 ++++++ .../subproject1/pom.xml | 28 ++++++ .../invoker.properties | 19 ++++ .../ci-friendly-multi-module/module-a/pom.xml | 2 +- .../ci-friendly-multi-module/module-b/pom.xml | 2 +- .../prepare/ci-friendly-multi-module/pom.xml | 3 +- .../ci-friendly-multi-module/verify.groovy | 14 +-- 16 files changed, 304 insertions(+), 71 deletions(-) create mode 100644 maven-release-manager/src/main/java/org/apache/maven/shared/release/util/CiFriendlyVersion.java create mode 100644 maven-release-manager/src/test/resources/projects/rewrite-for-development/pom-with-parent-and-cifriendly-revision/expected-pom.xml create mode 100644 maven-release-manager/src/test/resources/projects/rewrite-for-development/pom-with-parent-and-cifriendly-revision/pom.xml create mode 100644 maven-release-manager/src/test/resources/projects/rewrite-for-development/pom-with-parent-and-cifriendly-revision/subproject1/expected-pom.xml create mode 100644 maven-release-manager/src/test/resources/projects/rewrite-for-development/pom-with-parent-and-cifriendly-revision/subproject1/pom.xml create mode 100644 maven-release-plugin/src/it/projects/prepare/ci-friendly-multi-module/invoker.properties diff --git a/maven-release-manager/src/main/java/org/apache/maven/shared/release/phase/AbstractRewritePomsPhase.java b/maven-release-manager/src/main/java/org/apache/maven/shared/release/phase/AbstractRewritePomsPhase.java index 3065ccc62..5df2aef3e 100644 --- a/maven-release-manager/src/main/java/org/apache/maven/shared/release/phase/AbstractRewritePomsPhase.java +++ b/maven-release-manager/src/main/java/org/apache/maven/shared/release/phase/AbstractRewritePomsPhase.java @@ -23,15 +23,12 @@ import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.ArrayList; -import java.util.Arrays; import java.util.Collection; import java.util.Date; import java.util.List; import java.util.Map; import java.util.Properties; import java.util.TimeZone; -import java.util.regex.Matcher; -import java.util.regex.Pattern; import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.ArtifactUtils; @@ -63,6 +60,7 @@ import org.apache.maven.shared.release.transform.ModelETLFactory; import org.apache.maven.shared.release.transform.ModelETLRequest; import org.apache.maven.shared.release.transform.jdom2.JDomModelETLFactory; +import org.apache.maven.shared.release.util.CiFriendlyVersion; import org.apache.maven.shared.release.util.ReleaseUtil; import org.codehaus.plexus.util.StringUtils; @@ -92,18 +90,6 @@ public abstract class AbstractRewritePomsPhase extends AbstractReleasePhase impl */ private String modelETL = JDomModelETLFactory.NAME; - /** - * Regular expression pattern matching Maven expressions (i.e. references to Maven properties). - * The first group selects the property name the expression refers to. - */ - private static final Pattern EXPRESSION_PATTERN = Pattern.compile("\\$\\{(.+?)\\}"); - - /** - * All Maven properties allowed to be referenced in parent versions via expressions - * @see CI-Friendly Versions - */ - private static final List CI_FRIENDLY_PROPERTIES = Arrays.asList("revision", "sha1", "changelist"); - private long startTime = -1 * 1000; protected AbstractRewritePomsPhase( @@ -455,23 +441,6 @@ private void rewriteVersion( modelTarget.setVersion(version); } - /** - * Extracts the Maven property name from a given expression. - * @param expression the expression - * @return either {@code null} if value is no expression otherwise the property referenced in the expression - */ - public static String extractPropertyFromExpression(String expression) { - Matcher matcher = EXPRESSION_PATTERN.matcher(expression); - if (!matcher.find()) { - return null; - } - return matcher.group(1); - } - - public static boolean isCiFriendlyVersion(String version) { - return CI_FRIENDLY_PROPERTIES.contains(extractPropertyFromExpression(version)); - } - private String rewriteParent( MavenProject project, Model targetModel, @@ -494,7 +463,8 @@ private String rewriteParent( throw new ReleaseFailureException("Version for parent '" + parent.getName() + "' was not mapped"); } } else { - if (!isCiFriendlyVersion(targetModel.getParent().getVersion())) { + if (!CiFriendlyVersion.isCiFriendlyVersion( + targetModel.getParent().getVersion())) { targetModel.getParent().setVersion(parentVersion); } else { logInfo( @@ -564,7 +534,7 @@ private void rewriteArtifactVersions( logInfo(result, " Updating " + artifactId + " to " + mappedVersion); coordinate.setVersion(mappedVersion); } else { - String property = extractPropertyFromExpression(rawVersion); + String property = CiFriendlyVersion.extractPropertyFromExpression(rawVersion); logInfo(result, "CI Friendly property " + property + " and rawVersion is " + rawVersion); if (property != null) { if (property.startsWith("project.") @@ -610,7 +580,7 @@ private void rewriteArtifactVersions( } } } else { - if (CI_FRIENDLY_PROPERTIES.contains(property)) { + if (CiFriendlyVersion.containsCiFriendlyProperties(property)) { // the parent's pom revision is set inside // org.apache.maven.shared.release.transform.jdom2.JDomModel.setVersion logInfo( diff --git a/maven-release-manager/src/main/java/org/apache/maven/shared/release/transform/jdom2/JDomModel.java b/maven-release-manager/src/main/java/org/apache/maven/shared/release/transform/jdom2/JDomModel.java index 4c131d21c..5eb5ca9da 100644 --- a/maven-release-manager/src/main/java/org/apache/maven/shared/release/transform/jdom2/JDomModel.java +++ b/maven-release-manager/src/main/java/org/apache/maven/shared/release/transform/jdom2/JDomModel.java @@ -23,7 +23,6 @@ import java.util.List; import java.util.Properties; -import org.apache.maven.artifact.ArtifactUtils; import org.apache.maven.model.Build; import org.apache.maven.model.Dependency; import org.apache.maven.model.DependencyManagement; @@ -32,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.shared.release.phase.AbstractRewritePomsPhase; +import org.apache.maven.shared.release.util.CiFriendlyVersion; import org.jdom2.Document; import org.jdom2.Element; import org.jdom2.Text; @@ -181,7 +180,7 @@ public void setVersion(String version) { if (versionElement == null) { // never add version when parent references CI friendly property - if (!(parentVersion != null && AbstractRewritePomsPhase.isCiFriendlyVersion(parentVersion)) + if (!(parentVersion != null && CiFriendlyVersion.isCiFriendlyVersion(parentVersion)) && !version.equals(parentVersion)) { // we will add this after artifactId, since it was missing but different from the inherited version Element artifactIdElement = project.getChild("artifactId", project.getNamespace()); @@ -193,24 +192,10 @@ public void setVersion(String version) { project.addContent(index + 2, versionElement); } } else { - if (AbstractRewritePomsPhase.isCiFriendlyVersion(versionElement.getTextNormalize())) { + if (CiFriendlyVersion.isCiFriendlyVersion(versionElement.getTextNormalize())) { // try to rewrite property if CI friendly expression is used - String ciFriendlyPropertyName = - AbstractRewritePomsPhase.extractPropertyFromExpression(versionElement.getTextNormalize()); - Properties properties = getProperties(); - if (properties != null) { - String sha1 = properties.getProperty("sha1", ""); - String changelist = properties.getProperty("changelist", ""); - properties.setProperty( - ciFriendlyPropertyName, - // assume that everybody follows the example and properties are simply chained - version.replaceAll(sha1, "").replaceAll(changelist, "")); - if (ArtifactUtils.isSnapshot(version)) { - properties.setProperty("changelist", changelist); - } else { - properties.setProperty("changelist", ""); - } - } + CiFriendlyVersion.rewriteVersionAndProperties( + version, versionElement.getTextNormalize(), getProperties()); } else { JDomUtils.rewriteValue(versionElement, version); } diff --git a/maven-release-manager/src/main/java/org/apache/maven/shared/release/transform/jdom2/JDomProperties.java b/maven-release-manager/src/main/java/org/apache/maven/shared/release/transform/jdom2/JDomProperties.java index 0ffac3186..f689ff451 100644 --- a/maven-release-manager/src/main/java/org/apache/maven/shared/release/transform/jdom2/JDomProperties.java +++ b/maven-release-manager/src/main/java/org/apache/maven/shared/release/transform/jdom2/JDomProperties.java @@ -51,13 +51,13 @@ public JDomProperties(Element properties) { } @Override - public synchronized Object setProperty(String key, String value) { - Element property = properties.getChild(key, properties.getNamespace()); + public synchronized Object put(Object key, Object value) { + Element property = properties.getChild((String) key, properties.getNamespace()); if (property == null) { - property = new Element(key, properties.getNamespace()); + property = new Element((String) key, properties.getNamespace()); } - JDomUtils.rewriteValue(property, value); + JDomUtils.rewriteValue(property, (String) value); // todo follow specs of Hashtable.put return null; @@ -115,6 +115,15 @@ public String getProperty(String key) { } } + @Override + public boolean containsKey(Object key) { + if (key instanceof String) { + Element property = properties.getChild((String) key, properties.getNamespace()); + return property != null; + } + return false; + } + @Override public String getProperty(String key, String defaultValue) { String property = getProperty(key); diff --git a/maven-release-manager/src/main/java/org/apache/maven/shared/release/util/CiFriendlyVersion.java b/maven-release-manager/src/main/java/org/apache/maven/shared/release/util/CiFriendlyVersion.java new file mode 100644 index 000000000..fe5bad8e4 --- /dev/null +++ b/maven-release-manager/src/main/java/org/apache/maven/shared/release/util/CiFriendlyVersion.java @@ -0,0 +1,96 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.maven.shared.release.util; + +import java.util.Arrays; +import java.util.List; +import java.util.Properties; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import org.apache.maven.artifact.ArtifactUtils; + +public class CiFriendlyVersion { + + /** + * Regular expression pattern matching Maven expressions (i.e. references to Maven properties). + * The first group selects the property name the expression refers to. + */ + private static final Pattern EXPRESSION_PATTERN = Pattern.compile("\\$\\{(.+?)\\}"); + + /** + * All Maven properties allowed to be referenced in parent versions via expressions + * @see CI-Friendly Versions + */ + private static final String REVISION = "revision"; + + private static final String SHA_1 = "sha1"; + private static final String CHANGELIST = "changelist"; + private static final List CI_FRIENDLY_PROPERTIES = Arrays.asList(REVISION, SHA_1, CHANGELIST); + + private static final String SNAPSHOT = "-SNAPSHOT"; + + private CiFriendlyVersion() {} + + /** + * Extracts the Maven property name from a given expression. + * @param expression the expression + * @return either {@code null} if value is no expression otherwise the property referenced in the expression + */ + public static String extractPropertyFromExpression(String expression) { + Matcher matcher = EXPRESSION_PATTERN.matcher(expression); + if (!matcher.find()) { + return null; + } + return matcher.group(1); + } + + public static boolean isCiFriendlyVersion(String version) { + return containsCiFriendlyProperties(extractPropertyFromExpression(version)); + } + + public static boolean containsCiFriendlyProperties(String property) { + return CI_FRIENDLY_PROPERTIES.contains(property); + } + + public static void rewriteVersionAndProperties(String version, String versionElement, Properties properties) { + // try to rewrite property if CI friendly expression is used + String ciFriendlyPropertyName = extractPropertyFromExpression(versionElement); + if (properties != null) { + String sha1 = properties.getProperty(SHA_1, System.getProperty(SHA_1, "")); + // assume that everybody follows the example and properties are simply chained + // and the changelist can only be '-SNAPSHOT' + if (ArtifactUtils.isSnapshot(version)) { + if (properties.containsKey(CHANGELIST)) { + properties.setProperty( + ciFriendlyPropertyName, version.replace(sha1, "").replace(SNAPSHOT, "")); + properties.setProperty(CHANGELIST, SNAPSHOT); + } else { + properties.setProperty(ciFriendlyPropertyName, version.replace(sha1, "")); + } + } else { + properties.setProperty( + ciFriendlyPropertyName, version.replace(sha1, "").replace(SNAPSHOT, "")); + if (properties.containsKey(CHANGELIST)) { + properties.setProperty(CHANGELIST, ""); + } + } + } + } +} diff --git a/maven-release-manager/src/test/java/org/apache/maven/shared/release/phase/RewritePomsForDevelopmentPhaseTest.java b/maven-release-manager/src/test/java/org/apache/maven/shared/release/phase/RewritePomsForDevelopmentPhaseTest.java index 59cc47c3d..366d04f41 100644 --- a/maven-release-manager/src/test/java/org/apache/maven/shared/release/phase/RewritePomsForDevelopmentPhaseTest.java +++ b/maven-release-manager/src/test/java/org/apache/maven/shared/release/phase/RewritePomsForDevelopmentPhaseTest.java @@ -416,4 +416,19 @@ public void testRewritePomWithCiFriendlyReactor() throws Exception { assertTrue(comparePomFiles(reactorProjects)); } + + @Test + public void testRewritePomWithCiFriendlyReactorWithOnlyRevision() throws Exception { + List reactorProjects = createReactorProjects("pom-with-parent-and-cifriendly-revision"); + + ReleaseDescriptorBuilder builder = + createDescriptorFromProjects(reactorProjects, "pom-with-parent-and-cifriendly-revision"); + builder.addReleaseVersion("groupId:artifactId", RELEASE_VERSION); + builder.addDevelopmentVersion("groupId:artifactId", NEXT_VERSION); + builder.addReleaseVersion("groupId:subproject1", RELEASE_VERSION); + builder.addDevelopmentVersion("groupId:subproject1", NEXT_VERSION); + phase.execute(ReleaseUtils.buildReleaseDescriptor(builder), new DefaultReleaseEnvironment(), reactorProjects); + + assertTrue(comparePomFiles(reactorProjects)); + } } diff --git a/maven-release-manager/src/test/resources/projects/rewrite-for-development/pom-with-parent-and-cifriendly-expressions/expected-pom.xml b/maven-release-manager/src/test/resources/projects/rewrite-for-development/pom-with-parent-and-cifriendly-expressions/expected-pom.xml index d2fe2b7a2..60b7355ba 100644 --- a/maven-release-manager/src/test/resources/projects/rewrite-for-development/pom-with-parent-and-cifriendly-expressions/expected-pom.xml +++ b/maven-release-manager/src/test/resources/projects/rewrite-for-development/pom-with-parent-and-cifriendly-expressions/expected-pom.xml @@ -31,7 +31,8 @@ 1.1 - .123 + + -SNAPSHOT diff --git a/maven-release-manager/src/test/resources/projects/rewrite-for-development/pom-with-parent-and-cifriendly-expressions/pom.xml b/maven-release-manager/src/test/resources/projects/rewrite-for-development/pom-with-parent-and-cifriendly-expressions/pom.xml index e42e87c9f..8b16adc12 100644 --- a/maven-release-manager/src/test/resources/projects/rewrite-for-development/pom-with-parent-and-cifriendly-expressions/pom.xml +++ b/maven-release-manager/src/test/resources/projects/rewrite-for-development/pom-with-parent-and-cifriendly-expressions/pom.xml @@ -31,8 +31,9 @@ 1.0 - .123 - -SNAPSHOT + + + diff --git a/maven-release-manager/src/test/resources/projects/rewrite-for-development/pom-with-parent-and-cifriendly-revision/expected-pom.xml b/maven-release-manager/src/test/resources/projects/rewrite-for-development/pom-with-parent-and-cifriendly-revision/expected-pom.xml new file mode 100644 index 000000000..04e6e68f1 --- /dev/null +++ b/maven-release-manager/src/test/resources/projects/rewrite-for-development/pom-with-parent-and-cifriendly-revision/expected-pom.xml @@ -0,0 +1,39 @@ + + + + + + 4.0.0 + groupId + artifactId + ${revision} + pom + + + scm:svn:file://localhost/tmp/scm-repo/tags/release-label + scm:svn:file://localhost/tmp/scm-repo/tags/release-label + file://localhost/tmp/scm-repo/tags/release-label + + + + 1.1-SNAPSHOT + + + + subproject1 + + diff --git a/maven-release-manager/src/test/resources/projects/rewrite-for-development/pom-with-parent-and-cifriendly-revision/pom.xml b/maven-release-manager/src/test/resources/projects/rewrite-for-development/pom-with-parent-and-cifriendly-revision/pom.xml new file mode 100644 index 000000000..b4326d54a --- /dev/null +++ b/maven-release-manager/src/test/resources/projects/rewrite-for-development/pom-with-parent-and-cifriendly-revision/pom.xml @@ -0,0 +1,39 @@ + + + + + + 4.0.0 + groupId + artifactId + ${revision} + pom + + + scm:svn:file://localhost/tmp/scm-repo/tags/release-label + scm:svn:file://localhost/tmp/scm-repo/tags/release-label + file://localhost/tmp/scm-repo/tags/release-label + + + + 1.0 + + + + subproject1 + + diff --git a/maven-release-manager/src/test/resources/projects/rewrite-for-development/pom-with-parent-and-cifriendly-revision/subproject1/expected-pom.xml b/maven-release-manager/src/test/resources/projects/rewrite-for-development/pom-with-parent-and-cifriendly-revision/subproject1/expected-pom.xml new file mode 100644 index 000000000..7b2fa88c1 --- /dev/null +++ b/maven-release-manager/src/test/resources/projects/rewrite-for-development/pom-with-parent-and-cifriendly-revision/subproject1/expected-pom.xml @@ -0,0 +1,28 @@ + + + + + + 4.0.0 + + groupId + artifactId + ${revision} + + + subproject1 + diff --git a/maven-release-manager/src/test/resources/projects/rewrite-for-development/pom-with-parent-and-cifriendly-revision/subproject1/pom.xml b/maven-release-manager/src/test/resources/projects/rewrite-for-development/pom-with-parent-and-cifriendly-revision/subproject1/pom.xml new file mode 100644 index 000000000..7b2fa88c1 --- /dev/null +++ b/maven-release-manager/src/test/resources/projects/rewrite-for-development/pom-with-parent-and-cifriendly-revision/subproject1/pom.xml @@ -0,0 +1,28 @@ + + + + + + 4.0.0 + + groupId + artifactId + ${revision} + + + subproject1 + diff --git a/maven-release-plugin/src/it/projects/prepare/ci-friendly-multi-module/invoker.properties b/maven-release-plugin/src/it/projects/prepare/ci-friendly-multi-module/invoker.properties new file mode 100644 index 000000000..0f980493f --- /dev/null +++ b/maven-release-plugin/src/it/projects/prepare/ci-friendly-multi-module/invoker.properties @@ -0,0 +1,19 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +invoker.goals=release:clean release:prepare +invoker.mavenOpts=-Dsha1=-123 \ No newline at end of file diff --git a/maven-release-plugin/src/it/projects/prepare/ci-friendly-multi-module/module-a/pom.xml b/maven-release-plugin/src/it/projects/prepare/ci-friendly-multi-module/module-a/pom.xml index 1a042b0ca..14c54172a 100644 --- a/maven-release-plugin/src/it/projects/prepare/ci-friendly-multi-module/module-a/pom.xml +++ b/maven-release-plugin/src/it/projects/prepare/ci-friendly-multi-module/module-a/pom.xml @@ -21,7 +21,7 @@ under the License. ci-friendly-multi-module-project org.apache.maven.plugin.release - ${revision}${changelist} + ${revision}${sha1}${changelist} 4.0.0 org.apache.maven.plugin.release diff --git a/maven-release-plugin/src/it/projects/prepare/ci-friendly-multi-module/module-b/pom.xml b/maven-release-plugin/src/it/projects/prepare/ci-friendly-multi-module/module-b/pom.xml index f7fec33f1..e44304546 100644 --- a/maven-release-plugin/src/it/projects/prepare/ci-friendly-multi-module/module-b/pom.xml +++ b/maven-release-plugin/src/it/projects/prepare/ci-friendly-multi-module/module-b/pom.xml @@ -21,7 +21,7 @@ under the License. ci-friendly-multi-module-project org.apache.maven.plugin.release - ${revision}${changelist} + ${revision}${sha1}${changelist} 4.0.0 org.apache.maven.plugin.release diff --git a/maven-release-plugin/src/it/projects/prepare/ci-friendly-multi-module/pom.xml b/maven-release-plugin/src/it/projects/prepare/ci-friendly-multi-module/pom.xml index ff2ffc91a..8d140e272 100644 --- a/maven-release-plugin/src/it/projects/prepare/ci-friendly-multi-module/pom.xml +++ b/maven-release-plugin/src/it/projects/prepare/ci-friendly-multi-module/pom.xml @@ -22,7 +22,7 @@ org.apache.maven.plugin.release ci-friendly-multi-module-project pom - ${revision}${changelist} + ${revision}${sha1}${changelist} parent-project http://maven.apache.org @@ -33,6 +33,7 @@ 1.0 + -SNAPSHOT @maven.compiler.source@ @maven.compiler.target@ diff --git a/maven-release-plugin/src/it/projects/prepare/ci-friendly-multi-module/verify.groovy b/maven-release-plugin/src/it/projects/prepare/ci-friendly-multi-module/verify.groovy index ed53638ed..4486794e6 100644 --- a/maven-release-plugin/src/it/projects/prepare/ci-friendly-multi-module/verify.groovy +++ b/maven-release-plugin/src/it/projects/prepare/ci-friendly-multi-module/verify.groovy @@ -24,27 +24,29 @@ assert buildLog.exists() // tag versions def projectRootTag = new XmlSlurper().parse( new File( basedir, 'pom.xml.tag' ) ) -assert projectRootTag.version.text() == '${revision}${changelist}' +assert projectRootTag.version.text() == '${revision}${sha1}${changelist}' assert projectRootTag.properties.revision.text() == "1.0" +assert projectRootTag.properties.sha1.text() == "" assert projectRootTag.properties.changelist.text() == "" def projectATag = new XmlSlurper().parse( new File( basedir, 'module-a/pom.xml.tag' ) ) -assert projectATag.parent.version.text() == '${revision}${changelist}' +assert projectATag.parent.version.text() == '${revision}${sha1}${changelist}' def projectBTag = new XmlSlurper().parse( new File( basedir, 'module-b/pom.xml.tag' ) ) -assert projectBTag.parent.version.text() == '${revision}${changelist}' +assert projectBTag.parent.version.text() == '${revision}${sha1}${changelist}' // next development versions def projectRoot = new XmlSlurper().parse( new File( basedir, 'pom.xml.next' ) ) -assert projectRoot.version.text() == '${revision}${changelist}' +assert projectRoot.version.text() == '${revision}${sha1}${changelist}' assert projectRoot.properties.revision.text() == "1.1" +assert projectRoot.properties.sha1.text() == "" assert projectRoot.properties.changelist.text() == "-SNAPSHOT" def projectA = new XmlSlurper().parse( new File( basedir, 'module-a/pom.xml.next' ) ) -assert projectA.parent.version.text() == '${revision}${changelist}' +assert projectA.parent.version.text() == '${revision}${sha1}${changelist}' def projectB = new XmlSlurper().parse( new File( basedir, 'module-b/pom.xml.next' ) ) -assert projectB.parent.version.text() == '${revision}${changelist}' +assert projectB.parent.version.text() == '${revision}${sha1}${changelist}' return true From 6b4fc8103fbb6862123053b3257efa8ea06f13ea Mon Sep 17 00:00:00 2001 From: mkolesnikov Date: Mon, 3 Jun 2024 18:24:36 +0300 Subject: [PATCH 09/15] MRELEASE-1109 removed redundant comment --- .../maven/shared/release/phase/AbstractRewritePomsPhase.java | 4 ---- 1 file changed, 4 deletions(-) diff --git a/maven-release-manager/src/main/java/org/apache/maven/shared/release/phase/AbstractRewritePomsPhase.java b/maven-release-manager/src/main/java/org/apache/maven/shared/release/phase/AbstractRewritePomsPhase.java index 5df2aef3e..b93a41ad4 100644 --- a/maven-release-manager/src/main/java/org/apache/maven/shared/release/phase/AbstractRewritePomsPhase.java +++ b/maven-release-manager/src/main/java/org/apache/maven/shared/release/phase/AbstractRewritePomsPhase.java @@ -602,10 +602,6 @@ private void rewriteArtifactVersions( } } else { // different/previous version not related to current release - // this is the only place where the returned null from `extractPropertyFromExpression` is - // supposed to be handled. - // And the unit test RewritePomsForBranchPhaseTest depends on null result from - // `extractPropertyFromExpression`. } } } else if (resolvedSnapshotVersion != null) { From fbc3ac22b79298d34317ed0e0b459cc8ef02e05c Mon Sep 17 00:00:00 2001 From: mkolesnikov Date: Tue, 4 Jun 2024 12:25:05 +0300 Subject: [PATCH 10/15] MRELEASE-1109 renamed SHA1 constant --- .../maven/shared/release/util/CiFriendlyVersion.java | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/maven-release-manager/src/main/java/org/apache/maven/shared/release/util/CiFriendlyVersion.java b/maven-release-manager/src/main/java/org/apache/maven/shared/release/util/CiFriendlyVersion.java index fe5bad8e4..8b3369655 100644 --- a/maven-release-manager/src/main/java/org/apache/maven/shared/release/util/CiFriendlyVersion.java +++ b/maven-release-manager/src/main/java/org/apache/maven/shared/release/util/CiFriendlyVersion.java @@ -19,8 +19,9 @@ package org.apache.maven.shared.release.util; import java.util.Arrays; -import java.util.List; +import java.util.HashSet; import java.util.Properties; +import java.util.Set; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -40,9 +41,9 @@ public class CiFriendlyVersion { */ private static final String REVISION = "revision"; - private static final String SHA_1 = "sha1"; + private static final String SHA1 = "sha1"; private static final String CHANGELIST = "changelist"; - private static final List CI_FRIENDLY_PROPERTIES = Arrays.asList(REVISION, SHA_1, CHANGELIST); + private static final Set CI_FRIENDLY_PROPERTIES = new HashSet<>(Arrays.asList(REVISION, SHA1, CHANGELIST)); private static final String SNAPSHOT = "-SNAPSHOT"; @@ -73,7 +74,7 @@ public static void rewriteVersionAndProperties(String version, String versionEle // try to rewrite property if CI friendly expression is used String ciFriendlyPropertyName = extractPropertyFromExpression(versionElement); if (properties != null) { - String sha1 = properties.getProperty(SHA_1, System.getProperty(SHA_1, "")); + String sha1 = properties.getProperty(SHA1, System.getProperty(SHA1, "")); // assume that everybody follows the example and properties are simply chained // and the changelist can only be '-SNAPSHOT' if (ArtifactUtils.isSnapshot(version)) { From 0be8e5a7207e7259247a318e29c3983268be4612 Mon Sep 17 00:00:00 2001 From: mkolesnikov Date: Tue, 4 Jun 2024 14:01:04 +0300 Subject: [PATCH 11/15] MRELEASE-1109 JDomModel should know about current MavenProject --- .../release/transform/jdom2/JDomModel.java | 18 +++++++++++---- .../release/transform/jdom2/JDomModelETL.java | 2 +- .../release/util/CiFriendlyVersion.java | 22 ++++++++++--------- .../transform/jdom2/JDomModelTest.java | 12 +++++----- 4 files changed, 34 insertions(+), 20 deletions(-) diff --git a/maven-release-manager/src/main/java/org/apache/maven/shared/release/transform/jdom2/JDomModel.java b/maven-release-manager/src/main/java/org/apache/maven/shared/release/transform/jdom2/JDomModel.java index 5eb5ca9da..c60d99363 100644 --- a/maven-release-manager/src/main/java/org/apache/maven/shared/release/transform/jdom2/JDomModel.java +++ b/maven-release-manager/src/main/java/org/apache/maven/shared/release/transform/jdom2/JDomModel.java @@ -31,6 +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.util.CiFriendlyVersion; import org.jdom2.Document; import org.jdom2.Element; @@ -47,13 +48,18 @@ public class JDomModel extends Model { private final JDomModelBase modelBase; + /** + * The currently running MavenProject + */ + private final MavenProject mavenProject; + /** *

Constructor for JDomModel.

* * @param document a {@link org.jdom2.Document} object */ - public JDomModel(Document document) { - this(document.getRootElement()); + public JDomModel(Document document, MavenProject mavenProject) { + this(document.getRootElement(), mavenProject); } /** @@ -61,8 +67,9 @@ public JDomModel(Document document) { * * @param project a {@link org.jdom2.Element} object */ - public JDomModel(Element project) { + public JDomModel(Element project, MavenProject mavenProject) { this.project = project; + this.mavenProject = mavenProject; this.modelBase = new JDomModelBase(project); } @@ -195,7 +202,10 @@ public void setVersion(String version) { if (CiFriendlyVersion.isCiFriendlyVersion(versionElement.getTextNormalize())) { // try to rewrite property if CI friendly expression is used CiFriendlyVersion.rewriteVersionAndProperties( - version, versionElement.getTextNormalize(), getProperties()); + version, + versionElement.getTextNormalize(), + (JDomProperties) getProperties(), + mavenProject.getProperties()); } else { JDomUtils.rewriteValue(versionElement, version); } diff --git a/maven-release-manager/src/main/java/org/apache/maven/shared/release/transform/jdom2/JDomModelETL.java b/maven-release-manager/src/main/java/org/apache/maven/shared/release/transform/jdom2/JDomModelETL.java index 86bf8e272..e8d740967 100644 --- a/maven-release-manager/src/main/java/org/apache/maven/shared/release/transform/jdom2/JDomModelETL.java +++ b/maven-release-manager/src/main/java/org/apache/maven/shared/release/transform/jdom2/JDomModelETL.java @@ -159,7 +159,7 @@ public void load(File targetFile) throws ReleaseExecutionException { @Override public Model getModel() { - return new JDomModel(document); + return new JDomModel(document, project); } private void normaliseLineEndings(Document document) { diff --git a/maven-release-manager/src/main/java/org/apache/maven/shared/release/util/CiFriendlyVersion.java b/maven-release-manager/src/main/java/org/apache/maven/shared/release/util/CiFriendlyVersion.java index 8b3369655..406b015a3 100644 --- a/maven-release-manager/src/main/java/org/apache/maven/shared/release/util/CiFriendlyVersion.java +++ b/maven-release-manager/src/main/java/org/apache/maven/shared/release/util/CiFriendlyVersion.java @@ -26,6 +26,7 @@ import java.util.regex.Pattern; import org.apache.maven.artifact.ArtifactUtils; +import org.apache.maven.shared.release.transform.jdom2.JDomProperties; public class CiFriendlyVersion { @@ -70,26 +71,27 @@ public static boolean containsCiFriendlyProperties(String property) { return CI_FRIENDLY_PROPERTIES.contains(property); } - public static void rewriteVersionAndProperties(String version, String versionElement, Properties properties) { + public static void rewriteVersionAndProperties( + String version, String versionElement, JDomProperties jDomProperties, Properties mavenProperties) { // try to rewrite property if CI friendly expression is used String ciFriendlyPropertyName = extractPropertyFromExpression(versionElement); - if (properties != null) { - String sha1 = properties.getProperty(SHA1, System.getProperty(SHA1, "")); + if (jDomProperties != null) { + String sha1 = jDomProperties.getProperty(SHA1, mavenProperties.getProperty(SHA1, "")); // assume that everybody follows the example and properties are simply chained // and the changelist can only be '-SNAPSHOT' if (ArtifactUtils.isSnapshot(version)) { - if (properties.containsKey(CHANGELIST)) { - properties.setProperty( + if (jDomProperties.containsKey(CHANGELIST)) { + jDomProperties.setProperty( ciFriendlyPropertyName, version.replace(sha1, "").replace(SNAPSHOT, "")); - properties.setProperty(CHANGELIST, SNAPSHOT); + jDomProperties.setProperty(CHANGELIST, SNAPSHOT); } else { - properties.setProperty(ciFriendlyPropertyName, version.replace(sha1, "")); + jDomProperties.setProperty(ciFriendlyPropertyName, version.replace(sha1, "")); } } else { - properties.setProperty( + jDomProperties.setProperty( ciFriendlyPropertyName, version.replace(sha1, "").replace(SNAPSHOT, "")); - if (properties.containsKey(CHANGELIST)) { - properties.setProperty(CHANGELIST, ""); + if (jDomProperties.containsKey(CHANGELIST)) { + jDomProperties.setProperty(CHANGELIST, ""); } } } diff --git a/maven-release-manager/src/test/java/org/apache/maven/shared/release/transform/jdom2/JDomModelTest.java b/maven-release-manager/src/test/java/org/apache/maven/shared/release/transform/jdom2/JDomModelTest.java index abe03d6cc..5849121cc 100644 --- a/maven-release-manager/src/test/java/org/apache/maven/shared/release/transform/jdom2/JDomModelTest.java +++ b/maven-release-manager/src/test/java/org/apache/maven/shared/release/transform/jdom2/JDomModelTest.java @@ -22,6 +22,7 @@ import org.apache.maven.model.Model; import org.apache.maven.model.Scm; +import org.apache.maven.project.MavenProject; import org.jdom2.Document; import org.jdom2.Element; import org.jdom2.input.SAXBuilder; @@ -33,19 +34,20 @@ public class JDomModelTest { private SAXBuilder builder = new SAXBuilder(); + private MavenProject mavenProject = new MavenProject(); @Test public void testGetScm() throws Exception { String content = ""; Document document = builder.build(new StringReader(content)); - assertNull(new JDomModel(document).getScm()); + assertNull(new JDomModel(document, mavenProject).getScm()); } @Test public void testSetScm() throws Exception { String content = ""; Document document = builder.build(new StringReader(content)); - Model model = new JDomModel(document); + Model model = new JDomModel(document, mavenProject); assertNull(model.getScm()); model.setScm(new Scm()); @@ -59,7 +61,7 @@ public void testSetScm() throws Exception { public void testSetVersion() throws Exception { String content = ""; Element projectElm = builder.build(new StringReader(content)).getRootElement(); - Model model = new JDomModel(projectElm); + Model model = new JDomModel(projectElm, mavenProject); assertNull(model.getVersion()); model.setVersion("VERSION"); @@ -71,7 +73,7 @@ public void testSetVersion() throws Exception { // inherit from parent via CI friendly content = "${revision}${changelist}"; projectElm = builder.build(new StringReader(content)).getRootElement(); - model = new JDomModel(projectElm); + model = new JDomModel(projectElm, mavenProject); assertNull(model.getVersion()); model.setVersion("PARENT_VERSION"); assertNull(getVersion(projectElm)); @@ -79,7 +81,7 @@ public void testSetVersion() throws Exception { // this business logic might need to moved. content = "PARENT_VERSION"; projectElm = builder.build(new StringReader(content)).getRootElement(); - model = new JDomModel(projectElm); + model = new JDomModel(projectElm, mavenProject); assertNull(model.getVersion()); model.setVersion("PARENT_VERSION"); From 56fe3ed2d4d9df18d237cbecaef14365b00e1798 Mon Sep 17 00:00:00 2001 From: mkolesnikov Date: Wed, 5 Jun 2024 03:21:12 +0300 Subject: [PATCH 12/15] MRELEASE-1109 Using scmRevision as sha1 value if the property defined but nothing was provided by a user --- .../release/transform/jdom2/JDomModel.java | 17 +++++++------- .../release/transform/jdom2/JDomModelETL.java | 2 +- .../release/util/CiFriendlyVersion.java | 22 ++++++++++++++++--- .../transform/jdom2/JDomModelTest.java | 15 +++++++------ .../invoker.properties | 2 +- .../ci-friendly-multi-module/verify.groovy | 2 +- 6 files changed, 39 insertions(+), 21 deletions(-) diff --git a/maven-release-manager/src/main/java/org/apache/maven/shared/release/transform/jdom2/JDomModel.java b/maven-release-manager/src/main/java/org/apache/maven/shared/release/transform/jdom2/JDomModel.java index c60d99363..c6b153d97 100644 --- a/maven-release-manager/src/main/java/org/apache/maven/shared/release/transform/jdom2/JDomModel.java +++ b/maven-release-manager/src/main/java/org/apache/maven/shared/release/transform/jdom2/JDomModel.java @@ -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,17 +49,18 @@ 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; /** *

Constructor for JDomModel.

* * @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); } /** @@ -67,9 +68,9 @@ public JDomModel(Document document, MavenProject mavenProject) { * * @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); } diff --git a/maven-release-manager/src/main/java/org/apache/maven/shared/release/transform/jdom2/JDomModelETL.java b/maven-release-manager/src/main/java/org/apache/maven/shared/release/transform/jdom2/JDomModelETL.java index e8d740967..640f574e0 100644 --- a/maven-release-manager/src/main/java/org/apache/maven/shared/release/transform/jdom2/JDomModelETL.java +++ b/maven-release-manager/src/main/java/org/apache/maven/shared/release/transform/jdom2/JDomModelETL.java @@ -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) { diff --git a/maven-release-manager/src/main/java/org/apache/maven/shared/release/util/CiFriendlyVersion.java b/maven-release-manager/src/main/java/org/apache/maven/shared/release/util/CiFriendlyVersion.java index 406b015a3..3eaeaa926 100644 --- a/maven-release-manager/src/main/java/org/apache/maven/shared/release/util/CiFriendlyVersion.java +++ b/maven-release-manager/src/main/java/org/apache/maven/shared/release/util/CiFriendlyVersion.java @@ -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 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)); + } } diff --git a/maven-release-manager/src/test/java/org/apache/maven/shared/release/transform/jdom2/JDomModelTest.java b/maven-release-manager/src/test/java/org/apache/maven/shared/release/transform/jdom2/JDomModelTest.java index 5849121cc..3967274b1 100644 --- a/maven-release-manager/src/test/java/org/apache/maven/shared/release/transform/jdom2/JDomModelTest.java +++ b/maven-release-manager/src/test/java/org/apache/maven/shared/release/transform/jdom2/JDomModelTest.java @@ -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 = ""; 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 = ""; 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 = ""; 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,7 +74,7 @@ public void testSetVersion() throws Exception { // inherit from parent via CI friendly content = "${revision}${changelist}"; 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)); @@ -81,7 +82,7 @@ public void testSetVersion() throws Exception { // this business logic might need to moved. content = "PARENT_VERSION"; projectElm = builder.build(new StringReader(content)).getRootElement(); - model = new JDomModel(projectElm, mavenProject); + model = new JDomModel(projectElm, releaseDescriptor); assertNull(model.getVersion()); model.setVersion("PARENT_VERSION"); diff --git a/maven-release-plugin/src/it/projects/prepare/ci-friendly-multi-module/invoker.properties b/maven-release-plugin/src/it/projects/prepare/ci-friendly-multi-module/invoker.properties index 0f980493f..652188646 100644 --- a/maven-release-plugin/src/it/projects/prepare/ci-friendly-multi-module/invoker.properties +++ b/maven-release-plugin/src/it/projects/prepare/ci-friendly-multi-module/invoker.properties @@ -16,4 +16,4 @@ # under the License. invoker.goals=release:clean release:prepare -invoker.mavenOpts=-Dsha1=-123 \ No newline at end of file +invoker.mavenOpts=-Dsha1=-abcdef12 -agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=*:5005 \ No newline at end of file diff --git a/maven-release-plugin/src/it/projects/prepare/ci-friendly-multi-module/verify.groovy b/maven-release-plugin/src/it/projects/prepare/ci-friendly-multi-module/verify.groovy index 4486794e6..febfa0cee 100644 --- a/maven-release-plugin/src/it/projects/prepare/ci-friendly-multi-module/verify.groovy +++ b/maven-release-plugin/src/it/projects/prepare/ci-friendly-multi-module/verify.groovy @@ -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' ) ) From 28ebea02826a9b63a2c050dd39ce2f062c94f19a Mon Sep 17 00:00:00 2001 From: mkolesnikov Date: Wed, 5 Jun 2024 12:32:56 +0300 Subject: [PATCH 13/15] MRELEASE-1109 fixed integration test --- .../org/apache/maven/shared/release/util/CiFriendlyVersion.java | 2 +- .../prepare/ci-friendly-multi-module/invoker.properties | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/maven-release-manager/src/main/java/org/apache/maven/shared/release/util/CiFriendlyVersion.java b/maven-release-manager/src/main/java/org/apache/maven/shared/release/util/CiFriendlyVersion.java index 3eaeaa926..ca3d98093 100644 --- a/maven-release-manager/src/main/java/org/apache/maven/shared/release/util/CiFriendlyVersion.java +++ b/maven-release-manager/src/main/java/org/apache/maven/shared/release/util/CiFriendlyVersion.java @@ -110,6 +110,6 @@ public static void rewriteVersionAndProperties( private static String resolveSha1Property(JDomProperties jDomProperties, ReleaseDescriptor releaseDescriptor) { String scmVersion = releaseDescriptor.getScmReleasedPomRevision(); - return jDomProperties.getProperty(SHA1, System.getProperty(SHA1, scmVersion == null ? "" : scmVersion)); + return System.getProperty(SHA1, scmVersion == null ? "" : scmVersion); } } diff --git a/maven-release-plugin/src/it/projects/prepare/ci-friendly-multi-module/invoker.properties b/maven-release-plugin/src/it/projects/prepare/ci-friendly-multi-module/invoker.properties index 652188646..c8e7f246d 100644 --- a/maven-release-plugin/src/it/projects/prepare/ci-friendly-multi-module/invoker.properties +++ b/maven-release-plugin/src/it/projects/prepare/ci-friendly-multi-module/invoker.properties @@ -16,4 +16,4 @@ # under the License. invoker.goals=release:clean release:prepare -invoker.mavenOpts=-Dsha1=-abcdef12 -agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=*:5005 \ No newline at end of file +invoker.mavenOpts=-Dsha1=-abcdef12 \ No newline at end of file From fe02232e3e562b8f3746292561a79a5fb240b84c Mon Sep 17 00:00:00 2001 From: mkolesnikov Date: Wed, 5 Jun 2024 12:34:42 +0300 Subject: [PATCH 14/15] MRELEASE-1109 clean up and format --- .../release/transform/jdom2/JDomModel.java | 5 +-- .../release/util/CiFriendlyVersion.java | 32 +++++++++---------- 2 files changed, 17 insertions(+), 20 deletions(-) diff --git a/maven-release-manager/src/main/java/org/apache/maven/shared/release/transform/jdom2/JDomModel.java b/maven-release-manager/src/main/java/org/apache/maven/shared/release/transform/jdom2/JDomModel.java index c6b153d97..20ee84339 100644 --- a/maven-release-manager/src/main/java/org/apache/maven/shared/release/transform/jdom2/JDomModel.java +++ b/maven-release-manager/src/main/java/org/apache/maven/shared/release/transform/jdom2/JDomModel.java @@ -203,10 +203,7 @@ public void setVersion(String version) { if (CiFriendlyVersion.isCiFriendlyVersion(versionElement.getTextNormalize())) { // try to rewrite property if CI friendly expression is used CiFriendlyVersion.rewriteVersionAndProperties( - version, - versionElement.getTextNormalize(), - (JDomProperties) getProperties(), - releaseDescriptor); + version, versionElement.getTextNormalize(), getProperties(), releaseDescriptor); } else { JDomUtils.rewriteValue(versionElement, version); } diff --git a/maven-release-manager/src/main/java/org/apache/maven/shared/release/util/CiFriendlyVersion.java b/maven-release-manager/src/main/java/org/apache/maven/shared/release/util/CiFriendlyVersion.java index ca3d98093..6b812a402 100644 --- a/maven-release-manager/src/main/java/org/apache/maven/shared/release/util/CiFriendlyVersion.java +++ b/maven-release-manager/src/main/java/org/apache/maven/shared/release/util/CiFriendlyVersion.java @@ -20,13 +20,13 @@ 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,43 +72,43 @@ public static boolean containsCiFriendlyProperties(String property) { } public static void rewriteVersionAndProperties( - String version, String versionElement, JDomProperties jDomProperties, ReleaseDescriptor releaseDescriptor) { + String version, String versionElement, Properties properties, ReleaseDescriptor releaseDescriptor) { // try to rewrite property if CI friendly expression is used String ciFriendlyPropertyName = extractPropertyFromExpression(versionElement); - if (jDomProperties != null) { - String sha1 = resolveSha1Property(jDomProperties, releaseDescriptor); + if (properties != null) { + String sha1 = resolveSha1Property(releaseDescriptor); // assume that everybody follows the example and properties are simply chained // and the changelist can only be '-SNAPSHOT' if (ArtifactUtils.isSnapshot(version)) { - if (jDomProperties.containsKey(CHANGELIST)) { - jDomProperties.setProperty( + if (properties.containsKey(CHANGELIST)) { + properties.setProperty( ciFriendlyPropertyName, version.replace(sha1, "").replace(SNAPSHOT, "")); - jDomProperties.setProperty(CHANGELIST, SNAPSHOT); + properties.setProperty(CHANGELIST, SNAPSHOT); } else { - jDomProperties.setProperty(ciFriendlyPropertyName, version.replace(sha1, "")); + properties.setProperty(ciFriendlyPropertyName, version.replace(sha1, "")); } - if (jDomProperties.containsKey(SHA1)) { + if (properties.containsKey(SHA1)) { // drop the value for the next version - jDomProperties.setProperty(SHA1, ""); + properties.setProperty(SHA1, ""); } } else { - jDomProperties.setProperty( + properties.setProperty( ciFriendlyPropertyName, version.replace(sha1, "").replace(SNAPSHOT, "")); - if (jDomProperties.containsKey(CHANGELIST)) { - jDomProperties.setProperty(CHANGELIST, ""); + if (properties.containsKey(CHANGELIST)) { + properties.setProperty(CHANGELIST, ""); } - if (jDomProperties.containsKey(SHA1) && !sha1.isEmpty()) { + if (properties.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); + properties.setProperty(SHA1, sha1); } } } } - private static String resolveSha1Property(JDomProperties jDomProperties, ReleaseDescriptor releaseDescriptor) { + private static String resolveSha1Property(ReleaseDescriptor releaseDescriptor) { String scmVersion = releaseDescriptor.getScmReleasedPomRevision(); return System.getProperty(SHA1, scmVersion == null ? "" : scmVersion); } From 491ee0d50549ee5b3a0558ffb13bc4e1710b845a Mon Sep 17 00:00:00 2001 From: mkolesnikov Date: Wed, 26 Jun 2024 15:36:29 +0300 Subject: [PATCH 15/15] MRELEASE-1109 More integration tests, map-version-phase should know about CI Friendly. --- .gitignore | 4 +- .../config/ReleaseDescriptorBuilder.java | 2 +- .../phase/AbstractMapVersionsPhase.java | 26 ++++++- .../phase/AbstractRewritePomsPhase.java | 5 +- .../release/transform/jdom2/JDomModel.java | 3 +- .../release/util/CiFriendlyVersion.java | 77 +++++++++---------- .../shared/release/util/MavenExpression.java | 19 ++++- .../release/phase/MapVersionsPhaseTest.java | 4 +- .../invoker.properties | 4 +- .../prepare/ci-friendly-multi-module/pom.xml | 7 +- .../ci-friendly-multi-module/verify.groovy | 4 +- 11 files changed, 97 insertions(+), 58 deletions(-) diff --git a/.gitignore b/.gitignore index 0e7c2b303..8937ec50e 100644 --- a/.gitignore +++ b/.gitignore @@ -10,4 +10,6 @@ target build .checkstyle .DS_Store -.factorypath \ No newline at end of file +.factorypath +.mvn/ +mvnw diff --git a/maven-release-manager/src/main/java/org/apache/maven/shared/release/config/ReleaseDescriptorBuilder.java b/maven-release-manager/src/main/java/org/apache/maven/shared/release/config/ReleaseDescriptorBuilder.java index a2afc729d..6a04f2b72 100644 --- a/maven-release-manager/src/main/java/org/apache/maven/shared/release/config/ReleaseDescriptorBuilder.java +++ b/maven-release-manager/src/main/java/org/apache/maven/shared/release/config/ReleaseDescriptorBuilder.java @@ -34,7 +34,7 @@ * @since 3.0.0-M5 */ public class ReleaseDescriptorBuilder { - private static final Pattern PROPERTY_PATTERN = Pattern.compile("\\$\\{[^}]+}"); + private static final Pattern PROPERTY_PATTERN = Pattern.compile("\\$\\{[^}]+?}"); private final Logger logger; diff --git a/maven-release-manager/src/main/java/org/apache/maven/shared/release/phase/AbstractMapVersionsPhase.java b/maven-release-manager/src/main/java/org/apache/maven/shared/release/phase/AbstractMapVersionsPhase.java index 57605bfd8..a7559b0e6 100644 --- a/maven-release-manager/src/main/java/org/apache/maven/shared/release/phase/AbstractMapVersionsPhase.java +++ b/maven-release-manager/src/main/java/org/apache/maven/shared/release/phase/AbstractMapVersionsPhase.java @@ -35,6 +35,7 @@ import org.apache.maven.shared.release.policy.version.VersionPolicy; import org.apache.maven.shared.release.policy.version.VersionPolicyRequest; import org.apache.maven.shared.release.scm.ScmRepositoryConfigurator; +import org.apache.maven.shared.release.util.CiFriendlyVersion; import org.apache.maven.shared.release.util.ReleaseUtil; import org.apache.maven.shared.release.versions.VersionParseException; import org.codehaus.plexus.components.interactivity.Prompter; @@ -238,12 +239,12 @@ private String resolveNextVersion( try { try { - suggestedVersion = - resolveSuggestedVersion(baseVersion, releaseDescriptor, releaseEnvironment); + suggestedVersion = resolveSuggestedVersion( + project, baseVersion, releaseDescriptor, releaseEnvironment); } catch (VersionParseException e) { if (releaseDescriptor.isInteractive()) { suggestedVersion = - resolveSuggestedVersion("1.0", releaseDescriptor, releaseEnvironment); + resolveSuggestedVersion(project, "1.0", releaseDescriptor, releaseEnvironment); } else { throw new ReleaseExecutionException( "Error parsing version, cannot determine next " + "version: " + e.getMessage(), @@ -292,7 +293,10 @@ private String getContextString(ReleaseDescriptor releaseDescriptor) { } private String resolveSuggestedVersion( - String baseVersion, ReleaseDescriptor releaseDescriptor, ReleaseEnvironment releaseEnvironment) + MavenProject project, + String baseVersion, + ReleaseDescriptor releaseDescriptor, + ReleaseEnvironment releaseEnvironment) throws PolicyException, VersionParseException { String policyId = releaseDescriptor.getProjectVersionPolicyId(); VersionPolicy policy = versionPolicies.get(policyId); @@ -307,6 +311,11 @@ private String resolveSuggestedVersion( } request.setWorkingDirectory(releaseDescriptor.getWorkingDirectory()); + if (CiFriendlyVersion.isCiFriendlyVersion(getOriginalVersion(project))) { + String sha1 = CiFriendlyVersion.resolveSha1Property(project.getProperties(), releaseDescriptor); + request.setVersion(baseVersion.replace(sha1, "")); + } + if (scmRepositoryConfigurator != null && releaseDescriptor.getScmSourceUrl() != null) { try { ScmRepository repository = scmRepositoryConfigurator.getConfiguredRepository( @@ -332,6 +341,15 @@ private String resolveSuggestedVersion( : policy.getReleaseVersion(request).getVersion(); } + private static String getOriginalVersion(MavenProject project) { + String version = null; + while (version == null && project != null) { + version = project.getOriginalModel().getVersion(); + project = project.getParent(); + } + return version; + } + private String getDevelopmentVersion(String projectId, ReleaseDescriptor releaseDescriptor) { String projectVersion = releaseDescriptor.getProjectDevelopmentVersion(projectId); diff --git a/maven-release-manager/src/main/java/org/apache/maven/shared/release/phase/AbstractRewritePomsPhase.java b/maven-release-manager/src/main/java/org/apache/maven/shared/release/phase/AbstractRewritePomsPhase.java index 97f3cbba6..9b4dbde22 100644 --- a/maven-release-manager/src/main/java/org/apache/maven/shared/release/phase/AbstractRewritePomsPhase.java +++ b/maven-release-manager/src/main/java/org/apache/maven/shared/release/phase/AbstractRewritePomsPhase.java @@ -61,6 +61,7 @@ import org.apache.maven.shared.release.transform.ModelETLRequest; import org.apache.maven.shared.release.transform.jdom2.JDomModelETLFactory; import org.apache.maven.shared.release.util.CiFriendlyVersion; +import org.apache.maven.shared.release.util.MavenExpression; import org.apache.maven.shared.release.util.ReleaseUtil; import org.codehaus.plexus.util.StringUtils; @@ -534,7 +535,7 @@ private void rewriteArtifactVersions( logInfo(result, " Updating " + artifactId + " to " + mappedVersion); coordinate.setVersion(mappedVersion); } else { - String property = CiFriendlyVersion.extractPropertyFromExpression(rawVersion); + String property = MavenExpression.extractPropertyFromExpression(rawVersion); if (property != null) { if (property.startsWith("project.") || property.startsWith("pom.") @@ -579,7 +580,7 @@ private void rewriteArtifactVersions( } } } else { - if (CiFriendlyVersion.containsCiFriendlyProperties(property)) { + if (CiFriendlyVersion.isCiFriendlyProperty(property)) { // the parent's pom revision is set inside // org.apache.maven.shared.release.transform.jdom2.JDomModel.setVersion logInfo( diff --git a/maven-release-manager/src/main/java/org/apache/maven/shared/release/transform/jdom2/JDomModel.java b/maven-release-manager/src/main/java/org/apache/maven/shared/release/transform/jdom2/JDomModel.java index 20ee84339..4b14e82d8 100644 --- a/maven-release-manager/src/main/java/org/apache/maven/shared/release/transform/jdom2/JDomModel.java +++ b/maven-release-manager/src/main/java/org/apache/maven/shared/release/transform/jdom2/JDomModel.java @@ -202,8 +202,7 @@ public void setVersion(String version) { } else { if (CiFriendlyVersion.isCiFriendlyVersion(versionElement.getTextNormalize())) { // try to rewrite property if CI friendly expression is used - CiFriendlyVersion.rewriteVersionAndProperties( - version, versionElement.getTextNormalize(), getProperties(), releaseDescriptor); + CiFriendlyVersion.rewriteVersionAndProperties(version, getProperties(), releaseDescriptor); } else { JDomUtils.rewriteValue(versionElement, version); } diff --git a/maven-release-manager/src/main/java/org/apache/maven/shared/release/util/CiFriendlyVersion.java b/maven-release-manager/src/main/java/org/apache/maven/shared/release/util/CiFriendlyVersion.java index 6b812a402..d5cc3662a 100644 --- a/maven-release-manager/src/main/java/org/apache/maven/shared/release/util/CiFriendlyVersion.java +++ b/maven-release-manager/src/main/java/org/apache/maven/shared/release/util/CiFriendlyVersion.java @@ -22,94 +22,91 @@ 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.commons.lang3.StringUtils; import org.apache.maven.artifact.ArtifactUtils; import org.apache.maven.shared.release.config.ReleaseDescriptor; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class CiFriendlyVersion { - - /** - * Regular expression pattern matching Maven expressions (i.e. references to Maven properties). - * The first group selects the property name the expression refers to. - */ - private static final Pattern EXPRESSION_PATTERN = Pattern.compile("\\$\\{(.+?)\\}"); + private static final Logger LOGGER = LoggerFactory.getLogger(CiFriendlyVersion.class); /** * All Maven properties allowed to be referenced in parent versions via expressions * @see CI-Friendly Versions */ - private static final String REVISION = "revision"; + public static final String REVISION = "revision"; + + public static final String SHA1 = "sha1"; + public static final String CHANGELIST = "changelist"; - private static final String SHA1 = "sha1"; - private static final String CHANGELIST = "changelist"; private static final Set CI_FRIENDLY_PROPERTIES = new HashSet<>(Arrays.asList(REVISION, SHA1, CHANGELIST)); private static final String SNAPSHOT = "-SNAPSHOT"; private CiFriendlyVersion() {} - /** - * Extracts the Maven property name from a given expression. - * @param expression the expression - * @return either {@code null} if value is no expression otherwise the property referenced in the expression - */ - public static String extractPropertyFromExpression(String expression) { - Matcher matcher = EXPRESSION_PATTERN.matcher(expression); - if (!matcher.find()) { - return null; - } - return matcher.group(1); - } - public static boolean isCiFriendlyVersion(String version) { - return containsCiFriendlyProperties(extractPropertyFromExpression(version)); + if (StringUtils.isEmpty(version)) { + return false; + } + return isCiFriendlyProperty(MavenExpression.extractPropertyFromExpression(version)); } - public static boolean containsCiFriendlyProperties(String property) { + public static boolean isCiFriendlyProperty(String property) { return CI_FRIENDLY_PROPERTIES.contains(property); } public static void rewriteVersionAndProperties( - String version, String versionElement, Properties properties, ReleaseDescriptor releaseDescriptor) { + String version, Properties properties, ReleaseDescriptor releaseDescriptor) { // try to rewrite property if CI friendly expression is used - String ciFriendlyPropertyName = extractPropertyFromExpression(versionElement); if (properties != null) { - String sha1 = resolveSha1Property(releaseDescriptor); + String sha1 = resolveSha1Property(properties, releaseDescriptor); // assume that everybody follows the example and properties are simply chained // and the changelist can only be '-SNAPSHOT' if (ArtifactUtils.isSnapshot(version)) { if (properties.containsKey(CHANGELIST)) { - properties.setProperty( - ciFriendlyPropertyName, version.replace(sha1, "").replace(SNAPSHOT, "")); - properties.setProperty(CHANGELIST, SNAPSHOT); + String revision = version.replace(sha1, "").replace(SNAPSHOT, ""); + setAndLogPropertyChange(properties, REVISION, revision); + setAndLogPropertyChange(properties, CHANGELIST, SNAPSHOT); } else { - properties.setProperty(ciFriendlyPropertyName, version.replace(sha1, "")); + String revision = version.replace(sha1, ""); + setAndLogPropertyChange(properties, REVISION, revision); } if (properties.containsKey(SHA1)) { // drop the value for the next version - properties.setProperty(SHA1, ""); + setAndLogPropertyChange(properties, SHA1, ""); } } else { - properties.setProperty( - ciFriendlyPropertyName, version.replace(sha1, "").replace(SNAPSHOT, "")); + properties.setProperty(REVISION, version.replace(sha1, "")); if (properties.containsKey(CHANGELIST)) { - properties.setProperty(CHANGELIST, ""); + setAndLogPropertyChange(properties, CHANGELIST, ""); } if (properties.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 - properties.setProperty(SHA1, sha1); + setAndLogPropertyChange(properties, SHA1, sha1); } } } } - private static String resolveSha1Property(ReleaseDescriptor releaseDescriptor) { + private static void setAndLogPropertyChange(Properties properties, String key, String value) { + LOGGER.info("Updating {} property to {}", key, value); + properties.setProperty(key, value); + } + + public static String resolveSha1Property(Properties properties, ReleaseDescriptor releaseDescriptor) { + String sha1 = properties.getProperty(SHA1); String scmVersion = releaseDescriptor.getScmReleasedPomRevision(); - return System.getProperty(SHA1, scmVersion == null ? "" : scmVersion); + String systemSha1 = System.getProperty(SHA1); + String result = StringUtils.isNotEmpty(systemSha1) + ? systemSha1 + : StringUtils.isNotEmpty(sha1) ? sha1 : scmVersion != null ? scmVersion : ""; + LOGGER.info("Resolved SHA1 property value {}", result); + return result; } } diff --git a/maven-release-manager/src/main/java/org/apache/maven/shared/release/util/MavenExpression.java b/maven-release-manager/src/main/java/org/apache/maven/shared/release/util/MavenExpression.java index 4976efb11..f832c0929 100644 --- a/maven-release-manager/src/main/java/org/apache/maven/shared/release/util/MavenExpression.java +++ b/maven-release-manager/src/main/java/org/apache/maven/shared/release/util/MavenExpression.java @@ -26,7 +26,11 @@ * @author Mikhail Kolesnikov */ public class MavenExpression { - public static final Pattern EXPRESSION_PATTERN = Pattern.compile("\\$\\{(.+?)\\}"); + /** + * Regular expression pattern matching Maven expressions (i.e. references to Maven properties). + * The first group selects the property name the expression refers to. + */ + private static final Pattern EXPRESSION_PATTERN = Pattern.compile("\\$\\{(.+?)}"); private MavenExpression() {} @@ -41,4 +45,17 @@ public static String evaluate(String expression, Map properties) { } return result.toString(); } + + /** + * Extracts the Maven property name from a given expression. + * @param expression the expression + * @return either {@code null} if value is no expression otherwise the property referenced in the expression + */ + public static String extractPropertyFromExpression(String expression) { + Matcher matcher = EXPRESSION_PATTERN.matcher(expression); + if (!matcher.find()) { + return null; + } + return matcher.group(1); + } } diff --git a/maven-release-manager/src/test/java/org/apache/maven/shared/release/phase/MapVersionsPhaseTest.java b/maven-release-manager/src/test/java/org/apache/maven/shared/release/phase/MapVersionsPhaseTest.java index 36baf5df2..ffbb84ae1 100644 --- a/maven-release-manager/src/test/java/org/apache/maven/shared/release/phase/MapVersionsPhaseTest.java +++ b/maven-release-manager/src/test/java/org/apache/maven/shared/release/phase/MapVersionsPhaseTest.java @@ -2452,6 +2452,8 @@ private static MavenProject createProject(String artifactId, String version) { model.setGroupId("groupId"); model.setArtifactId(artifactId); model.setVersion(version); - return new MavenProject(model); + MavenProject mavenProject = new MavenProject(model); + mavenProject.setOriginalModel(model); + return mavenProject; } } diff --git a/maven-release-plugin/src/it/projects/prepare/ci-friendly-multi-module/invoker.properties b/maven-release-plugin/src/it/projects/prepare/ci-friendly-multi-module/invoker.properties index c8e7f246d..50eda5608 100644 --- a/maven-release-plugin/src/it/projects/prepare/ci-friendly-multi-module/invoker.properties +++ b/maven-release-plugin/src/it/projects/prepare/ci-friendly-multi-module/invoker.properties @@ -15,5 +15,5 @@ # specific language governing permissions and limitations # under the License. -invoker.goals=release:clean release:prepare -invoker.mavenOpts=-Dsha1=-abcdef12 \ No newline at end of file +invoker.goals=release:clean release:prepare -Dsha1=-abcdef12 +#invoker.mavenOpts=-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=*:5005 \ No newline at end of file diff --git a/maven-release-plugin/src/it/projects/prepare/ci-friendly-multi-module/pom.xml b/maven-release-plugin/src/it/projects/prepare/ci-friendly-multi-module/pom.xml index 8d140e272..e6c05b916 100644 --- a/maven-release-plugin/src/it/projects/prepare/ci-friendly-multi-module/pom.xml +++ b/maven-release-plugin/src/it/projects/prepare/ci-friendly-multi-module/pom.xml @@ -32,8 +32,8 @@
- 1.0 - + 1.0.0 + -SNAPSHOT @maven.compiler.source@ @maven.compiler.target@ @@ -46,6 +46,9 @@ org.apache.maven.plugins maven-release-plugin @project.version@ + + -Dsha1=${sha1} + org.apache.maven.its.release diff --git a/maven-release-plugin/src/it/projects/prepare/ci-friendly-multi-module/verify.groovy b/maven-release-plugin/src/it/projects/prepare/ci-friendly-multi-module/verify.groovy index febfa0cee..2b83f5f9c 100644 --- a/maven-release-plugin/src/it/projects/prepare/ci-friendly-multi-module/verify.groovy +++ b/maven-release-plugin/src/it/projects/prepare/ci-friendly-multi-module/verify.groovy @@ -25,7 +25,7 @@ assert buildLog.exists() // tag versions 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.revision.text() == "1.0.0" assert projectRootTag.properties.sha1.text() == "-abcdef12" assert projectRootTag.properties.changelist.text() == "" @@ -39,7 +39,7 @@ assert projectBTag.parent.version.text() == '${revision}${sha1}${changelist}' // next development versions def projectRoot = new XmlSlurper().parse( new File( basedir, 'pom.xml.next' ) ) assert projectRoot.version.text() == '${revision}${sha1}${changelist}' -assert projectRoot.properties.revision.text() == "1.1" +assert projectRoot.properties.revision.text() == "1.0.1" assert projectRoot.properties.sha1.text() == "" assert projectRoot.properties.changelist.text() == "-SNAPSHOT"