Skip to content

Commit

Permalink
[MINVOKER-307] Support @{..} syntax in mavenOpts
Browse files Browse the repository at this point in the history
  • Loading branch information
slawekjaranowski committed May 16, 2024
1 parent 89b9633 commit 0e572a9
Show file tree
Hide file tree
Showing 5 changed files with 165 additions and 6 deletions.
9 changes: 9 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,11 @@ under the License.
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
Expand Down Expand Up @@ -377,6 +382,10 @@ under the License.
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.eclipse.sisu</groupId>
<artifactId>sisu-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,10 @@ public abstract class AbstractInvokerMojo extends AbstractMojo {
/**
* The <code>MAVEN_OPTS</code> environment variable to use when invoking Maven. This value can be overridden for
* individual integration tests by using {@link #invokerPropertiesFile}.
* <br>
* Since the version <b>3.7.0</b> using an alternate syntax for mavenOpts, <code>@{...}</code>
* allows late replacement of properties when the plugin is executed,
* so properties that have been modified by other plugins will be picked up correctly.
*
* @since 1.2
*/
Expand Down Expand Up @@ -739,6 +743,9 @@ public abstract class AbstractInvokerMojo extends AbstractMojo {
@Component
private ToolchainManagerPrivate toolchainManagerPrivate;

@Component
private InterpolatorUtils interpolatorUtils;

/**
* Invokes Maven on the configured test projects.
*
Expand Down Expand Up @@ -2376,7 +2383,7 @@ private InvokerProperties getInvokerProperties(final File projectDirectory, Prop
invokerProperties.setDefaultGoals(goals);
invokerProperties.setDefaultProfiles(profiles);
invokerProperties.setDefaultMavenExecutable(mavenExecutable);
invokerProperties.setDefaultMavenOpts(mavenOpts);
invokerProperties.setDefaultMavenOpts(interpolatorUtils.interpolateAtPattern(mavenOpts));
invokerProperties.setDefaultTimeoutInSeconds(timeoutInSeconds);
invokerProperties.setDefaultEnvironmentVariables(environmentVariables);
invokerProperties.setDefaultUpdateSnapshots(updateSnapshots);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* 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.plugins.invoker;

import javax.inject.Inject;
import javax.inject.Named;

import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.interpolation.InterpolationException;
import org.codehaus.plexus.interpolation.Interpolator;
import org.codehaus.plexus.interpolation.MapBasedValueSource;
import org.codehaus.plexus.interpolation.RegexBasedInterpolator;

/**
* Helper component for interpolating values.
*/
@Named
class InterpolatorUtils {

private final Interpolator atInterpolator;

/**
* A default constructor.
*
* @param mavenProject a MavenProject
*/
@Inject
InterpolatorUtils(MavenProject mavenProject) {
atInterpolator = new RegexBasedInterpolator("[@\\$]\\{(.+?)", "}");
atInterpolator.addValueSource(new MapBasedValueSource(mavenProject.getProperties()));
}

public String interpolateAtPattern(String value) throws MojoExecutionException {

if (value == null || !(value.contains("@{") || value.contains("${"))) {
return value;
}

try {
return atInterpolator.interpolate(value);
} catch (InterpolationException e) {
throw new MojoExecutionException(e.getMessage(), e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* 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.plugins.invoker;

import java.util.Properties;
import java.util.stream.Stream;

import org.apache.maven.project.MavenProject;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.when;

@ExtendWith(MockitoExtension.class)
class InterpolatorUtilsTest {

@Mock
private MavenProject mavenProject;

static Stream<Arguments> testAtInterpolate() {
return Stream.of(
Arguments.of(null, null),
Arguments.of("test", "test"),
Arguments.of("test@test", "test@test"),
Arguments.of("test$test", "test$test"),
Arguments.of("@{test}", "testInProps"),
Arguments.of("${test}", "testInProps"),
Arguments.of("test @{test} test", "test testInProps test"),
Arguments.of("test ${test} test", "test testInProps test"),
Arguments.of("@{test} @{test}", "testInProps testInProps"),
Arguments.of("${test} ${test}", "testInProps testInProps"),
Arguments.of("@{test} ${test}", "testInProps testInProps"));
}

@ParameterizedTest
@MethodSource
void testAtInterpolate(String input, String expected) throws Exception {
// given
Properties properties = new Properties();
properties.put("test", "testInProps");
when(mavenProject.getProperties()).thenReturn(properties);
InterpolatorUtils interpolatorUtils = new InterpolatorUtils(mavenProject);

// when
String output = interpolatorUtils.interpolateAtPattern(input);

// then
assertThat(output).isEqualTo(expected);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,13 @@ private MavenProject getMavenProject() {
@Test
void testSingleInvokerTest() throws Exception {
// given
MavenProject mavenProject = getMavenProject();
InvokerMojo invokerMojo = new InvokerMojo();
String dirPath = getBasedir() + "/src/test/resources/unit";
setVariableValueToObject(invokerMojo, "projectsDirectory", new File(dirPath));
setVariableValueToObject(invokerMojo, "invokerPropertiesFile", "invoker.properties");
setVariableValueToObject(invokerMojo, "project", getMavenProject());
setVariableValueToObject(invokerMojo, "project", mavenProject);
setVariableValueToObject(invokerMojo, "interpolatorUtils", new InterpolatorUtils(mavenProject));
setVariableValueToObject(invokerMojo, "invokerTest", "*dummy*");
setVariableValueToObject(invokerMojo, "settings", new Settings());

Expand All @@ -67,11 +69,13 @@ void testSingleInvokerTest() throws Exception {
@Test
void testMultiInvokerTest() throws Exception {
// given
MavenProject mavenProject = getMavenProject();
InvokerMojo invokerMojo = new InvokerMojo();
String dirPath = getBasedir() + "/src/test/resources/unit";
setVariableValueToObject(invokerMojo, "projectsDirectory", new File(dirPath));
setVariableValueToObject(invokerMojo, "invokerPropertiesFile", "invoker.properties");
setVariableValueToObject(invokerMojo, "project", getMavenProject());
setVariableValueToObject(invokerMojo, "project", mavenProject);
setVariableValueToObject(invokerMojo, "interpolatorUtils", new InterpolatorUtils(mavenProject));
setVariableValueToObject(invokerMojo, "invokerTest", "*dummy*,*terpolatio*");
setVariableValueToObject(invokerMojo, "settings", new Settings());

Expand All @@ -85,11 +89,13 @@ void testMultiInvokerTest() throws Exception {
@Test
void testFullPatternInvokerTest() throws Exception {
// given
MavenProject mavenProject = getMavenProject();
InvokerMojo invokerMojo = new InvokerMojo();
String dirPath = getBasedir() + "/src/test/resources/unit";
setVariableValueToObject(invokerMojo, "projectsDirectory", new File(dirPath));
setVariableValueToObject(invokerMojo, "invokerPropertiesFile", "invoker.properties");
setVariableValueToObject(invokerMojo, "project", getMavenProject());
setVariableValueToObject(invokerMojo, "project", mavenProject);
setVariableValueToObject(invokerMojo, "interpolatorUtils", new InterpolatorUtils(mavenProject));
setVariableValueToObject(invokerMojo, "invokerTest", "*");
setVariableValueToObject(invokerMojo, "settings", new Settings());

Expand All @@ -106,11 +112,13 @@ void testFullPatternInvokerTest() throws Exception {
@Test
void testSetupInProjectList() throws Exception {
// given
MavenProject mavenProject = getMavenProject();
InvokerMojo invokerMojo = new InvokerMojo();
String dirPath = getBasedir() + "/src/test/resources/unit";
setVariableValueToObject(invokerMojo, "projectsDirectory", new File(dirPath));
setVariableValueToObject(invokerMojo, "invokerPropertiesFile", "invoker.properties");
setVariableValueToObject(invokerMojo, "project", getMavenProject());
setVariableValueToObject(invokerMojo, "project", mavenProject);
setVariableValueToObject(invokerMojo, "interpolatorUtils", new InterpolatorUtils(mavenProject));
setVariableValueToObject(invokerMojo, "settings", new Settings());
setVariableValueToObject(invokerMojo, "setupIncludes", Collections.singletonList("dum*/pom.xml"));

Expand All @@ -134,11 +142,13 @@ void testSetupInProjectList() throws Exception {
@Test
void testSetupProjectIsFiltered() throws Exception {
// given
MavenProject mavenProject = getMavenProject();
InvokerMojo invokerMojo = new InvokerMojo();
String dirPath = getBasedir() + "/src/test/resources/unit";
setVariableValueToObject(invokerMojo, "projectsDirectory", new File(dirPath));
setVariableValueToObject(invokerMojo, "invokerPropertiesFile", "invoker.properties");
setVariableValueToObject(invokerMojo, "project", getMavenProject());
setVariableValueToObject(invokerMojo, "project", mavenProject);
setVariableValueToObject(invokerMojo, "interpolatorUtils", new InterpolatorUtils(mavenProject));
setVariableValueToObject(invokerMojo, "settings", new Settings());
setVariableValueToObject(invokerMojo, "setupIncludes", Collections.singletonList("dum*/pom.xml"));
setVariableValueToObject(invokerMojo, "invokerTest", "*project-dir*");
Expand Down

0 comments on commit 0e572a9

Please sign in to comment.