转载地址:http://www.vineetmanohar.com/2009/11/3-ways-to-run-java-main-from-maven/
Maven exec plugin lets you run the main method of a Java class in your project, with the project dependencies automatically included in the classpath. This article show you 3 ways of using the maven exec plugin to run java, with code examples.
Since you are not running your code in a maven phase, you first need to compile the code. Remember exec:java does not automatically compile your code, you need to do that first.
Once your code is compiled, the following command runs your class
You can also run the main method in a maven phase. For example, you can run the CodeGenerator.main() method as part of the test phase.
To run the exec plugin with above configuration, simply run the corresponding phase.
You can also run the main method in a different profile. Simply wrap the above config in the
To call the above profile, run the following command:
You can get a list of all available parameters by typing:
arguments (exec.arguments)
The class arguments.
classpathScope (exec.classpathScope, Default: compile)
Defines the scope of the classpath passed to the plugin. Set to compile, test, runtime or system depending on your needs
cleanupDaemonThreads (exec.cleanupDaemonThreads)
Wether 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.
commandlineArgs (exec.args)
Arguments for the executed program
daemonThreadJoinTimeout (exec.daemonThreadJoinTimeout, Default: 15000)
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.
executableDependency
If provided the ExecutableDependency identifies which of the plugin dependencies contains the executable class. This will have the affect 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 (exec.includePluginDependencies, Default: false)
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.
includeProjectDependencies (exec.includeProjectDependencies, Default: true)
Indicates if the project dependencies should be used when executing the main class.
mainClass (exec.mainClass)
The main class to execute.
sourceRoot (sourceRoot)
This folder is added to the list of those folders containing source to be compiled. Use this if your plugin generates source code.
stopUnresponsiveDaemonThreads (exec.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 Timer which doesn't respond to interruption. To have Timer fixed, vote for this bug.
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.
testSourceRoot (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.
Solution
exec.arguments was used before version 1.1 of the exec plugin, it did not support conversion of command line String to String[] array.