Fork me on GitHub

exec:java

Full name:

org.codehaus.mojo:exec-maven-plugin:3.4.1:java

Description:

Executes the supplied java class in the current VM with the enclosing project's dependencies as classpath.

Attributes:

  • Requires a Maven project to be executed.
  • Requires dependency resolution of artifacts in scope: test.
  • The goal is thread-safe and supports parallel builds.
  • Since version: 1.0.

Required Parameters

Name Type Since Description
<mainClass> String 1.0 The main class to execute.
With Java 9 and above you can prefix it with the modulename, e.g. com.greetings/com.greetings.Main Without modulename the classpath will be used, with modulename a new modulelayer will be created.

Note that you can also provide a Runnable fully 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 -parameters Java compiler option) for loose coupling. Current support loose injections are:

  • systemProperties: Properties, session system properties
  • systemPropertiesUpdater: 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 properties
  • userPropertiesUpdater: BiConsumer<String, String>, session user properties update callback (pass the key/value to update, null value means removal of the key)
  • projectProperties: Properties, project properties
  • projectPropertiesUpdater: 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 a groupId:artifactId you get the latest resolved version from the project repositories

User Property: exec.mainClass

Optional Parameters

Name Type Since Description
<addOutputToClasspath> boolean 1.5.1 Add project output directory to classpath. This might be undesirable when the exec plugin is run before the compile step. Default is true.
Default: true
User Property: addOutputToClasspath
<addResourcesToClasspath> boolean 1.5.1 Add project resource directories to classpath. This is especially useful if the exec plugin is used for a code generator that reads its settings from the classpath.
Default: false
User Property: addResourcesToClasspath
<additionalClasspathElements> List<String> 1.3 Additional elements to be appended to the classpath.
<arguments> String[] 1.0 The class arguments.
User Property: exec.arguments
<blockSystemExit> boolean 3.2.0 Whether to try and prohibit the called Java program from terminating the JVM (and with it the whole Maven build) by calling System.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 that System.exit(int) was called. Otherwise, it will throw a SystemExitException, 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 the exec instead of the java goal in such cases, which now with this option is no longer necessary.
Default: false
User Property: exec.blockSystemExit
<classpathFilenameExclusions> List<String> 3.0.1 List of file to exclude from the classpath. It matches the jar name, for example slf4j-simple-1.7.30.jar.
<classpathScope> String - Defines the scope of the classpath passed to the plugin.
  • runtime (default): Include "compile" and "runtime" scopes
  • compile: Include "compile", "provided", and "system" scopes
  • test: Include all scopes
  • provided: Include "compile", "runtime", "provided", and "system" scopes
  • system: Include "system" scope
Since 1.1.2, the default value is 'runtime' instead of 'compile'.
Default: runtime
User Property: exec.classpathScope
<cleanupDaemonThreads> boolean 1.1-beta-1 Whether to interrupt/join and possibly stop the daemon threads upon quitting.
If this is false, 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 daemonThreadJoinTimeout and stopUnresponsiveDaemonThreads for further tuning.


Default: true
User Property: exec.cleanupDaemonThreads
<commandlineArgs> String - Arguments separated by space for the executed program. For example: "-j 20"
User Property: exec.args
<daemonThreadJoinTimeout> long 1.1-beta-1 This defines the number of milliseconds to wait for daemon threads to quit following their interruption.
This is only taken into account if cleanupDaemonThreads is true. 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.


Default: 15000
User Property: exec.daemonThreadJoinTimeout
<executableDependency> ExecutableDependency 1.1-beta-1 If provided the ExecutableDependency identifies which of the plugin dependencies contains the executable class. This will have the effect of only including plugin dependencies required by the identified ExecutableDependency.

If includeProjectDependencies is set to true, all of the project dependencies will be included on the executable's classpath. Whether a particular project dependency is a dependency of the identified ExecutableDependency will be irrelevant to its inclusion in the classpath.

<includePluginDependencies> boolean 3.4.0 Indicates if this plugin's dependencies should be used when executing the main class.

This is useful when project dependencies are not appropriate. Using only the plugin dependencies can be particularly useful when the project is not a java project. For example a mvn project using the csharp plugins only expects to see dotnet libraries as dependencies.


