Intellij Java 2016 & Maven:如何在 JAR 中嵌入依赖项?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39273877/
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-11-03 04:14:39  来源:igfitidea点击:

Intellij Java 2016 & Maven : how to embed dependencies in JAR?

javamavenintellij-ideajardependencies

提问by matteoh

I'm using Intellij Java 2016.2.2 and Maven to create a very simple Java console application.

我正在使用 Intellij Java 2016.2.2 和 Maven 创建一个非常简单的 Java 控制台应用程序。

I want to add an external library, so I add my dependency in Maven like this:

我想添加一个外部库,所以我在 Maven 中添加我的依赖项,如下所示:

<dependency>
    <groupId>jline</groupId>
    <artifactId>jline</artifactId>
    <version>2.12</version>
</dependency>

It works fine when I run it in the IDE, but not in an external console (I have the following error: java.lang.NoClassDefFoundError).

当我在 IDE 中运行它时,它运行良好,但在外部控制台中运行不正常(我有以下错误:java.lang.NoClassDefFoundError)。

I checked and for some reason, the external JAR is not added in the JAR I just generated. I also tried many things in "File -> Project Structure", but still not working...

我查了一下,由于某种原因,我刚刚生成的 JAR 中没有添加外部 JAR。我也在“文件 - > 项目结构”中尝试了很多东西,但仍然无法正常工作......

I just want to build my JAR with my dependencies in it, so I can simply run my application in a console using:

我只想用我的依赖项构建我的 JAR,所以我可以使用以下命令在控制台中简单地运行我的应用程序:

java -jar myproject.jar

How can I do that? Thanks for your help!

我怎样才能做到这一点?谢谢你的帮助!

回答by matteoh

I finally managed to generate this JAR with Intellij Java, here is how I do:

我终于设法用 Intellij Java 生成了这个 JAR,这是我的做法:

  • add the dependencies in the pom.xml file
  • go to File -> Project Structure -> Artifacts -> New -> JAR -> From module with dependencies
  • choose the Main class and click OK
  • in your project, in src/main, create the "resources" folder
  • move the "META-INF" (with MANIFEST.MF in it) folder in this "resources" folder
  • go to Build -> build artifacts to build the JAR
  • 在 pom.xml 文件中添加依赖项
  • 转到文件 -> 项目结构 -> 工件 -> 新建 -> JAR -> 来自具有依赖项的模块
  • 选择 Main 类并单击 OK
  • 在您的项目中,在 src/main 中,创建“资源”文件夹
  • 在这个“资源”文件夹中移动“META-INF”(其中包含 MANIFEST.MF)文件夹
  • 转到 Build -> build artifacts 以构建 JAR

EDIT

编辑

A better (and easier way) to do it is adding the following lines in the pom.xml file :

一个更好(更简单)的方法是在 pom.xml 文件中添加以下几行:

<build>
    <plugins>
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <configuration>
                <archive>
                    <manifest>
                        <mainClass>your.MainClass</mainClass>
                    </manifest>
                </archive>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
            </configuration>
            <executions>
                <execution>
                    <id>make-assembly</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

then use the "clean" and "package" maven commands.

然后使用“clean”和“package”maven命令。

The last 3 steps above (about MANIFEST.MF) still seem to be mandatory.

上面的最后 3 个步骤(关于 MANIFEST.MF)似乎仍然是强制性的。

回答by Fabien Benoit-Koch

Okay, so you basically want to create a "fat jar" (sometimes called assembly), that contains all its own dependencies (usually, the dependencies are external).

好的,所以您基本上想要创建一个“fat jar”(有时称为程序集),其中包含它自己的所有依赖项(通常,依赖项是外部的)。

You need to use a Maven plugin for that. Below is a sample assembly plugin configuration jar-with-dependencies:

您需要为此使用 Maven 插件。下面是一个示例程序集插件配置jar-with-dependencies

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

then, simply run

然后,只需运行

mvn package