如何创建包含依赖项和所提供范围的Maven uber jar [英] How to create maven uber jar which includes dependencies with scope provided
问题描述
<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中,所以我无法更改这些依赖项的范围,因为常规jars build不应该包含它们.因为这些是容器提供的.
I have the above code piece to create an jar with dependencies, but in my pom i also have some dependencies with scope provided but these are not included in the uber jar, Well I cannot change the scope of those dependencies because the regular jars build should not include them.Since those are provided by the container.
推荐答案
预定义的 <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>
这将包括运行时通常需要的所有依赖项(因此具有compile
和runtime
的范围)以及所有具有provided
范围的依赖项.
This will include all dependencies normally needed at runtime (so having a scope of compile
and runtime
) and all dependencies having the provided
scope.
您可以通过以下方式配置此描述符格式的使用:
You would configure the use of this descriptor format with:
<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的位置.
where path/to/assembly.xml
corresponds to the path to the above descriptor format, relative to the location of the POM.
这篇关于如何创建包含依赖项和所提供范围的Maven uber jar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!