java 如何在 CLI 上运行 maven 生成的 jar

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12749206/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 10:03:32  来源:igfitidea点击:

How to run maven generated jar on CLI

javamavencommand-line-interface

提问by Nick

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

我正在尝试让 Maven 管理的项目在命令行上运行。

I have a set of dependencies in the pom.xml which are subsequently downloaded and installed in the ~/.m2/repository/. I've included the necessary config in my pom to add the classpath to the jar manifest.

我在 pom.xml 中有一组依赖项,它们随后被下载并安装在 ~/.m2/repository/ 中。我已经在我的 pom 中包含了必要的配置,以将类路径添加到 jar 清单。

Now the problem is i'm attempting to run the jar thus: java -jar project-SNAPSHOT.jar.

现在的问题是我试图这样运行 jar:java -jar project-SNAPSHOT.jar。

Java can't find the downloaded dependencies (i'm assuming because they are listed without paths in the manifest?) , but i'm not sure how best to get this running.

Java 找不到下载的依赖项(我假设是因为它们在清单中没有列出路径?),但我不确定如何最好地运行它。

采纳答案by Nishant

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

选项1:
创建的jar没有依赖的jar文件。所以,你需要告诉 java 所有依赖的 jars 所在的类路径

    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 AppAssemblerplugin. What it does it packages your jar in a directory structure that contains

选项 2:
更简单更好的解决方案是使用AppAssembler插件。它的作用是将您的 jar 打包到一个目录结构中,该目录结构包含

  1. dependent jars
  2. the created jar
  3. shell/windows scripts to execute it
  1. 依赖 jars
  2. 创建的罐子
  3. shell/windows 脚本来执行它

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

看看这里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-dependencyYou may want to refer here How can I create an executable JAR with dependencies using Maven?

选项 3:
如果您不想要所有的行李而只想拥有一个jar-with-dependency您可能需要参考此处如何使用 Maven 创建具有依赖项的可执行 JAR?

This will contain all the dependent jars within it.

这将包含其中的所有依赖 jar。



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

编辑 1:对于选项 1,Brad M提到您可以使用依赖插件获取所有项目 deps 的列表。dependency:build-classpath

回答by messivanio

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.

您可以在此处找到更多示例:从 Maven 运行 Java main 的 3 种方法