Skip to content

Commit 2741848

Browse files
authored
[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+
1 parent 35e10b8 commit 2741848

File tree

6 files changed

+114
-3
lines changed

6 files changed

+114
-3
lines changed

apache-maven/src/assembly/maven/bin/mvn

+6-2
Original file line numberDiff line numberDiff line change
@@ -165,10 +165,14 @@ find_file_argument_basedir() {
165165
)
166166
}
167167

168-
# concatenates all lines of a file
168+
# concatenates all lines of a file and replaces variables
169169
concat_lines() {
170170
if [ -f "$1" ]; then
171-
echo "`tr -s '\r\n' ' ' < "$1"`"
171+
# First transform line endings to spaces
172+
content=$(tr -s '\r\n' ' ' < "$1")
173+
# Handle both ${var} and $var formats, only substitute MAVEN_PROJECTBASEDIR
174+
echo "$content" | sed -e "s|\${MAVEN_PROJECTBASEDIR}|$MAVEN_PROJECTBASEDIR|g" \
175+
-e "s|\$MAVEN_PROJECTBASEDIR|$MAVEN_PROJECTBASEDIR|g"
172176
fi
173177
}
174178

apache-maven/src/assembly/maven/bin/mvn.cmd

+7-1
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,13 @@ cd /d "%EXEC_DIR%"
176176
if not exist "%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config" goto endReadJvmConfig
177177

178178
@setlocal EnableExtensions EnableDelayedExpansion
179-
for /F "usebackq delims=" %%a in ("%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config") do set JVM_CONFIG_MAVEN_OPTS=!JVM_CONFIG_MAVEN_OPTS! %%a
179+
set JVM_CONFIG_MAVEN_OPTS=
180+
for /F "usebackq delims=" %%a in ("%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config") do (
181+
set "line=%%a"
182+
set "line=!line:$MAVEN_PROJECTBASEDIR=%MAVEN_PROJECTBASEDIR%!"
183+
set "line=!line:${MAVEN_PROJECTBASEDIR}=%MAVEN_PROJECTBASEDIR%!"
184+
set JVM_CONFIG_MAVEN_OPTS=!JVM_CONFIG_MAVEN_OPTS! !line!
185+
)
180186
@endlocal & set MAVEN_OPTS=%MAVEN_OPTS% %JVM_CONFIG_MAVEN_OPTS%
181187

182188
:endReadJvmConfig
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.maven.it;
20+
21+
import java.io.File;
22+
import java.util.Properties;
23+
24+
import org.junit.jupiter.api.Test;
25+
26+
import static org.junit.jupiter.api.Assertions.assertEquals;
27+
28+
/**
29+
* This is a test set for <a href="https://issues.apache.org/jira/browse/MNG-8598">MNG-8598</a>:
30+
* Verify that ${MAVEN_PROJECTBASEDIR} and $MAVEN_PROJECTBASEDIR in .mvn/jvm.config are properly
31+
* substituted with the actual project base directory.
32+
*/
33+
public class MavenITmng8598JvmConfigSubstitutionTest extends AbstractMavenIntegrationTestCase {
34+
public MavenITmng8598JvmConfigSubstitutionTest() {
35+
super("[4.0.0-rc-4,)");
36+
}
37+
38+
@Test
39+
public void testProjectBasedirSubstitution() throws Exception {
40+
File testDir = extractResources("/mng-8598");
41+
42+
Verifier verifier = newVerifier(testDir.getAbsolutePath());
43+
verifier.addCliArgument(
44+
"-Dexpression.outputFile=" + new File(testDir, "target/pom.properties").getAbsolutePath());
45+
verifier.setForkJvm(true); // custom .mvn/jvm.config
46+
verifier.addCliArgument("validate");
47+
verifier.execute();
48+
verifier.verifyErrorFreeLog();
49+
50+
Properties props = verifier.loadProperties("target/pom.properties");
51+
String expectedPath = testDir.getAbsolutePath().replace('\\', '/');
52+
assertEquals(
53+
expectedPath + "/curated",
54+
props.getProperty("project.properties.curatedPathProp").replace('\\', '/'));
55+
assertEquals(
56+
expectedPath + "/simple",
57+
props.getProperty("project.properties.simplePathProp").replace('\\', '/'));
58+
}
59+
}

its/core-it-suite/src/test/java/org/apache/maven/it/TestSuiteOrdering.java

+1
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ public TestSuiteOrdering() {
101101
* the tests are to finishing. Newer tests are also more likely to fail, so this is
102102
* a fail fast technique as well.
103103
*/
104+
suite.addTestSuite(MavenITmng8598JvmConfigSubstitutionTest.class);
104105
suite.addTestSuite(MavenITmng8653AfterAndEachPhasesWithConcurrentBuilderTest.class);
105106
suite.addTestSuite(MavenITmng5668AfterPhaseExecutionTest.class);
106107
suite.addTestSuite(MavenITmng8648ProjectStartedEventsTest.class);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
-DcuratedPath=${MAVEN_PROJECTBASEDIR}/curated
2+
-DsimplePath=$MAVEN_PROJECTBASEDIR/simple
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<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">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<groupId>org.apache.maven.its.mng8598</groupId>
6+
<artifactId>jvm-config-substitution</artifactId>
7+
<version>1.0</version>
8+
<packaging>pom</packaging>
9+
10+
<properties>
11+
<curatedPathProp>${curatedPath}</curatedPathProp>
12+
<simplePathProp>${simplePath}</simplePathProp>
13+
</properties>
14+
15+
<build>
16+
<plugins>
17+
<plugin>
18+
<groupId>org.apache.maven.its.plugins</groupId>
19+
<artifactId>maven-it-plugin-expression</artifactId>
20+
<version>2.1-SNAPSHOT</version>
21+
<executions>
22+
<execution>
23+
<goals>
24+
<goal>eval</goal>
25+
</goals>
26+
<phase>validate</phase>
27+
<configuration>
28+
<outputFile>${expression.outputFile}</outputFile>
29+
<expressions>
30+
<expression>project/properties/curatedPathProp</expression>
31+
<expression>project/properties/simplePathProp</expression>
32+
</expressions>
33+
</configuration>
34+
</execution>
35+
</executions>
36+
</plugin>
37+
</plugins>
38+
</build>
39+
</project>

0 commit comments

Comments
 (0)