java 如何制作Maven项目的“胖罐子”?

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

How to make a "fat jar" of a Maven project?

javamavenintellij-idea

提问by daniels

Using IntelliJ I just created a new Maven project and added the following to the pom file http://undertow.io/downloads.htmland the following to a Main.java file http://undertow.io/index.html

使用 IntelliJ 我刚刚创建了一个新的 Maven 项目并将以下内容添加到 pom 文件http://undertow.io/downloads.html并将以下内容添加到 Main.java 文件http://undertow.io/index.html

Now if I run the code all works well, but how do I make this as a "fat jar" that will contain all the dependencies in the pom file and that I'll be able to run by just java -jar my.jar? Like you are able to do with a Spring Boot app.

现在,如果我运行代码一切正常,但是我如何将它作为一个“fat jar”来包含 pom 文件中的所有依赖项,并且我只能通过它运行java -jar my.jar?就像您可以使用 Spring Boot 应用程序一样。

回答by Stewart

Maven Shade Plugindoes this well.

Maven Shade Plugin在这方面做得很好。

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-shade-plugin</artifactId>
      <version>2.4.3</version>
      <executions>
        <execution>
          <phase>package</phase>
          <goals>
            <goal>shade</goal>
          </goals>
          <configuration>
            <transformers>
              <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                <mainClass>package.Main</mainClass>
              </transformer>
            </transformers>
          </configuration>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>

回答by K139

1) Add the Spring-boot-maven-pluginto the pom.xml file

1)Spring-boot-maven-plugin在pom.xml文件中添加

 <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>1.4.0.RELEASE</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

2) Change the packing to jar

2) 将包装改为 jar

<packaging>jar</packaging>

3) mvn packagewill create the executable jar.

3)mvn package将创建可执行jar。