This is an automated email from the ASF dual-hosted git repository.
gnodet pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven.git
The following commit(s) were added to refs/heads/master by this push:
new 274184803c [MNG-8598] Add support for MAVEN_PROJECTBASEDIR
substitution in jvm.config (#2194)
274184803c is described below
commit 274184803c3726cdc3501237c379901740566bbf
Author: Guillaume Nodet <[email protected]>
AuthorDate: Fri Mar 28 17:15:17 2025 +0100
[MNG-8598] Add support for MAVEN_PROJECTBASEDIR substitution in jvm.config
(#2194)
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 | 8 ++-
apache-maven/src/assembly/maven/bin/mvn.cmd | 8 ++-
.../MavenITmng8598JvmConfigSubstitutionTest.java | 59 ++++++++++++++++++++++
.../org/apache/maven/it/TestSuiteOrdering.java | 1 +
.../src/test/resources/mng-8598/.mvn/jvm.config | 2 +
.../src/test/resources/mng-8598/pom.xml | 39 ++++++++++++++
6 files changed, 114 insertions(+), 3 deletions(-)
diff --git a/apache-maven/src/assembly/maven/bin/mvn
b/apache-maven/src/assembly/maven/bin/mvn
index 6fd203348f..139492a947 100755
--- a/apache-maven/src/assembly/maven/bin/mvn
+++ b/apache-maven/src/assembly/maven/bin/mvn
@@ -165,10 +165,14 @@ 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 transform line endings to spaces
+ 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
}
diff --git a/apache-maven/src/assembly/maven/bin/mvn.cmd
b/apache-maven/src/assembly/maven/bin/mvn.cmd
index d64073c400..f6b802e505 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 0000000000..9c2399fd48
--- /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 <a
href="https://issues.apache.org/jira/browse/MNG-8598">MNG-8598</a>:
+ * 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 8404b80183..1d948a9468 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(MavenITmng8653AfterAndEachPhasesWithConcurrentBuilderTest.class);
suite.addTestSuite(MavenITmng5668AfterPhaseExecutionTest.class);
suite.addTestSuite(MavenITmng8648ProjectStartedEventsTest.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 0000000000..2109a0e0d1
--- /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 0000000000..a7c12be745
--- /dev/null
+++ b/its/core-it-suite/src/test/resources/mng-8598/pom.xml
@@ -0,0 +1,39 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+
+ <groupId>org.apache.maven.its.mng8598</groupId>
+ <artifactId>jvm-config-substitution</artifactId>
+ <version>1.0</version>
+ <packaging>pom</packaging>
+
+ <properties>
+ <curatedPathProp>${curatedPath}</curatedPathProp>
+ <simplePathProp>${simplePath}</simplePathProp>
+ </properties>
+
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.maven.its.plugins</groupId>
+ <artifactId>maven-it-plugin-expression</artifactId>
+ <version>2.1-SNAPSHOT</version>
+ <executions>
+ <execution>
+ <goals>
+ <goal>eval</goal>
+ </goals>
+ <phase>validate</phase>
+ <configuration>
+ <outputFile>${expression.outputFile}</outputFile>
+ <expressions>
+ <expression>project/properties/curatedPathProp</expression>
+ <expression>project/properties/simplePathProp</expression>
+ </expressions>
+ </configuration>
+ </execution>
+ </executions>
+ </plugin>
+ </plugins>
+ </build>
+</project>