|
1 | 1 | package io.quarkus.maven.components; |
2 | 2 |
|
3 | 3 | import java.io.IOException; |
| 4 | +import java.util.Properties; |
4 | 5 |
|
5 | 6 | import javax.inject.Inject; |
6 | 7 | import javax.inject.Named; |
|
9 | 10 | import org.apache.maven.AbstractMavenLifecycleParticipant; |
10 | 11 | import org.apache.maven.MavenExecutionException; |
11 | 12 | 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; |
12 | 16 |
|
13 | 17 | import io.quarkus.maven.BuildAnalyticsProvider; |
14 | 18 | import io.quarkus.maven.QuarkusBootstrapProvider; |
@@ -39,13 +43,73 @@ public void afterSessionEnd(MavenSession session) throws MavenExecutionException |
39 | 43 | } |
40 | 44 |
|
41 | 45 | @Override |
42 | | - public void afterProjectsRead(MavenSession session) |
43 | | - throws MavenExecutionException { |
| 46 | + public void afterProjectsRead(MavenSession session) { |
44 | 47 | // if this method is called then Maven plugin extensions are enabled |
45 | 48 | 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; |
46 | 92 | } |
47 | 93 |
|
48 | 94 | public boolean isEnabled() { |
49 | 95 | return enabled; |
50 | 96 | } |
| 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 | + } |
51 | 115 | } |
0 commit comments