Class AbstractExecJavaBase
- java.lang.Object
-
- org.apache.maven.plugin.AbstractMojo
-
- org.codehaus.mojo.exec.AbstractExecMojo
-
- org.codehaus.mojo.exec.AbstractExecJavaBase
-
- All Implemented Interfaces:
org.apache.maven.plugin.ContextEnabled,org.apache.maven.plugin.Mojo
- Direct Known Subclasses:
ExecJavaMojo
public abstract class AbstractExecJavaBase extends AbstractExecMojo
Abstract base class for ExecJavaMojo implementations (Java 8 and Java 9+). Contains all common code shared between the Java 8 base and Java 9+ version.- Since:
- 3.6.2
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) classAbstractExecJavaBase.IsolatedThreadGroupa ThreadGroup to isolate execution and collect exceptions.
-
Field Summary
Fields Modifier and Type Field Description protected java.util.List<java.lang.String>additionalClasspathElementsAdditional elements to be appended to the classpath.protected java.lang.String[]argumentsThe class arguments.protected booleanblockSystemExitWhether to try and prohibit the called Java program from terminating the JVM (and with it the whole Maven build) by callingSystem.exit(int).protected java.util.List<java.lang.String>classpathFilenameExclusionsList of file to exclude from the classpath.protected booleancleanupDaemonThreadsWhether to interrupt/join and possibly stop the daemon threads upon quitting.protected org.codehaus.plexus.PlexusContainercontainerprotected longdaemonThreadJoinTimeoutThis defines the number of milliseconds to wait for daemon threads to quit following their interruption.
This is only taken into account ifcleanupDaemonThreadsistrue.protected java.util.List<java.lang.String>excludedJvmPackagesAdditional packages to NOT load from the jvm even if it is in a flat classpath.protected java.util.List<java.lang.String>forcedJvmPackagesAdditional packages to load from the jvm even if a classpath dependency matches.protected booleanincludeProjectDependenciesIndicates if the project dependencies should be used when executing the main class.protected booleankeepAliveDeprecated.since 1.1-alpha-1protected java.lang.StringmainClassThe main class to execute.
With Java 9 and above you can prefix it with the modulename, e.g.protected java.util.PropertiesoriginalSystemPropertiesprotected intpreloadCommonPoolForces the creation of fork join common pool to avoids the threads to be owned by the isolated thread group and prevent a proper shutdown.protected booleanstopUnresponsiveDaemonThreadsWether to callThread.stop()following a timing out of waiting for an interrupted thread to finish.protected AbstractProperty[]systemPropertiesA list of system properties to be passed.protected static java.lang.StringTHREAD_STOP_UNAVAILABLE-
Fields inherited from class org.codehaus.mojo.exec.AbstractExecMojo
classpathScope, executableDependency, includePluginDependencies, project, repositorySystem
-
-
Constructor Summary
Constructors Modifier Constructor Description protectedAbstractExecJavaBase(org.eclipse.aether.RepositorySystem repositorySystem, org.codehaus.plexus.PlexusContainer container)
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected voidaddAdditionalClasspathElements(java.util.List<java.nio.file.Path> path)protected voidaddRelevantPluginDependenciesToClasspath(java.util.List<java.nio.file.Path> path)Add any relevant project dependencies to the classpath.protected voidaddRelevantProjectDependenciesToClasspath(java.util.List<java.nio.file.Path> path)Add any relevant project dependencies to the classpath.protected voiddoExecClassLoader(java.lang.String bootClassName)protected abstract voiddoExecModulePath(java.lang.String moduleName, java.lang.String bootClassName)Execute using module path (Java 9+ JPMS).protected voiddoRun(java.lang.Class<?> bootClass)voidexecute()Execute goal.protected voidexecuteMainMethod(java.lang.Class<?> bootClass)Execute the main method.protected java.lang.reflect.MethodfindMethod(java.lang.Class<?> clazz, java.lang.String methodName, boolean isStatic, java.lang.Class<?>[] parameterTypes)Finds a method with the given name, parameter types, and static modifier requirement.protected java.util.Collection<java.lang.Thread>getActiveThreads(java.lang.ThreadGroup threadGroup)protected java.net.URLClassLoadergetClassLoader()Set up a classloader for the execution of the main class.protected voidjoinNonDaemonThreads(java.lang.ThreadGroup threadGroup)protected voidjoinThread(java.lang.Thread thread, long timeoutMsecs)protected java.lang.Objectlookup(java.lang.reflect.Parameter param, java.lang.String name)protected java.lang.ObjectlookupParam(java.lang.reflect.Parameter param)protected java.lang.ObjectnewInstance(java.lang.Class<?> bootClass)protected voidpreloadCommonPool()To avoid the exec:java to consider common pool threads leaked, let's pre-create them.protected java.util.function.BiConsumer<java.lang.String,java.lang.String>propertiesUpdater(java.util.Properties props)protected java.util.function.Function<java.lang.String,java.lang.String>resolveVersion(java.util.function.Function<org.eclipse.aether.resolution.VersionRangeResult,java.lang.Object> fn)protected voidsetSystemProperties()Pass any given system properties to the java system properties.protected voidterminateThreads(java.lang.ThreadGroup threadGroup)protected voidwaitFor(long millis)Stop program execution for nn millis.-
Methods inherited from class org.codehaus.mojo.exec.AbstractExecMojo
collectProjectArtifactsAndClasspath, determineRelevantPluginDependencies, findExecutableArtifact, getPluginDependencies, getSession, hasCommandlineArgs, isSkip, parseCommandlineArgs, registerSourceRoots
-
-
-
-
Field Detail
-
THREAD_STOP_UNAVAILABLE
protected static final java.lang.String THREAD_STOP_UNAVAILABLE
- See Also:
- Constant Field Values
-
mainClass
@Parameter(required=true, property="exec.mainClass") protected java.lang.String mainClassThe main class to execute.
With Java 9 and above you can prefix it with the modulename, e.g.com.greetings/com.greetings.MainWithout modulename the classpath will be used, with modulename a new modulelayer will be created.Note that you can also provide a
Runnablefully qualified name. The runnable can get constructor injections either by type if you have maven in your classpath (can be provided) or by name (ensure to enable-parametersJava compiler option) for loose coupling. Current support loose injections are:systemProperties:Properties, session system propertiessystemPropertiesUpdater:BiConsumer<String, String>, session system properties update callback (pass the key/value to update, null value means removal of the key)userProperties:Properties, session user propertiesuserPropertiesUpdater:BiConsumer<String, String>, session user properties update callback (pass the key/value to update, null value means removal of the key)projectProperties:Properties, project propertiesprojectPropertiesUpdater:BiConsumer<String, String>, project properties update callback (pass the key/value to update, null value means removal of the key)highestVersionResolver:Function<String, String>, passing agroupId:artifactIdyou get the latest resolved version from the project repositories
- Since:
- 1.0
-
preloadCommonPool
@Parameter(property="exec.preloadCommonPool", defaultValue="0") protected int preloadCommonPoolForces the creation of fork join common pool to avoids the threads to be owned by the isolated thread group and prevent a proper shutdown. If set to zero the default parallelism is used to precreate all threads, if negative it is ignored else the value is the one used to create the fork join threads.- Since:
- 3.0.1
-
arguments
@Parameter(property="exec.arguments") protected java.lang.String[] arguments
The class arguments.- Since:
- 1.0
-
systemProperties
@Parameter protected AbstractProperty[] systemProperties
A list of system properties to be passed. Note: as the execution is not forked, some system properties required by the JVM cannot be passed here. Use MAVEN_OPTS or the exec:exec instead. See the user guide for more information.- Since:
- 1.0
-
keepAlive
@Parameter(property="exec.keepAlive", defaultValue="false") @Deprecated protected boolean keepAliveDeprecated.since 1.1-alpha-1Indicates if mojo should be kept running after the mainclass terminates. Use full for server like apps with daemon threads.- Since:
- 1.0
-
includeProjectDependencies
@Parameter(property="exec.includeProjectDependencies", defaultValue="true") protected boolean includeProjectDependenciesIndicates if the project dependencies should be used when executing the main class.- Since:
- 1.1-beta-1
-
cleanupDaemonThreads
@Parameter(property="exec.cleanupDaemonThreads", defaultValue="true") protected boolean cleanupDaemonThreadsWhether to interrupt/join and possibly stop the daemon threads upon quitting.
If this isfalse, maven does nothing about the daemon threads. When maven has no more work to do, the VM will normally terminate any remaining daemon threads.In certain cases (in particular if maven is embedded), you might need to keep this enabled to make sure threads are properly cleaned up to ensure they don't interfere with subsequent activity. In that case, see
daemonThreadJoinTimeoutandstopUnresponsiveDaemonThreadsfor further tuning.- Since:
- 1.1-beta-1
-
daemonThreadJoinTimeout
@Parameter(property="exec.daemonThreadJoinTimeout", defaultValue="15000") protected long daemonThreadJoinTimeoutThis defines the number of milliseconds to wait for daemon threads to quit following their interruption.
This is only taken into account ifcleanupDaemonThreadsistrue. A value <=0 means to not timeout (i.e. wait indefinitely for threads to finish). Following a timeout, a warning will be logged.Note: properly coded threads should terminate upon interruption but some threads may prove problematic: as the VM does interrupt daemon threads, some code may not have been written to handle interruption properly. For example java.util.Timer is known to not handle interruptions in JDK <= 1.6. So it is not possible for us to infinitely wait by default otherwise maven could hang. A sensible default value has been chosen, but this default value may change in the future based on user feedback.
- Since:
- 1.1-beta-1
-
stopUnresponsiveDaemonThreads
@Parameter(property="exec.stopUnresponsiveDaemonThreads", defaultValue="false") protected boolean stopUnresponsiveDaemonThreadsWether to callThread.stop()following a timing out of waiting for an interrupted thread to finish. This is only taken into account ifcleanupDaemonThreadsistrueand thedaemonThreadJoinTimeoutthreshold has been reached for an uncooperative thread. If this isfalse, or ifThread.stop()fails to get the thread to stop, then a warning is logged and Maven will continue on while the affected threads (and related objects in memory) linger on. Consider setting this totrueif you are invoking problematic code that you can't fix. An example isTimerwhich doesn't respond to interruption. To haveTimerfixed, vote for this bug.Note: In JDK 20+, the long deprecated
Thread.stop()(since JDK 1.2) has been removed and will throw anUnsupportedOperationException. This will be handled gracefully, yielding a log warning "Thread.stop() is unavailable in this JRE version, cannot force-stop any threads" once and not trying to stop any further threads during the same execution.- Since:
- 1.1-beta-1
-
originalSystemProperties
protected java.util.Properties originalSystemProperties
-
additionalClasspathElements
@Parameter protected java.util.List<java.lang.String> additionalClasspathElements
Additional elements to be appended to the classpath.- Since:
- 1.3
-
classpathFilenameExclusions
@Parameter protected java.util.List<java.lang.String> classpathFilenameExclusions
List of file to exclude from the classpath. It matches the jar name, for exampleslf4j-simple-1.7.30.jar.- Since:
- 3.0.1
-
forcedJvmPackages
@Parameter protected java.util.List<java.lang.String> forcedJvmPackages
Additional packages to load from the jvm even if a classpath dependency matches.- Since:
- 3.5.0
-
excludedJvmPackages
@Parameter protected java.util.List<java.lang.String> excludedJvmPackages
Additional packages to NOT load from the jvm even if it is in a flat classpath. Can enable to reproduce a webapp behavior for example where library is loaded over the JVM.- Since:
- 3.5.0
-
blockSystemExit
@Parameter(property="exec.blockSystemExit", defaultValue="false") protected boolean blockSystemExitWhether to try and prohibit the called Java program from terminating the JVM (and with it the whole Maven build) by callingSystem.exit(int). When active, loaded classes will replace this call by a custom callback. In case of an exit code 0 (OK), it will simply log the fact thatSystem.exit(int)was called. Otherwise, it will throw aSystemExitException, failing the Maven goal as if the called Java code itself had exited with an exception. This way, the error is propagated without terminating the whole Maven JVM. In previous versions, users had to use theexecinstead of thejavagoal in such cases, which now with this option is no longer necessary.- Since:
- 3.2.0
-
container
protected final org.codehaus.plexus.PlexusContainer container
-
-
Method Detail
-
execute
public void execute() throws org.apache.maven.plugin.MojoExecutionException, org.apache.maven.plugin.MojoFailureExceptionExecute goal.- Throws:
org.apache.maven.plugin.MojoExecutionException- execution of the main class or one of the threads it generated failed.org.apache.maven.plugin.MojoFailureException- something bad happened...
-
doExecClassLoader
protected void doExecClassLoader(java.lang.String bootClassName) throws java.lang.Throwable- Throws:
java.lang.Throwable
-
doExecModulePath
protected abstract void doExecModulePath(java.lang.String moduleName, java.lang.String bootClassName) throws java.lang.ThrowableExecute using module path (Java 9+ JPMS). Subclasses must implement this method to provide version-specific module path execution.- Parameters:
moduleName- the module namebootClassName- the fully qualified class name- Throws:
java.lang.Throwable- if execution fails
-
executeMainMethod
protected void executeMainMethod(java.lang.Class<?> bootClass) throws java.lang.ThrowableExecute the main method. Subclasses may override to provide version-specific behavior (e.g., Module opening logic for Java 9+).- Parameters:
bootClass- the class containing the main method- Throws:
java.lang.Throwable- if execution fails
-
findMethod
protected java.lang.reflect.Method findMethod(java.lang.Class<?> clazz, java.lang.String methodName, boolean isStatic, java.lang.Class<?>[] parameterTypes)Finds a method with the given name, parameter types, and static modifier requirement. Searches the class hierarchy including inherited methods. Only matches methods with void return type (for JSR-512 compliance).- Parameters:
clazz- the class to searchmethodName- the method nameisStatic- whether the method should be staticparameterTypes- the parameter types- Returns:
- the matching method, or null if not found
-
newInstance
protected java.lang.Object newInstance(java.lang.Class<?> bootClass) throws java.lang.reflect.InvocationTargetException, java.lang.InstantiationException, java.lang.IllegalAccessException, java.lang.NoSuchMethodException- Throws:
java.lang.reflect.InvocationTargetExceptionjava.lang.InstantiationExceptionjava.lang.IllegalAccessExceptionjava.lang.NoSuchMethodException
-
doRun
protected void doRun(java.lang.Class<?> bootClass) throws java.lang.InstantiationException, java.lang.IllegalAccessException, java.lang.reflect.InvocationTargetException, java.lang.NoSuchMethodException- Throws:
java.lang.InstantiationExceptionjava.lang.IllegalAccessExceptionjava.lang.reflect.InvocationTargetExceptionjava.lang.NoSuchMethodException
-
lookupParam
protected java.lang.Object lookupParam(java.lang.reflect.Parameter param) throws org.codehaus.plexus.component.repository.exception.ComponentLookupException- Throws:
org.codehaus.plexus.component.repository.exception.ComponentLookupException
-
lookup
protected java.lang.Object lookup(java.lang.reflect.Parameter param, java.lang.String name) throws org.codehaus.plexus.component.repository.exception.ComponentLookupException- Throws:
org.codehaus.plexus.component.repository.exception.ComponentLookupException
-
resolveVersion
protected java.util.function.Function<java.lang.String,java.lang.String> resolveVersion(java.util.function.Function<org.eclipse.aether.resolution.VersionRangeResult,java.lang.Object> fn)
-
propertiesUpdater
protected java.util.function.BiConsumer<java.lang.String,java.lang.String> propertiesUpdater(java.util.Properties props)
-
preloadCommonPool
protected void preloadCommonPool()
To avoid the exec:java to consider common pool threads leaked, let's pre-create them.
-
joinNonDaemonThreads
protected void joinNonDaemonThreads(java.lang.ThreadGroup threadGroup)
-
joinThread
protected void joinThread(java.lang.Thread thread, long timeoutMsecs)
-
terminateThreads
protected void terminateThreads(java.lang.ThreadGroup threadGroup)
-
getActiveThreads
protected java.util.Collection<java.lang.Thread> getActiveThreads(java.lang.ThreadGroup threadGroup)
-
setSystemProperties
protected void setSystemProperties()
Pass any given system properties to the java system properties.
-
getClassLoader
protected java.net.URLClassLoader getClassLoader() throws org.apache.maven.plugin.MojoExecutionExceptionSet up a classloader for the execution of the main class.- Returns:
- the classloader
- Throws:
org.apache.maven.plugin.MojoExecutionException- if a problem happens
-
addAdditionalClasspathElements
protected void addAdditionalClasspathElements(java.util.List<java.nio.file.Path> path)
-
addRelevantPluginDependenciesToClasspath
protected void addRelevantPluginDependenciesToClasspath(java.util.List<java.nio.file.Path> path) throws org.apache.maven.plugin.MojoExecutionExceptionAdd any relevant project dependencies to the classpath. Indirectly takes includePluginDependencies and ExecutableDependency into consideration.- Parameters:
path- classpath ofURLobjects- Throws:
org.apache.maven.plugin.MojoExecutionException- if a problem happens
-
addRelevantProjectDependenciesToClasspath
protected void addRelevantProjectDependenciesToClasspath(java.util.List<java.nio.file.Path> path)
Add any relevant project dependencies to the classpath. Takes includeProjectDependencies into consideration.- Parameters:
path- classpath ofURLobjects
-
waitFor
protected void waitFor(long millis)
Stop program execution for nn millis.- Parameters:
millis- the number of millis-seconds to wait for,0stops program forever.
-
-