本文介绍了如何在CLI上运行maven生成的jar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试让maven托管项目在命令行上运行。

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

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

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.

现在问题是我正在尝试运行jar:java - jar项目-SNAPSHOT.jar。

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

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

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.

推荐答案

选项1:

创建的jar没有相关的jar文件。所以,你需要告诉java所有依赖jar的类路径

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

选项2:

更简单,更好的解决方案是使用 AppAssembler 插件。它的作用是将你的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. 依赖的罐子

  2. 创建的jar

  3. 执行它的shell / windows脚本

看看这里

选项3:

如果您不想要所有行李并且只想要一个 jar-with-dependency
你可能想在这里引用

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

This will contain all the dependent jars within it.

编辑1:对于选项1,提到你可以克以及使用。 依赖项:build-classpath

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

这篇关于如何在CLI上运行maven生成的jar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 05:39