java Intellij Maven:使用所有库依赖项创建 jar

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

Intellij Maven: Create jar with all library dependencies

javaintellij-ideamaven-plugin

提问by D. Müller

Working with IntelliJ Idea, I want to implement the following:

使用 IntelliJ Idea,我想实现以下内容:

I want to export an application to a jar, which contains all needed libraries. E.g. I need the AWS Java SDK libraries for S3 access, but if I upload the jar to the server and run the jar I get an NoClassDefFoundError, see below:

我想将应用程序导出到 jar,其中包含所有需要的库。例如,我需要 AWS Java SDK 库来访问 S3,但是如果我将 jar 上传到服务器并运行该 jar,我会收到 NoClassDefFoundError,请参见下文:

Exception in thread "main" java.lang.NoClassDefFoundError: com/amazonaws/auth/AWSCredentials
    at java.lang.Class.getDeclaredMethods0(Native Method)
    at java.lang.Class.privateGetDeclaredMethods(Class.java:2625)
    at java.lang.Class.getMethod0(Class.java:2866)
    at java.lang.Class.getMethod(Class.java:1676)
    at sun.launcher.LauncherHelper.getMainMethod(LauncherHelper.java:494)
    at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:486)
Caused by: java.lang.ClassNotFoundException: com.amazonaws.auth.AWSCredentials
    at java.net.URLClassLoader.run(URLClassLoader.java:366)
    at java.net.URLClassLoader.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
    ... 6 more

Comparison: I created the same project in Eclipse and get no error! The jar file sizes are very different (Eclipse: ~35 MB vs. IntelliJ Idea: ~5,5 MB)!

比较:我在 Eclipse 中创建了相同的项目并且没有出错!jar 文件大小非常不同(Eclipse:~35 MB 与 IntelliJ Idea:~5,5 MB)!

I included the libraries via Maven and downloaded them also into the "lib" folder of my project: enter image description here

我通过 Maven 包含了这些库,并将它们也下载到了我的项目的“lib”文件夹中: 在此处输入图片说明

enter image description here

在此处输入图片说明

As parameter in the run configurations I set "package", see screenshot below: enter image description here

作为运行配置中的参数,我设置了“包”,请参见下面的屏幕截图: 在此处输入图片说明

SOLUTION:

解决方案:

Thanks for all your hints, I got it to work now! The trick was that I did't add the dependencies to the pom.xml file (because I thought that this would be done automatically after setting them in the Project Structure, but it didn't)!!! See also my other question: Intellij IDEA Maven Plugin - Manage Dependenciesand https://www.jetbrains.com/idea/help/maven.html! Add the dependencies by

感谢您的所有提示,我现在可以使用了!诀窍是我没有将依赖项添加到 pom.xml 文件中(因为我认为这会在项目结构中设置它们后自动完成,但事实并非如此)!!!另请参阅我的另一个问题:Intellij IDEA Maven 插件 - 管理依赖项https://www.jetbrains.com/idea/help/maven.html!添加依赖项

  1. Open pom.xml
  2. Menu "Code" > "Generate" > "Dependency" (or shortcut ALT + INSERT)
  1. 打开 pom.xml
  2. 菜单“代码”>“生成”>“依赖项”(或快捷键 ALT + INSERT)

回答by Héctor

You need to use Maven Assembly Plugin to include dependencies:

您需要使用 Maven Assembly Plugin 来包含依赖项:

<build>
  <plugins>
    <plugin>
      <artifactId>maven-assembly-plugin</artifactId>
      <configuration>
        <archive>
          <manifest>
            <mainClass>your.package.MainClass</mainClass>
          </manifest>
        </archive>
        <descriptorRefs>
          <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
      </configuration>
    </plugin>
  </plugins>
</build>

and then: mvn clean compile assembly:single

接着: mvn clean compile assembly:single

回答by emanciperingsivraren

I recommend that you use the plugin shade. It is better than Assemblybecause it is possible to relocate classesif a conflict occur.

我建议您使用插件shade。这比Assembly因为发生冲突时可以重新定位类要好。

A typical setup can look like this: (copied from the official documentation)

典型的设置可能如下所示:(从官方文档中复制)

<project>
  ...
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>2.4.2</version>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>shade</goal>
            </goals>
            <configuration>
              <artifactSet>
                <excludes>
                  <exclude>classworlds:classworlds</exclude>
                  <exclude>junit:junit</exclude>
                  <exclude>jmock:*</exclude>
                  <exclude>*:xml-apis</exclude>
                  <exclude>org.apache.maven:lib:tests</exclude>
                  <exclude>log4j:log4j:jar:</exclude>
                </excludes>
              </artifactSet>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
  ...
</project>

If size is important you can try to use the <minimizeJar>true</minimizeJar>to reduce size.

如果尺寸很重要,您可以尝试使用<minimizeJar>true</minimizeJar>来减小尺寸。