maven,3 ways to run Java main from Maven Overview

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.
1) Running from Command line
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. view plaincopy to clipboardprint?
  1. mvn compile
mvn compile
Once your code is compiled, the following command runs your class
Without arguments:
view plaincopy to clipboardprint?
  1. mvn exec:java -Dexec.mainClass="com.vineetmanohar.module.Main"
mvn exec:java -Dexec.mainClass="com.vineetmanohar.module.Main"
With arguments:
view plaincopy to clipboardprint?
  1. mvn exec:java -Dexec.mainClass="com.vineetmanohar.module.Main" -Dexec.args="arg0 arg1 arg2"
mvn exec:java -Dexec.mainClass="com.vineetmanohar.module.Main" -Dexec.args="arg0 arg1 arg2"
With runtime dependencies in the CLASSPATH:
view plaincopy to clipboardprint?
  1. mvn exec:java -Dexec.mainClass="com.vineetmanohar.module.Main" -Dexec.classpathScope=runtime
mvn exec:java -Dexec.mainClass="com.vineetmanohar.module.Main" -Dexec.classpathScope=runtime
2) Running in a phase in pom.xml
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. view plaincopy to clipboardprint?
  1. org.codehaus.mojo
  2. exec-maven-plugin
  3. 1.1.1
  4. test
  5. java
  6. com.vineetmanohar.module.CodeGenerator
  7. arg0
  8. arg1


org.codehaus.mojo exec-maven-plugin 1.1.1 test
java com.vineetmanohar.module.CodeGenerator arg0 arg1


To run the exec plugin with above configuration, simply run the corresponding phase. view plaincopy to clipboardprint?
  1. mvn test
mvn test
3) Running in a profile in pom.xml
You can also run the main method in a different profile. Simply wrap the above config in the tag. view plaincopy to clipboardprint?
  1. code-generator
  2. org.codehaus.mojo
  3. exec-maven-plugin
  4. 1.1.1
  5. test
  6. java
  7. com.vineetmanohar.module.CodeGenerator
  8. arg0
  9. arg1




code-generator org.codehaus.mojo exec-maven-plugin 1.1.1 test
java com.vineetmanohar.module.CodeGenerator arg0 arg1



To call the above profile, run the following command: view plaincopy to clipboardprint?
  1. mvn test -Pcode-generator
mvn test -Pcode-generator

Advanced options:

You can get a list of all available parameters by typing: view plaincopy to clipboardprint?
  1. mvn exec:help -Ddetail=true -Dgoal=java
mvn exec:help -Ddetail=true -Dgoal=java
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 _disibledevent=> executableDependency
If provided the ExecutableDependency identifies which of the plugin dependencies contains the executable class. This will have the affect of _disibledevent=>view plaincopy to clipboardprint?
  1. [ERROR] BUILD ERROR
  2. [INFO] ------------------------------------------------------------------------
  3. [INFO] Failed to configure plugin parameters for: org.codehaus.mojo:exec-maven-plugin:1.1.1
  4. on the command line, specify: '-Dexec.arguments=VALUE'
  5. Cause: Cannot assign configuration entry 'arguments' to 'class [Ljava.lang.String;' from '${exec.arguments}',
  6. which is of type class java.lang.String
  7. [INFO] ------------------------------------------------------------------------
  8. [INFO] Trace
  9. org.apache.maven.lifecycle.LifecycleExecutionException: Error configuring: org.codehaus.mojo:exec-maven-plugin.
  10. Reason: Unable to parse the created DOM for plugin configuration
  11. at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoals(DefaultLifecycleExecutor.java:588)
  12. at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeStandaloneGoal(DefaultLifecycleExecutor.java:513)
  13. at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoal(DefaultLifecycleExecutor.java:483)
  14. at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoalAndHandleFailures(DefaultLifecycleExecutor.java:331)
  15. at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeTaskSegments(DefaultLifecycleExecutor.java:292)
  16. at org.apache.maven.lifecycle.DefaultLifecycleExecutor.execute(DefaultLifecycleExecutor.java:142)
  17. at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:336)
  18. at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:129)
  19. at org.apache.maven.cli.MavenCli.main(MavenCli.java:301)
[ERROR] BUILD ERROR [INFO] ------------------------------------------------------------------------ [INFO] Failed to configure plugin parameters for: org.codehaus.mojo:exec-maven-plugin:1.1.1 on the command line, specify: '-Dexec.arguments=VALUE' Cause: Cannot assign configuration entry 'arguments' to 'class [Ljava.lang.String;' from '${exec.arguments}', which is of type class java.lang.String [INFO] ------------------------------------------------------------------------ [INFO] Trace org.apache.maven.lifecycle.LifecycleExecutionException: Error configuring: org.codehaus.mojo:exec-maven-plugin. Reason: Unable to parse the created DOM for plugin configuration at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoals(DefaultLifecycleExecutor.java:588) at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeStandaloneGoal(DefaultLifecycleExecutor.java:513) at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoal(DefaultLifecycleExecutor.java:483) at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoalAndHandleFailures(DefaultLifecycleExecutor.java:331) at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeTaskSegments(DefaultLifecycleExecutor.java:292) at org.apache.maven.lifecycle.DefaultLifecycleExecutor.execute(DefaultLifecycleExecutor.java:142) at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:336) at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:129) at org.apache.maven.cli.MavenCli.main(MavenCli.java:301)
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.
  1. If possible upgrade to 1.1 or later and use exec.args instead of exec.arguments.
  2. If you can't upgrade the plugin version, you can still use command line arguments with a profile and use multiple tags associated in the pom.xml
References
  • Official Maven Exec Plugin Documentation
ShareThis Related posts:
  1. Tweet your builds with Maven Twitter Plugin
  2. The plugin ‘org.codehaus.mojo:selenium-maven-plugin’ does not exist or no valid version could be found
  3. Maven Cargo JBoss
  4. How to automate project versioning and release with Maven
  5. Maven Selenium
域名空间 团购空间 企业内部搜索
Tags:  maven配置 maven实战 maven2 maven

延伸阅读

最新评论

发表评论