gpt4 book ai didi

maven - 如何创建 Maven uber jar,其中包含提供范围的依赖项

转载 作者:行者123 更新时间:2023-12-04 02:10:24 25 4
gpt4 key购买 nike

<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<archive>
<manifest>
<mainClass>com.XXXX.XXXXOfflineApp</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>

我有上面的代码片段来创建一个具有依赖关系的 jar,但是在我的 pom 中我也有一些提供范围的依赖关系但是这些不包含在 uber jar 中,我无法更改这些依赖关系的范围,因为常规 jar 构建不应该包括它们。因为那些是由容器提供的。

最佳答案

预定义的 jar-with-dependencies 描述符将运行时所需的所有依赖项解压缩到生成的 JAR 的根目录中。如果你想添加提供的依赖项,可以在它的基础上构建并添加特定的 <dependencySet> <scope>provided</scope> .

<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd">
<id>jar-with-dependencies</id>
<formats>
<format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
<dependencySet>
<outputDirectory>/</outputDirectory>
<useProjectArtifact>true</useProjectArtifact>
<unpack>true</unpack>
<scope>provided</scope>
</dependencySet>
<dependencySet>
<outputDirectory>/</outputDirectory>
<useProjectArtifact>true</useProjectArtifact>
<unpack>true</unpack>
<scope>runtime</scope>
</dependencySet>
</dependencySets>
</assembly>

这将包括运行时通常需要的所有依赖项(因此具有 compileruntime 的范围)以及具有 provided 的所有依赖项范围。

您将配置此描述符格式的使用:

<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<id>id</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<archive>
<manifest>
<mainClass>com.XXXX.XXXXOfflineApp</mainClass>
</manifest>
</archive>
<descriptors>
<descriptor>path/to/assembly.xml</descriptor>
</descriptors>
</configuration>
</execution>
</executions>
</plugin>

哪里path/to/assembly.xml对应上述描述符格式的路径,相对于POM的位置。

关于maven - 如何创建 Maven uber jar,其中包含提供范围的依赖项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39132906/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com