From 188cd438dcec1c0155084e95041b9e7b06e9d331 Mon Sep 17 00:00:00 2001 From: Guillaume Nodet Date: Thu, 27 Mar 2025 14:29:01 +0100 Subject: [PATCH 1/3] [MNG-8598] Add support for MAVEN_PROJECTBASEDIR substitution in jvm.config Added support for substituting and in .mvn/jvm.config with the actual project base directory. Changes: - Modified mvn and mvn.cmd scripts to handle the substitution - Added integration test to verify the functionality The test verifies: - Both curly brace and simple syntax variants work - Substitution happens correctly in forked JVM - Feature is available in Maven 4.0+ --- apache-maven/src/assembly/maven/bin/mvn | 10 +++- apache-maven/src/assembly/maven/bin/mvn.cmd | 8 ++- ...venITmng8598JvmConfigSubstitutionTest.java | 59 +++++++++++++++++++ .../apache/maven/it/TestSuiteOrdering.java | 1 + .../test/resources/mng-8598/.mvn/jvm.config | 2 + .../src/test/resources/mng-8598/pom.xml | 39 ++++++++++++ 6 files changed, 116 insertions(+), 3 deletions(-) create mode 100644 its/core-it-suite/src/test/java/org/apache/maven/it/MavenITmng8598JvmConfigSubstitutionTest.java create mode 100644 its/core-it-suite/src/test/resources/mng-8598/.mvn/jvm.config create mode 100644 its/core-it-suite/src/test/resources/mng-8598/pom.xml diff --git a/apache-maven/src/assembly/maven/bin/mvn b/apache-maven/src/assembly/maven/bin/mvn index 6fd203348f43..e8470ee353d1 100755 --- a/apache-maven/src/assembly/maven/bin/mvn +++ b/apache-maven/src/assembly/maven/bin/mvn @@ -165,10 +165,16 @@ find_file_argument_basedir() { ) } -# concatenates all lines of a file +# concatenates all lines of a file and replaces variables concat_lines() { if [ -f "$1" ]; then - echo "`tr -s '\r\n' ' ' < "$1"`" + # First replace variables in the content + while IFS= read -r line || [ -n "$line" ]; do + # Replace both ${MAVEN_PROJECTBASEDIR} and $MAVEN_PROJECTBASEDIR with actual value + line=${line//\$\{MAVEN_PROJECTBASEDIR\}/$MAVEN_PROJECTBASEDIR} + line=${line//\$MAVEN_PROJECTBASEDIR/$MAVEN_PROJECTBASEDIR} + echo "$line" + done < "$1" | tr -s '\r\n' ' ' fi } diff --git a/apache-maven/src/assembly/maven/bin/mvn.cmd b/apache-maven/src/assembly/maven/bin/mvn.cmd index d64073c400f8..f6b802e505e7 100644 --- a/apache-maven/src/assembly/maven/bin/mvn.cmd +++ b/apache-maven/src/assembly/maven/bin/mvn.cmd @@ -176,7 +176,13 @@ cd /d "%EXEC_DIR%" if not exist "%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config" goto endReadJvmConfig @setlocal EnableExtensions EnableDelayedExpansion -for /F "usebackq delims=" %%a in ("%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config") do set JVM_CONFIG_MAVEN_OPTS=!JVM_CONFIG_MAVEN_OPTS! %%a +set JVM_CONFIG_MAVEN_OPTS= +for /F "usebackq delims=" %%a in ("%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config") do ( + set "line=%%a" + set "line=!line:$MAVEN_PROJECTBASEDIR=%MAVEN_PROJECTBASEDIR%!" + set "line=!line:${MAVEN_PROJECTBASEDIR}=%MAVEN_PROJECTBASEDIR%!" + set JVM_CONFIG_MAVEN_OPTS=!JVM_CONFIG_MAVEN_OPTS! !line! +) @endlocal & set MAVEN_OPTS=%MAVEN_OPTS% %JVM_CONFIG_MAVEN_OPTS% :endReadJvmConfig diff --git a/its/core-it-suite/src/test/java/org/apache/maven/it/MavenITmng8598JvmConfigSubstitutionTest.java b/its/core-it-suite/src/test/java/org/apache/maven/it/MavenITmng8598JvmConfigSubstitutionTest.java new file mode 100644 index 000000000000..9c2399fd4841 --- /dev/null +++ b/its/core-it-suite/src/test/java/org/apache/maven/it/MavenITmng8598JvmConfigSubstitutionTest.java @@ -0,0 +1,59 @@ +/* + * 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.it; + +import java.io.File; +import java.util.Properties; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +/** + * This is a test set for MNG-8598: + * Verify that ${MAVEN_PROJECTBASEDIR} and $MAVEN_PROJECTBASEDIR in .mvn/jvm.config are properly + * substituted with the actual project base directory. + */ +public class MavenITmng8598JvmConfigSubstitutionTest extends AbstractMavenIntegrationTestCase { + public MavenITmng8598JvmConfigSubstitutionTest() { + super("[4.0.0-rc-4,)"); + } + + @Test + public void testProjectBasedirSubstitution() throws Exception { + File testDir = extractResources("/mng-8598"); + + Verifier verifier = newVerifier(testDir.getAbsolutePath()); + verifier.addCliArgument( + "-Dexpression.outputFile=" + new File(testDir, "target/pom.properties").getAbsolutePath()); + verifier.setForkJvm(true); // custom .mvn/jvm.config + verifier.addCliArgument("validate"); + verifier.execute(); + verifier.verifyErrorFreeLog(); + + Properties props = verifier.loadProperties("target/pom.properties"); + String expectedPath = testDir.getAbsolutePath().replace('\\', '/'); + assertEquals( + expectedPath + "/curated", + props.getProperty("project.properties.curatedPathProp").replace('\\', '/')); + assertEquals( + expectedPath + "/simple", + props.getProperty("project.properties.simplePathProp").replace('\\', '/')); + } +} diff --git a/its/core-it-suite/src/test/java/org/apache/maven/it/TestSuiteOrdering.java b/its/core-it-suite/src/test/java/org/apache/maven/it/TestSuiteOrdering.java index 364f4cd86d89..fe83bae342dd 100644 --- a/its/core-it-suite/src/test/java/org/apache/maven/it/TestSuiteOrdering.java +++ b/its/core-it-suite/src/test/java/org/apache/maven/it/TestSuiteOrdering.java @@ -101,6 +101,7 @@ public TestSuiteOrdering() { * the tests are to finishing. Newer tests are also more likely to fail, so this is * a fail fast technique as well. */ + suite.addTestSuite(MavenITmng8598JvmConfigSubstitutionTest.class); suite.addTestSuite(MavenITmng5668AfterPhaseExecutionTest.class); suite.addTestSuite(MavenITmng8648ProjectStartedEventsTest.class); suite.addTestSuite(MavenITmng8645ConsumerPomDependencyManagementTest.class); diff --git a/its/core-it-suite/src/test/resources/mng-8598/.mvn/jvm.config b/its/core-it-suite/src/test/resources/mng-8598/.mvn/jvm.config new file mode 100644 index 000000000000..2109a0e0d1f0 --- /dev/null +++ b/its/core-it-suite/src/test/resources/mng-8598/.mvn/jvm.config @@ -0,0 +1,2 @@ +-DcuratedPath=${MAVEN_PROJECTBASEDIR}/curated +-DsimplePath=$MAVEN_PROJECTBASEDIR/simple \ No newline at end of file diff --git a/its/core-it-suite/src/test/resources/mng-8598/pom.xml b/its/core-it-suite/src/test/resources/mng-8598/pom.xml new file mode 100644 index 000000000000..a7c12be7453a --- /dev/null +++ b/its/core-it-suite/src/test/resources/mng-8598/pom.xml @@ -0,0 +1,39 @@ + + + 4.0.0 + + org.apache.maven.its.mng8598 + jvm-config-substitution + 1.0 + pom + + + ${curatedPath} + ${simplePath} + + + + + + org.apache.maven.its.plugins + maven-it-plugin-expression + 2.1-SNAPSHOT + + + + eval + + validate + + ${expression.outputFile} + + project/properties/curatedPathProp + project/properties/simplePathProp + + + + + + + + From 71f2232b3d6bb148b9cc905e15122f099e2d0e99 Mon Sep 17 00:00:00 2001 From: Guillaume Nodet Date: Fri, 28 Mar 2025 15:07:25 +0100 Subject: [PATCH 2/3] Perform cr/lf handling first --- apache-maven/src/assembly/maven/bin/mvn | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/apache-maven/src/assembly/maven/bin/mvn b/apache-maven/src/assembly/maven/bin/mvn index e8470ee353d1..3440952bc854 100755 --- a/apache-maven/src/assembly/maven/bin/mvn +++ b/apache-maven/src/assembly/maven/bin/mvn @@ -168,13 +168,12 @@ find_file_argument_basedir() { # concatenates all lines of a file and replaces variables concat_lines() { if [ -f "$1" ]; then - # First replace variables in the content - while IFS= read -r line || [ -n "$line" ]; do - # Replace both ${MAVEN_PROJECTBASEDIR} and $MAVEN_PROJECTBASEDIR with actual value - line=${line//\$\{MAVEN_PROJECTBASEDIR\}/$MAVEN_PROJECTBASEDIR} - line=${line//\$MAVEN_PROJECTBASEDIR/$MAVEN_PROJECTBASEDIR} - echo "$line" - done < "$1" | tr -s '\r\n' ' ' + # First transform line endings to spaces + content=`tr -s '\r\n' ' ' < "$1"` + # Then do variable substitution on the single line + content=${content//\$\{MAVEN_PROJECTBASEDIR\}/$MAVEN_PROJECTBASEDIR} + content=${content//\$MAVEN_PROJECTBASEDIR/$MAVEN_PROJECTBASEDIR} + echo "$content" fi } From 67ebc4eaba74f80a3fd1fc8d99af12b8943e95bb Mon Sep 17 00:00:00 2001 From: Guillaume Nodet Date: Fri, 28 Mar 2025 16:12:57 +0100 Subject: [PATCH 3/3] Use sed, as shell variable substitution is not posix compliant --- apache-maven/src/assembly/maven/bin/mvn | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/apache-maven/src/assembly/maven/bin/mvn b/apache-maven/src/assembly/maven/bin/mvn index 3440952bc854..139492a947e0 100755 --- a/apache-maven/src/assembly/maven/bin/mvn +++ b/apache-maven/src/assembly/maven/bin/mvn @@ -169,11 +169,10 @@ find_file_argument_basedir() { concat_lines() { if [ -f "$1" ]; then # First transform line endings to spaces - content=`tr -s '\r\n' ' ' < "$1"` - # Then do variable substitution on the single line - content=${content//\$\{MAVEN_PROJECTBASEDIR\}/$MAVEN_PROJECTBASEDIR} - content=${content//\$MAVEN_PROJECTBASEDIR/$MAVEN_PROJECTBASEDIR} - echo "$content" + content=$(tr -s '\r\n' ' ' < "$1") + # Handle both ${var} and $var formats, only substitute MAVEN_PROJECTBASEDIR + echo "$content" | sed -e "s|\${MAVEN_PROJECTBASEDIR}|$MAVEN_PROJECTBASEDIR|g" \ + -e "s|\$MAVEN_PROJECTBASEDIR|$MAVEN_PROJECTBASEDIR|g" fi }