user373201
user373201

Reputation: 11425

run maven project with dependencies from command line

I have a maven project which depends on 2 other local project and a host of 3rd party jars. I want to move the jar file to another machine and run the application from there. I tried the mvn exec:exec command but it was not able to find the 2 local projects, which makes sense. How do I get this scenario to work. the machine on which the application will be installed have maven in it and is connected to the internet, so if required it can download the jars mentioned in the pom.

Upvotes: 3

Views: 12409

Answers (2)

Somum
Somum

Reputation: 2422

I couldnt really understand much from the above answer. Maven looks complicated to me. Anyway I figured out if you do this then your problem will be solved

First in pom.xml file add this plugin in build section just like shown below

<build>
  <plugins>
    <plugin>
      <artifactId>maven-assembly-plugin</artifactId>
      <configuration>
        <descriptorRefs>
          <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
      </configuration>
   </plugin>
 </plugins>
</build>

I think jar-with-dependencies does the trick

Now follow this sequence of commands from your root directory ( where the pom.xml exists )

mvn compile
mvn package
mvn install assembly:assembly

I dont know if mvn package is required or not but the third one is important. Now if you see your target/ you will see a jar with dependency.

How to run

 java -cp target/Your-Jar-1.0-SNAPSHOT-jar-with-dependencies.jar com.mycode.myapp

this will run your java program code com.mycode.myapp main method... ( Disclaimer : I am not an expert in maven but this worked for me )

Upvotes: 8

sethcall
sethcall

Reputation: 2897

Use the Maven-assembly-plugin to make a jar-with-dependencies, which will result in the execution of mvn package creating a more readily deployable package.

Upvotes: 1

Related Questions