Summary: How to run an Eclipse Java Maven Project outside of Eclipse
Details:
I am a Maven newbie and I have created a very simple Maven project in Eclipse.
The Package Explorer view of my project is as follows:
The source code for the two simple Java files is as follows:
package Pkg01;
public class Calculator
{
public double add(double number1, double number2)
{
return number1 + number2;
}
public static void main(String[] args) {
Calculator calculator = new Calculator();
System.out.println(calculator.add(100, 200));
}
}
and
package Pkg01;
import junit.framework.TestCase;
public class TestCalculator extends TestCase
{
public void testAdd()
{
Calculator calculator = new Calculator();
double result = calculator.add(10, 50);
assertEquals(60, result, 0);
}
}
From within Eclipse I am able to run the Java Application, the Junit Test and the Maven test, without any issues.
My question is how can I invoke the mvn
command from the DOS shell so that I can:
- Run the Java Application
- Run the Junit Test
- Run the Maven test
BTW running mvn exec:java
from the parent directory of the project is not executing the Java application for me.
My pom.xml
file looks like this:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.newbie</groupId>
<artifactId>Stackoverflow</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Stackoverflow</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
mvn exec:java
gives the following error:
[INFO] Scanning for projects...
[INFO]
[INFO] ----------------------< org.newbie:Stackoverflow >----------------------
[INFO] Building Stackoverflow 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- exec-maven-plugin:1.6.0:java (default-cli) @ Stackoverflow ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.957 s
[INFO] Finished at: 2019-11-05T22:47:00-06:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.6.0:java (default-cli) on project Stackoverflow: The parameters 'mainClass' for goal org.codehaus.mojo:exec-maven-plugin:1.6.0:java are missing or invalid -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/PluginParameterException
Here are the reasons why I feel that my question is not a duplicate.
I created a very simple project with maven-archetype-quickstart and my environment is correct because everything {i.e. (1) Run the Java Application, (2) Run the Junit Test and (3) Run the Maven test} works for me within the Eclipse IDE environment. The other poster was having trouble executing, which I don’t.
The comments that the other poster received were mainly for fixing his environment to get it running. I don't have that issue since, as already mentioned, everything works for me within the Eclipse IDE environment.
All I am asking is what I need to do get the following things working from the command shell:
- Running the Java Application
- Running the Junit Test
- Running the Maven test