Maven Run Project

Maven Run Project

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>

How do I run a java application with maven from command line on ubuntu?

Here, you have maven project which has one dependency on jackson-databind which in turn will have some more dependencies i.e jackson-core and jackson-annotations.

Classes from these dependencies are not bundled in your application jar, so you cannot just run the Application main class from your project directly using java command, you need to specify the dependent classes on java classpath so that java can load these dependent classes of your program.

Since, it is a maven project, these dependent jars will be pulled into maven default directory (.m2) into your's home path and as you mentioned, you are using ubuntu that will be /home/<your username>/, For example your username which you are logged in with is singularli then your home path must be /home/singularli, you can also check it with echo $HOME command.

So, you would find the maven folder, which stores all the jar(s), into your home /home/singularli/.m2/repository, now here you would find jars like jackson-databind, jackson-core (these will be little inside subdirectories, as it keeps according to the package name, given below command example will give you more idea about it).

At last, once you find these jars, you would need to specify the classpath using -cp flag and include these jars with your application jar which would look like as given below:

java -cp "target/httpclient-tutorial-1.0-SNAPSHOT.jar:/home/singularli/.m2/repository/com/fasterxml/jackson/core/jackson-core/2.11.4/jackson-core-2.11.4.jar:/home/singularli/.m2/repository/com/fasterxml/jackson/core/jackson-databind/2.11.4/jackson-databind-2.11.4.jar:/home/singularli/.m2/repository/com/fasterxml/jackson/core/jackson-annotations/2.11.4/jackson-annotations-2.11.4.jar" dev.danvega.Application

It should work the same way as shown in that video, you referred in your question.

Please notice that you may have different versions i.e com/fasterxml/jackson/core/jackson-annotations/2.11.4, I included 2.11.4 as an example, you may check the version in this project and include that, if different versions are there and you included anyone of them, it may cause some issue as some feature used in this project might not be present in that version

How to compile and run java maven project through command line?

Assuming you have set path to execute maven commands with command line...

execute following commands from the path of pom.xml

  1. mvn package - then there should be a jar file created called dbx.jar
  2. java -cp dbx.jar com.ms3.dbx.MS3

https://maven.apache.org/guides/getting-started/maven-in-five-minutes.html

Running an Eclipse Java Maven project from a Windows (DOS) shell

The error message states parameters 'mainClass' for goal org.codehaus.mojo:exec-maven-plugin.If you look at the exec:java documentation the required parameter is mainClass

I don't see exec-maven-plugin being configured in your pom.xml so you could execute Java programs using below command

mvn exec:java -Dexec.mainClass="com.example.Main"

Make sure you have compiled you code either using mvn compile or mvn install command before running mvn exec.. command

C:\data\development\app_code\my-app>mvn exec:java -Dexec.mainClass="com.mycompany.app.App"
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building my-app 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- exec-maven-plugin:1.6.0:java (default-cli) @ my-app ---
300.0
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.960 s
[INFO] Finished at: 2019-11-06T09:55:23+00:00
[INFO] Final Memory: 9M/116M
[INFO] ------------------------------------------------------------------------
C:\Sachin\data\development\app_code\test-maven\my-app>

For Junit testing you could call mvn test which would give you the test results status similar to your Eclipse IDE

-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.mycompany.app.AppTest
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.099 sec

Results :

Tests run: 2, Failures: 0, Errors: 0, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 6.198 s
[INFO] Finished at: 2019-11-06T09:55:58+00:00
[INFO] Final Memory: 19M/160M
[INFO] ------------------------------------------------------------------------

I do see the same details being mentioned in the link by Karthikeyan Vaithilingam

Reference:

https://www.mojohaus.org/exec-maven-plugin/usage.html


OP Addendum
You have provided the solution for:

1. Running the Java Application

3. Running the Maven test

From the IDE I can also invoke:

2. Running the Junit Test

Admittedly this is very similar to 3. Running the Maven test but the output looks different.

Sample Image

Is there a way to do that from outside of the IDE environment?

Run a Maven Project using IntelliJ IDEA

Refresh the Maven project in the Maven Projects tool window (Reimport All Maven Projects), if it doesn't help, refer to this answer for diagnostics.

Reimport

The issue is that your source roots were not configured correctly from the Maven model for some reason and the .java file appears in a plain directory instead of the Source root.

You can tell that by the color of the folders and by the icon of the file.

how to run/execute a java code(maven project) outside the IDE

first you have to build the maven project using this command in your project directory:

mvn package

After a successful build, you will see a .jar file has created in target folder same as your package name and version.

finally you have to RUN the project.

To run the project use this command:

java -cp target/jarfileName.jar path_of_the_project_startup

Done.

How do I run a single class after compiling the whole project with maven in command line?

You can create a deployable artifact as war file (By changing packaging to war using <packaging>war</packaging>) that will contain all the dependent jar files under its lib directory.

You can then run: mvn clean install

You can then extract it and run your class file directly by giving class-path parameter.

Other option is to use maven run plugin -
mvn exec:java -Dexec.mainClass="com.example.Main" [-Dexec.args="argument1"] ...

reference: Maven Run Project

run main class of Maven project

Try the maven-exec-plugin. From there:

mvn exec:java -Dexec.mainClass="com.example.Main"

This will run your class in the JVM. You can use -Dexec.args="arg0 arg1" to pass arguments.

If you're on Windows, apply quotes for exec.mainClass and exec.args:

mvn exec:java -D"exec.mainClass"="com.example.Main"

If you're doing this regularly, you can add the parameters into the pom.xml as well:

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<executions>
<execution>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>com.example.Main</mainClass>
<arguments>
<argument>foo</argument>
<argument>bar</argument>
</arguments>
</configuration>
</plugin>


Related Topics



Leave a reply



Submit