Default: false
User Property: exec.includePluginsDependencies
<includeProjectDependencies> boolean 1.1-beta-1 Indicates if the project dependencies should be used when executing the main class.
Default: true
User Property: exec.includeProjectDependencies
<keepAlive> boolean 1.0
Deprecated.
since 1.1-alpha-1

Indicates if mojo should be kept running after the mainclass terminates. Use full for server like apps with daemon threads.
Default: false
User Property: exec.keepAlive
<preloadCommonPool> int 3.0.1 Forces 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.
Default: 0
User Property: exec.preloadCommonPool
<skip> boolean 1.0.1 Skip the execution. Starting with version 1.4.0 the former name skip has been changed into exec.skip.
Default: false
User Property: exec.skip
Alias: skip
<sourceRoot> File - This folder is added to the list of those folders containing source to be compiled. Use this if your plugin generates source code.
User Property: sourceRoot
<stopUnresponsiveDaemonThreads> boolean 1.1-beta-1 Wether to call Thread.stop() following a timing out of waiting for an interrupted thread to finish. This is only taken into account if cleanupDaemonThreads is true and the daemonThreadJoinTimeout threshold has been reached for an uncooperative thread. If this is false, or if Thread.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 to true if you are invoking problematic code that you can't fix. An example is java.util.Timer which doesn't respond to interruption. To have Timer fixed, vote for this bug.

Note: In JDK 20+, the long deprecated Thread.stop() (since JDK 1.2) has been removed and will throw an UnsupportedOperationException. 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.


Default: false
User Property: exec.stopUnresponsiveDaemonThreads
<systemProperties> AbstractProperty[] 1.0 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.
<testSourceRoot> File - This folder is added to the list of those folders containing source to be compiled for testing. Use this if your plugin generates test source code.
User Property: testSourceRoot

Parameter Details

<addOutputToClasspath>

Add project output directory to classpath. This might be undesirable when the exec plugin is run before the compile step. Default is true.
  • Type: boolean
  • Since: 1.5.1
  • Required: No
  • User Property: addOutputToClasspath
  • Default: true

<addResourcesToClasspath>

Add project resource directories to classpath. This is especially useful if the exec plugin is used for a code generator that reads its settings from the classpath.
  • Type: boolean
  • Since: 1.5.1
  • Required: No
  • User Property: addResourcesToClasspath
  • Default: false

<additionalClasspathElements>

Additional elements to be appended to the classpath.
  • Type: java.util.List<java.lang.String>
  • Since: 1.3
  • Required: No

<arguments>

The class arguments.
  • Type: java.lang.String[]
  • Since: 1.0
  • Required: No
  • User Property: exec.arguments

<blockSystemExit>

Whether to try and prohibit the called Java program from terminating the JVM (and with it the whole Maven build) by calling System.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 that System.exit(int) was called. Otherwise, it will throw a SystemExitException, 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 the exec instead of the java goal in such cases, which now with this option is no longer necessary.
  • Type: boolean
  • Since: 3.2.0
  • Required: No
  • User Property: exec.blockSystemExit
  • Default: false

<classpathFilenameExclusions>

List of file to exclude from the classpath. It matches the jar name, for example slf4j-simple-1.7.30.jar.
  • Type: java.util.List<java.lang.String>
  • Since: 3.0.1
  • Required: No

<classpathScope>

Defines the scope of the classpath passed to the plugin.
  • runtime (default): Include "compile" and "runtime" scopes
  • compile: Include "compile", "provided", and "system" scopes
  • test: Include all scopes
  • provided: Include "compile", "runtime", "provided", and "system" scopes
  • system: Include "system" scope
Since 1.1.2, the default value is 'runtime' instead of 'compile'.
  • Type: java.lang.String
  • Required: No
  • User Property: exec.classpathScope
  • Default: runtime

<cleanupDaemonThreads>

Whether to interrupt/join and possibly stop the daemon threads upon quitting.
If this is false, 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 daemonThreadJoinTimeout and stopUnresponsiveDaemonThreads for further tuning.

  • Type: boolean
  • Since: 1.1-beta-1
  • Required: No
  • User Property: exec.cleanupDaemonThreads
  • Default: true

<commandlineArgs>

Arguments separated by space for the executed program. For example: "-j 20"
  • Type: java.lang.String
  • Required: No
  • User Property: exec.args

