How to run maven generated jar on CLI

前端 未结 2 696
南方客
南方客 2021-01-11 18:29

I\'m trying to get a maven managed project to run on the command line.

I have a set of dependencies in the pom.xml which are subsequently downloaded and installed i

相关标签:
2条回答
  • 2021-01-11 18:50

    Options 1:
    The jar created does not have the dependent jar files. So, you need to tell java the class-path where all the dependent jars are

        java -cp /lcoation/of/dependency1.jar:/location/of/dependency2.jar:/location/of/dependency3.jar -jar project-SNAPSHOT.jar
    

    Option 2:
    The easier and much better solution is to use AppAssembler plugin. What it does it packages your jar in a directory structure that contains

    1. dependent jars
    2. the created jar
    3. shell/windows scripts to execute it

    have a look here http://www.mojohaus.org/appassembler/appassembler-maven-plugin/

    Option 3:
    If you do not want all the baggage and just wanted to have one jar-with-dependency You may want to refer here How can I create an executable JAR with dependencies using Maven?

    This will contain all the dependent jars within it.


    Edit 1: For Option 1, Brad M mentioned that you can get a list of all your project's deps using the dependency plugin. dependency:build-classpath

    0 讨论(0)
  • 2021-01-11 19:05
    mvn exec:java -Dexec.mainClass="com.vineetmanohar.module.Main" -Dexec.classpathScope=runtime  
    

    You can find more examples here: 3 ways to run Java main from Maven.

    0 讨论(0)
提交回复
热议问题