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.
- mvn compile
Once your code is compiled, the following command runs your class
Without arguments:
- mvn exec:java -Dexec.mainClass="com.vineetmanohar.module.Main"
With arguments:
- mvn exec:java -Dexec.mainClass="com.vineetmanohar.module.Main" -Dexec.args="arg0 arg1 arg2"
With runtime dependencies in the CLASSPATH:
- 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 thetest phase.
- <build>
- <plugins>
- <plugin>
- <groupId>org.codehaus.mojo</groupId>
- <artifactId>exec-maven-plugin</artifactId>
- <version>1.1.1</version>
- <executions>
- <execution>
- <phase>test</phase>
- <goals>
- <goal>java</goal>
- </goals>
- <configuration>
- <mainClass>com.vineetmanohar.module.CodeGenerator</mainClass>
- <arguments>
- <argument>arg0</argument>
- <argument>arg1</argument>
- </arguments>
- </configuration>
- </execution>
- </executions>
- </plugin>
- </plugins>
- </build>
To run the exec plugin with above configuration, simply run the corresponding phase.
- 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 <profile> tag.
- <profiles>
- <profile>
- <id>code-generator</id>
- <build>
- <plugins>
- <plugin>
- <groupId>org.codehaus.mojo</groupId>
- <artifactId>exec-maven-plugin</artifactId>
- <version>1.1.1</version>
- <executions>
- <execution>
- <phase>test</phase>
- <goals>
- <goal>java</goal>
- </goals>
- <configuration>
- <mainClass>com.vineetmanohar.module.CodeGenerator</mainClass>
- <arguments>
- <argument>arg0</argument>
- <argument>arg1</argument>
- </arguments>
- </configuration>
- </execution>
- </executions>
- </plugin>
- </plugins>
- </build>
- </profile>
- </profiles>
To call the above profile, run the following command:
- mvn test -Pcode-generator
Advanced options:
You can get a list of all available parameters by typing:
- mvn exec:help -Ddetail=true -Dgoal=java
arguments (exec.arguments)
classpathScope (exec.classpathScope, Default: compile)
cleanupDaemonThreads (exec.cleanupDaemonThreads)
commandlineArgs (exec.args)
daemonThreadJoinTimeout (exec.daemonThreadJoinTimeout, Default: 15000)
executableDependency
includePluginDependencies (exec.includePluginDependencies, Default: false)
includeProjectDependencies (exec.includeProjectDependencies, Default: true)
mainClass (exec.mainClass)
sourceRoot (sourceRoot)
stopUnresponsiveDaemonThreads (exec.stopUnresponsiveDaemonThreads)
systemProperties
testSourceRoot (testSourceRoot)
FAQ and Errors
Why do I get this error when specifying arguments to my main method:
- [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.
- If possible upgrade to 1.1 or later and use exec.args instead of exec.arguments.
- If you can't upgrade the plugin version, you can still use command line arguments with a profile and use multiple <argument> tags associated in the pom.xml