从 jar-with-dependencies 构建的 jar 中排除文件
Exclude file from jar as built by jar-with-dependencies
我的 pom.xml
包含以下内容以创建我的项目的 jar 和所有依赖项。
现在我在 src/main/resources
中有一个属性文件,它是 运行 应用程序所必需的(我想从 IDE 开始使用它),但我不想将它发送到创建的 jar 文件,因为设置是单独维护的。
问题:我怎样才能得到一个包含所有依赖项的文件,但排除那些属性文件?
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<id>make-jar</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<archive>
<manifest>
<mainClass>x.Main</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</execution>
</executions>
</plugin>
如 maven-assembly-plugin
文档所述:
If your project wants to package your artifact in an uber-jar, the assembly plugin provides only basic support. For more control, use the Maven Shade Plugin.
使用maven-shade-plugin
你可以有一个fat jar(就像使用assembly插件一样)并通过配置解决类似的排除文件的问题(不需要外部汇编描述符文件)。
在您的情况下,要从组装的 jar 中排除资源,您可以使用 shade filters。
一个简单的配置如下:
<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>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>**/*file_pattern*</exclude>
</excludes>
</filter>
</filters>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
在上面的示例中,您可以使用 my.groupId:my.artifactId
.
自定义 file_pattern
或缩小 artifact
元素中的过滤器
注意:当从外部库中排除文件时,建议使用上述方法,但是您仍然可以使用 maven-assembly-plugin
通过自定义 assembly descriptor 文件从您自己的项目中排除文件。
如果您想坚持使用 maven-assembly-plugin,您可以使用一个程序集描述符文件来配置您的过滤器,如本节所示:
<fileSets>
<fileSet>
<directory>${basedir}</directory>
<includes>
<include>*.txt</include>
</includes>
<excludes>
<exclude>*.properties/exclude>
</excludes>
</fileSet>
并且在您的 Maven 配置中,pom.xml 您在 descriptors 标记中指定了 assemblu 描述符文件(distribution.xml 是一个包含来自以上)
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.6</version>
<configuration>
<filters>
<filter>src/assembly/filter.properties</filter>
</filters>
<descriptors>
<descriptor>src/assembly/distribution.xml</descriptor>
</descriptors>
</configuration>
</plugin>
还要检查这个 link maven-assembly
我遇到了类似的问题(我有一个 checkstyle-config.xml,不应包含在最终组装中)。以下解决方案对我有用:
添加 resources 部分使我的 project-jar-with-dependancies.jar 不包含 checkstyle-config.xml 文件
简化 pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>...</groupId>
<artifactId>...</artifactId>
<version>...</version>
<properties>
...
</properties>
<packaging>jar</packaging>
<build>
<resources>
<resource>
<directory>${project.basedir}/src/main/resources</directory>
<excludes>
<exclude>checkstyle-config.xml</exclude>
</excludes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.3.0</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>...</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
...
</dependencies>
</project>
我的 pom.xml
包含以下内容以创建我的项目的 jar 和所有依赖项。
现在我在 src/main/resources
中有一个属性文件,它是 运行 应用程序所必需的(我想从 IDE 开始使用它),但我不想将它发送到创建的 jar 文件,因为设置是单独维护的。
问题:我怎样才能得到一个包含所有依赖项的文件,但排除那些属性文件?
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<id>make-jar</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<archive>
<manifest>
<mainClass>x.Main</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</execution>
</executions>
</plugin>
如 maven-assembly-plugin
文档所述:
If your project wants to package your artifact in an uber-jar, the assembly plugin provides only basic support. For more control, use the Maven Shade Plugin.
使用maven-shade-plugin
你可以有一个fat jar(就像使用assembly插件一样)并通过配置解决类似的排除文件的问题(不需要外部汇编描述符文件)。
在您的情况下,要从组装的 jar 中排除资源,您可以使用 shade filters。
一个简单的配置如下:
<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>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>**/*file_pattern*</exclude>
</excludes>
</filter>
</filters>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
在上面的示例中,您可以使用 my.groupId:my.artifactId
.
file_pattern
或缩小 artifact
元素中的过滤器
注意:当从外部库中排除文件时,建议使用上述方法,但是您仍然可以使用 maven-assembly-plugin
通过自定义 assembly descriptor 文件从您自己的项目中排除文件。
如果您想坚持使用 maven-assembly-plugin,您可以使用一个程序集描述符文件来配置您的过滤器,如本节所示:
<fileSets>
<fileSet>
<directory>${basedir}</directory>
<includes>
<include>*.txt</include>
</includes>
<excludes>
<exclude>*.properties/exclude>
</excludes>
</fileSet>
并且在您的 Maven 配置中,pom.xml 您在 descriptors 标记中指定了 assemblu 描述符文件(distribution.xml 是一个包含来自以上)
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.6</version>
<configuration>
<filters>
<filter>src/assembly/filter.properties</filter>
</filters>
<descriptors>
<descriptor>src/assembly/distribution.xml</descriptor>
</descriptors>
</configuration>
</plugin>
还要检查这个 link maven-assembly
我遇到了类似的问题(我有一个 checkstyle-config.xml,不应包含在最终组装中)。以下解决方案对我有用:
添加 resources 部分使我的 project-jar-with-dependancies.jar 不包含 checkstyle-config.xml 文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>...</groupId>
<artifactId>...</artifactId>
<version>...</version>
<properties>
...
</properties>
<packaging>jar</packaging>
<build>
<resources>
<resource>
<directory>${project.basedir}/src/main/resources</directory>
<excludes>
<exclude>checkstyle-config.xml</exclude>
</excludes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.3.0</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>...</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
...
</dependencies>
</project>