如何制作Maven项目的"胖罐"?

dan*_*els 7 java intellij-idea maven

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

现在,如果我运行代码一切正常,但我如何将其作为一个"胖罐",将包含pom文件中的所有依赖项,我将能够只运行java -jar my.jar?就像你可以使用Spring Boot应用程序一样.

Ste*_*art 18

Maven Shade插件做得很好.

<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>
Run Code Online (Sandbox Code Playgroud)


K13*_*139 6

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>
Run Code Online (Sandbox Code Playgroud)

2)将包装更换为罐子

<packaging>jar</packaging>
Run Code Online (Sandbox Code Playgroud)

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

  • 我喜欢这个解决方案,但我认为您应该考虑编辑它以从插件中删除版本并让pom指定一个指向spring-boot-start-parent的父级. (2认同)