Maven Run Project

JavaMaven

Java Problem Overview


Is there a Maven "phase" or "goal" to simply execute the main method of a Java class? I have a project that I'd like to test manually by simply doing something like "mvn run".

Java Solutions


Solution 1 - Java

See the exec maven plugin. You can run Java classes using:

mvn exec:java -Dexec.mainClass="com.example.Main" [-Dexec.args="argument1"] ...

The invocation can be as simple as mvn exec:java if the plugin configuration is in your pom.xml. The plugin site on Mojohaus has a more detailed example.

<project>
	<build>
		<plugins>
			<plugin>
				<groupId>org.codehaus.mojo</groupId>
				<artifactId>exec-maven-plugin</artifactId>
				<version>1.2.1</version>
				<configuration>
					<mainClass>com.example.Main</mainClass>
					<arguments>
						<argument>argument1</argument>
					</arguments>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

Solution 2 - Java

1. Edit POM.xml

Add the following property in pom.xml. Make sure you use the fully qualified class name (i.e. with package name) which contains the main method:

<properties>
		<exec.mainClass>fully-qualified-class-name</exec.mainClass>
</properties>

2. Run Command

Now from the terminal, trigger the following command:

mvn clean compile exec:java

NOTE You can pass further arguments via -Dexec.args="xxx" flag.

Solution 3 - Java

The above mentioned answers are correct but I am simplifying it for noobs like me.Go to your project's pom file. Add a new property exec.mainClass and give its value as the class which contains your main method. For me it was DriverClass in mainpkg. Change it as per your project. enter image description here

Having done this navigate to the folder that contains your project's pom.xml and run this on the command prompt mvn exec:java. This should call the main method.

Solution 4 - Java

No need to add new plugin in pom.xml. Just run this command

mvn org.codehaus.mojo:exec-maven-plugin:1.5.0:java -Dexec.mainClass="com.example.Main" | grep -Ev '(^\[|Download\w+:)' 

See the [maven exec plugin][1] for more usage.

[1]: http://www.mojohaus.org/exec-maven-plugin/usage.html "exec usage"

Solution 5 - Java

Give the Exec Maven plugin a try

Solution 6 - Java

clean package exec:java -P Class_Containing_Main_Method command is also an option if you have only one Main method(PSVM) in the project, with the following Maven Setup.

Don't forget to mention the class in the <properties></properties> section of pom.xml :

<properties>
	<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	<java.main.class>com.test.service.MainTester</java.main.class>
</properties>

<plugin>
		<groupId>org.codehaus.mojo</groupId>
		<artifactId>exec-maven-plugin</artifactId>
		<version>1.2.1</version>
		<configuration>
		   <mainClass>${java.main.class}</mainClass>
		</configuration>
</plugin>

STS Run Configuration along with above Maven Setup:

enter image description here

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionVerhogenView Question on Stackoverflow
Solution 1 - JavaRobert MunteanuView Answer on Stackoverflow
Solution 2 - JavaSaikatView Answer on Stackoverflow
Solution 3 - JavashshnkView Answer on Stackoverflow
Solution 4 - Javaarulraj.netView Answer on Stackoverflow
Solution 5 - JavadfaView Answer on Stackoverflow
Solution 6 - JavaAbhijeetView Answer on Stackoverflow