<daemonThreadJoinTimeout>

This defines the number of milliseconds to wait for daemon threads to quit following their interruption.
This is only taken into account if cleanupDaemonThreads is true. 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.

  • Type: long
  • Since: 1.1-beta-1
  • Required: No
  • User Property: exec.daemonThreadJoinTimeout
  • Default: 15000

<executableDependency>

If provided the ExecutableDependency identifies which of the plugin dependencies contains the executable class. This will have the effect of only including plugin dependencies required by the identified ExecutableDependency.

If includeProjectDependencies is set to true, all of the project dependencies will be included on the executable's classpath. Whether a particular project dependency is a dependency of the identified ExecutableDependency will be irrelevant to its inclusion in the classpath.

  • Type: org.codehaus.mojo.exec.ExecutableDependency
  • Since: 1.1-beta-1
  • Required: No

<includePluginDependencies>

Indicates if this plugin's dependencies should be used when executing the main class.

This is useful when project dependencies are not appropriate. Using only the plugin dependencies can be particularly useful when the project is not a java project. For example a mvn project using the csharp plugins only expects to see dotnet libraries as dependencies.

  • Type: boolean
  • Since: 3.4.0
  • Required: No
  • User Property: exec.includePluginsDependencies
  • Default: false

<includeProjectDependencies>

Indicates if the project dependencies should be used when executing the main class.
  • Type: boolean
  • Since: 1.1-beta-1
  • Required: No
  • User Property: exec.includeProjectDependencies
  • Default: true

<keepAlive>

Deprecated.
since 1.1-alpha-1

Indicates if mojo should be kept running after the mainclass terminates. Use full for server like apps with daemon threads.
  • Type: boolean
  • Since: 1.0
  • Required: No
  • User Property: exec.keepAlive
  • Default: false

<mainClass>

The main class to execute.
With Java 9 and above you can prefix it with the modulename, e.g. com.greetings/com.greetings.Main Without modulename the classpath will be used, with modulename a new modulelayer will be created.

Note that you can also provide a Runnable fully 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 -parameters Java compiler option) for loose coupling. Current support loose injections are:

  • systemProperties: Properties, session system properties
  • systemPropertiesUpdater: 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 properties
  • userPropertiesUpdater: BiConsumer<String, String>, session user properties update callback (pass the key/value to update, null value means removal of the key)
  • projectProperties: Properties, project properties
  • projectPropertiesUpdater: 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 a groupId:artifactId you get the latest resolved version from the project repositories
  • Type: java.lang.String
  • Since: 1.0
  • Required: Yes
  • User Property: exec.mainClass

<preloadCommonPool>

Forces 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.
  • Type: int
  • Since: 3.0.1
  • Required: No
  • User Property: exec.preloadCommonPool
  • Default: 0

<skip>

Skip the execution. Starting with version 1.4.0 the former name skip has been changed into exec.skip.
  • Type: boolean
  • Since: 1.0.1
  • Required: No
  • User Property: exec.skip
  • Default: false
  • Alias: skip

<sourceRoot>

This folder is added to the list of those folders containing source to be compiled. Use this if your plugin generates source code.
  • Type: java.io.File
  • Required: No
  • User Property: sourceRoot

<stopUnresponsiveDaemonThreads>

Wether to call Thread.stop() following a timing out of waiting for an interrupted thread to finish. This is only taken into account if cleanupDaemonThreads is true and the daemonThreadJoinTimeout threshold has been reached for an uncooperative thread. If this is false, or if Thread.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 to true if you are invoking problematic code that you can't fix. An example is java.util.Timer which doesn't respond to interruption. To have Timer fixed, vote for this bug.

Note: In JDK 20+, the long deprecated Thread.stop() (since JDK 1.2) has been removed and will throw an UnsupportedOperationException. 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.

  • Type: boolean
  • Since: 1.1-beta-1
  • Required: No
  • User Property: exec.stopUnresponsiveDaemonThreads
  • Default: false

<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.
  • Type: org.codehaus.mojo.exec.AbstractProperty[]
  • Since: 1.0
  • Required: No

<testSourceRoot>

This folder is added to the list of those folders containing source to be compiled for testing. Use this if your plugin generates test source code.
  • Type: java.io.File
  • Required: No
  • User Property: testSourceRoot