Skip to content

Commit

Permalink
Maven: add "copySurefireVariables" config to the "dev" and "test" goals
Browse files Browse the repository at this point in the history
Disabled by default. If enabled, the `<environmentVariables>` and
`<systemPropertyVariables>` from the Maven Surefire plugin configuration
are copied to the Quarkus Maven plugin, to prevent duplication.
  • Loading branch information
Ladicek committed Jan 30, 2025
1 parent 4933bd8 commit 5f03a92
Showing 1 changed file with 53 additions and 23 deletions.
76 changes: 53 additions & 23 deletions devtools/maven/src/main/java/io/quarkus/maven/DevMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ public class DevMojo extends AbstractMojo {
private static final String ORG_APACHE_MAVEN_PLUGINS = "org.apache.maven.plugins";
private static final String MAVEN_COMPILER_PLUGIN = "maven-compiler-plugin";
private static final String MAVEN_RESOURCES_PLUGIN = "maven-resources-plugin";
private static final String MAVEN_SUREFIRE_PLUGIN = "maven-surefire-plugin";
private static final String MAVEN_TOOLCHAINS_PLUGIN = "maven-toolchains-plugin";

private static final String ORG_JETBRAINS_KOTLIN = "org.jetbrains.kotlin";
Expand Down Expand Up @@ -263,6 +264,22 @@ public class DevMojo extends AbstractMojo {
@Parameter
private Map<String, String> systemProperties = Map.of();

/**
* When enabled, the {@code <environmentVariables>} and {@code <systemPropertyVariables>}
* elements of the Maven Surefire plugin are copied to environment variables and system
* properties defined by this plugin. Note that no other Surefire configuration is used
* (notably {@code <systemProperties>}), only the 2 elements mentioned above.
* <p>
* This plugin's {@code <environmentVariables>} and {@code <systemProperties>} have
* priority, so duplicate keys are not copied.
* <p>
* Since environment variables and system properties are global to the entire process,
* this also affects dev mode (when executed as {@code quarkus:dev}). Because of that,
* this copying action is disabled by default and requires opt-in.
*/
@Parameter(defaultValue = "false")
private boolean copySurefireVariables;

@Parameter(defaultValue = "${session}")
private MavenSession session;

Expand Down Expand Up @@ -485,6 +502,8 @@ public void close() throws IOException {
}
});

copySurefireVariables();

try {
DevModeRunner runner = new DevModeRunner(bootstrapId);
Map<Path, Long> pomFiles = readPomFileTimestamps(runner);
Expand Down Expand Up @@ -1495,6 +1514,35 @@ private void setJvmArgs(DevModeCommandLineBuilder builder) throws Exception {

}

private void copySurefireVariables() {
if (!copySurefireVariables) {
return;
}

Plugin surefireMavenPlugin = getConfiguredPluginOrNull(ORG_APACHE_MAVEN_PLUGINS, MAVEN_SUREFIRE_PLUGIN);
if (surefireMavenPlugin == null) {
return;
}

Xpp3Dom config = (Xpp3Dom) surefireMavenPlugin.getConfiguration();
if (config != null) {
// we copy the maps because they can be unmodifiable
environmentVariables = new HashMap<>(environmentVariables);
copyConfiguration(config.getChild("environmentVariables"), environmentVariables);
systemProperties = new HashMap<>(systemProperties);
copyConfiguration(config.getChild("systemPropertyVariables"), systemProperties);
}
}

private void copyConfiguration(Xpp3Dom config, Map<String, String> targetMap) {
if (config == null) {
return;
}
for (Xpp3Dom child : config.getChildren()) {
targetMap.putIfAbsent(child.getName(), child.getValue());
}
}

private void applyCompilerFlag(Optional<Xpp3Dom> compilerPluginConfiguration, String flagName,
Consumer<String> builderCall) {
compilerPluginConfiguration
Expand Down Expand Up @@ -1599,14 +1647,7 @@ private List<org.eclipse.aether.graph.Dependency> getProjectAetherDependencyMana
}

private void setKotlinSpecificFlags(DevModeCommandLineBuilder builder) {
Plugin kotlinMavenPlugin = null;
for (Plugin plugin : project.getBuildPlugins()) {
if (plugin.getArtifactId().equals(KOTLIN_MAVEN_PLUGIN) && plugin.getGroupId().equals(ORG_JETBRAINS_KOTLIN)) {
kotlinMavenPlugin = plugin;
break;
}
}

Plugin kotlinMavenPlugin = getConfiguredPluginOrNull(ORG_JETBRAINS_KOTLIN, KOTLIN_MAVEN_PLUGIN);
if (kotlinMavenPlugin == null) {
return;
}
Expand Down Expand Up @@ -1645,14 +1686,7 @@ private void setKotlinSpecificFlags(DevModeCommandLineBuilder builder) {
}

private void setAnnotationProcessorFlags(DevModeCommandLineBuilder builder) {
Plugin compilerMavenPlugin = null;
for (Plugin plugin : project.getBuildPlugins()) {
if (plugin.getArtifactId().equals("maven-compiler-plugin")
&& plugin.getGroupId().equals("org.apache.maven.plugins")) {
compilerMavenPlugin = plugin;
break;
}
}
Plugin compilerMavenPlugin = getConfiguredPluginOrNull(ORG_APACHE_MAVEN_PLUGINS, MAVEN_COMPILER_PLUGIN);
if (compilerMavenPlugin == null) {
return;
}
Expand Down Expand Up @@ -1683,13 +1717,9 @@ protected void modifyDevModeContext(DevModeCommandLineBuilder builder) {
}

private Optional<Xpp3Dom> findCompilerPluginConfiguration() {
for (final Plugin plugin : project.getBuildPlugins()) {
if (plugin.getArtifactId().equals(MAVEN_COMPILER_PLUGIN) && plugin.getGroupId().equals(ORG_APACHE_MAVEN_PLUGINS)) {
final Xpp3Dom compilerPluginConfiguration = (Xpp3Dom) plugin.getConfiguration();
if (compilerPluginConfiguration != null) {
return Optional.of(compilerPluginConfiguration);
}
}
Plugin plugin = getConfiguredPluginOrNull(ORG_APACHE_MAVEN_PLUGINS, MAVEN_COMPILER_PLUGIN);
if (plugin != null) {
return Optional.ofNullable((Xpp3Dom) plugin.getConfiguration());
}
return Optional.empty();
}
Expand Down

0 comments on commit 5f03a92

Please sign in to comment.