使用maven构建胖jar

ogq8wdun  于 2021-08-20  发布在  Java
关注(0)|答案(6)|浏览(367)

我有一个代码库,我想作为jar分发。它还依赖于外部jar,我想将其打包到最终的jar中。
我听说这可以通过使用 maven-assembly-plug-in ,但我不明白怎么做。有人能给我举几个例子吗。
现在,我正在使用fat-jar打包最后一个jar。我想用maven实现同样的目标。

fsi0uk1n

fsi0uk1n1#

注意:如果您是spring引导应用程序,请阅读答案的末尾
将以下插件添加到您的 pom.xml 最新版本可在

...
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>CHOOSE LATEST VERSION HERE</version>
            <configuration>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
            </configuration>
            <executions>
                <execution>
                    <id>assemble-all</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
...

配置此插件后,运行 mvn package 将生成两个jar:一个只包含项目类,另一个fat jar包含后缀为“-jar with dependencies”的所有依赖项。
如果你想要正确的 classpath 在运行时安装,然后还添加以下插件

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <configuration>
        <archive>
            <manifest>
                <addClasspath>true</addClasspath>
                <mainClass>fully.qualified.MainClass</mainClass>
            </manifest>
        </archive>
    </configuration>
</plugin>

对于spring启动应用程序,只需使用以下插件(选择合适的版本)

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <configuration>
        <fork>true</fork>
        <mainClass>${start-class}</mainClass>
    </configuration>
    <executions>
        <execution>
            <goals>
                <goal>repackage</goal>
            </goals>
        </execution>
    </executions>
</plugin>
cgfeq70w

cgfeq70w2#

您可以使用maven shade插件。
在构建中配置shade插件后,请执行以下命令 mvn package 将创建一个jar,并将所有依赖项合并到其中。

siotufzp

siotufzp3#

也许你想要 maven-shade-plugin ,绑定依赖项,最小化未使用的代码并隐藏外部依赖项以避免冲突。

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>3.1.1</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <minimizeJar>true</minimizeJar>
                        <createDependencyReducedPom>true</createDependencyReducedPom>
                        <dependencyReducedPomLocation>
                            ${java.io.tmpdir}/dependency-reduced-pom.xml
                        </dependencyReducedPomLocation>
                        <relocations>
                            <relocation>
                                <pattern>com.acme.coyote</pattern>
                                <shadedPattern>hidden.coyote</shadedPattern>
                            </relocation>
                        </relocations>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

参考资料:
http://maven.apache.org/plugins/maven-shade-plugin/plugin-info.html
http://maven.apache.org/plugins/maven-shade-plugin/shade-mojo.html

dly7yett

dly7yett4#

实际上,添加

<archive>
   <manifest>
    <addClasspath>true</addClasspath>
    <packageName>com.some.pkg</packageName>                     
    <mainClass>com.MainClass</mainClass>
  </manifest>
</archive>

对maven jar插件的声明不会为我将主类条目添加到清单文件中。我不得不将它添加到maven程序集插件中,以便在清单中获得它

anauzrmj

anauzrmj5#

您可以使用onejar maven插件进行打包。基本上,它将您的项目及其依赖项组装为一个jar,不仅包括您的项目jar文件,还包括所有外部依赖项作为“jar的jar”,例如。

<build>
    <plugins>
        <plugin>
            <groupId>com.jolira</groupId>
            <artifactId>onejar-maven-plugin</artifactId>
                <version>1.4.4</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>one-jar</goal>
                        </goals>
                    </execution>
                </executions>
        </plugin>
    </plugins>
</build>

注1:项目主页上提供了配置选项。
注2:出于这样或那样的原因,onejar maven插件项目没有在maven central发布。然而,jolira.com跟踪原始项目并使用groupid将其发布到 com.jolira .

yqlxgs2m

yqlxgs2m6#

另一种方法是使用maven shade插件构建 uber-jar .

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version> Your Version Here </version>
    <configuration>
            <!-- put your configurations here -->
    </configuration>
    <executions>
            <execution>
                    <phase>package</phase>
                    <goals>
                            <goal>shade</goal>
                    </goals>
            </execution>
    </executions>
</plugin>

相关问题