Intellij Java 2016& Maven:如何在JAR中嵌入依赖项? [英] Intellij Java 2016 & Maven : how to embed dependencies in JAR?

查看:114
本文介绍了Intellij Java 2016& Maven:如何在JAR中嵌入依赖项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我想添加一个外部库,所以我添加了我的Maven中的依赖关系如下:

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

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

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

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

我检查过,由于某种原因,外部JAR未添加到JAR中我只是产生。我还在文件 - >项目结构中尝试了很多东西,但仍然没有工作......

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,所以我可以使用以下命令在控制台中运行我的应用程序:

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

java -jar myproject.jar

我该怎么做?谢谢你的帮助!

How can I do that? Thanks for your help!

推荐答案

我终于设法用Intellij Java生成这个JAR,这是我的工作方式:

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


  • 在pom.xml文件中添加依赖项

  • 转到文件 - >项目结构 - >工件 - >新建 - > JAR - >从具有依赖关系的模块

  • 选择Main类并单击确定

  • 在项目中,在src / main中,创建资源文件夹

  • 在此资源文件夹中移动META-INF(其中包含MANIFEST.MF)文件夹

  • go构建 - >构建工件以构建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文件中添加以下行:

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

<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>

然后使用clean和packagemaven命令。

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

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

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

这篇关于Intellij Java 2016&amp; Maven:如何在JAR中嵌入依赖项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