Skip to content

Commit 16376a8

Browse files
committed
Use existing Maven BootstrapSessionListener to hint Surefire about required JVM parameters
1 parent 6b06919 commit 16376a8

File tree

1 file changed

+66
-2
lines changed

1 file changed

+66
-2
lines changed

devtools/maven/src/main/java/io/quarkus/maven/components/BootstrapSessionListener.java

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.quarkus.maven.components;
22

33
import java.io.IOException;
4+
import java.util.Properties;
45

56
import javax.inject.Inject;
67
import javax.inject.Named;
@@ -9,6 +10,9 @@
910
import org.apache.maven.AbstractMavenLifecycleParticipant;
1011
import org.apache.maven.MavenExecutionException;
1112
import org.apache.maven.execution.MavenSession;
13+
import org.apache.maven.model.Plugin;
14+
import org.apache.maven.project.MavenProject;
15+
import org.codehaus.plexus.logging.Logger;
1216

1317
import io.quarkus.maven.BuildAnalyticsProvider;
1418
import io.quarkus.maven.QuarkusBootstrapProvider;
@@ -39,13 +43,73 @@ public void afterSessionEnd(MavenSession session) throws MavenExecutionException
3943
}
4044

4145
@Override
42-
public void afterProjectsRead(MavenSession session)
43-
throws MavenExecutionException {
46+
public void afterProjectsRead(MavenSession session) {
4447
// if this method is called then Maven plugin extensions are enabled
4548
enabled = true;
49+
50+
// Now let's automatically apply the required JVM flags to Surefire;
51+
// bear in mind that this is just a convenience: we can't rely on Maven
52+
// extensions to be enabled - but when they are, we can make it more useful.
53+
injectSurefireTuning(session);
54+
}
55+
56+
private void injectSurefireTuning(MavenSession session) {
57+
logDebug(session, "Quarkus Maven extension: injecting JVM args for Surefire");
58+
for (MavenProject project : session.getProjects()) {
59+
if (isQuarkusPluginConfigured(project)) {
60+
logDebug(session,
61+
"Found quarkus-maven-plugin in " + project.getArtifactId() + ": injecting Surefire arguments");
62+
injectSurefireArgs(session, project);
63+
}
64+
}
65+
}
66+
67+
private void injectSurefireArgs(MavenSession session, MavenProject project) {
68+
Properties props = project.getProperties();
69+
String existingArgLine = props.getProperty("argLine", "");
70+
71+
String jvmArgsAdd = "--add-opens=java.base/java.lang.invoke=ALL-UNNAMED"
72+
+ " --add-opens=java.base/java.lang=ALL-UNNAMED"
73+
+ " --add-exports=java.base/jdk.internal.module=ALL-UNNAMED";
74+
String newArgLine = (existingArgLine.trim().isEmpty())
75+
? jvmArgsAdd
76+
: jvmArgsAdd + existingArgLine;
77+
78+
props.setProperty("argLine", newArgLine);
79+
logDebug(session, "Quarkus Maven extension: injected 'argLine' for Surefire: " + newArgLine);
80+
}
81+
82+
private boolean isQuarkusPluginConfigured(MavenProject project) {
83+
if (project.getBuild() == null || project.getBuild().getPlugins() == null) {
84+
return false;
85+
}
86+
for (Plugin plugin : project.getBuild().getPlugins()) {
87+
if ("quarkus-maven-plugin".equals(plugin.getArtifactId())) {
88+
return true; // it's enabled on this project
89+
}
90+
}
91+
return false;
4692
}
4793

4894
public boolean isEnabled() {
4995
return enabled;
5096
}
97+
98+
//Injecting the Logger is apparently error-prone, it might fail in various conditions.
99+
//It's tempting to rather not log at all, but let's have some output about this situation
100+
//by falling back to a regular error stream rather than being fully unaware.
101+
private void logDebug(MavenSession session, String message) {
102+
Logger logger = null;
103+
try {
104+
logger = (Logger) session.getContainer().lookup(Logger.class.getName());
105+
} catch (Exception e) {
106+
// Let's ignore the message in this case: while we don't want to fail just because
107+
// we wanted to print some diagnostics message doesn't imply that the message was important.
108+
// Not logging the message as it might confuse about its severity.
109+
System.err.println("[Quarkus Maven extension: failed Logger Lookup, debug logging will not be available]");
110+
}
111+
if (logger != null) {
112+
logger.debug(message);
113+
}
114+
}
51115
}

0 commit comments

Comments
 (0)