在 Maven 中如何从生成的 jar 中排除资源?

当我创建一个具有依赖项的可执行 jar (使用 这本指南)时,所有属性文件也都打包到这个 jar 中。如何阻止这一切发生?谢谢。

更新: 我试图使用 Maven 资源插件排除它们,但是当我在 Eclipse 中运行它时,我的应用程序找不到属性文件(右键单击模块-> Run As-> Java Application)

更新: 谢谢你有用的答案。我想我最好花时间学习 Maven,因为现在我只选择最简单的解决方案。

142843 次浏览

您是指位于 src/main/resources中的属性文件吗?然后您应该使用 maven-resource-plugin 排除它们。详情请参阅以下网页:

Http://maven.apache.org/plugins/maven-resources-plugin/examples/include-exclude.html

按照约定,目录 src/main/resources包含应用程序将使用的资源。所以 Maven 会把它们包含在最终的 JAR中。

因此,在您的应用程序中,您将使用 getResourceAsStream()方法访问它们,因为资源将装载到类路径中。

如果您需要在应用程序之外使用它们,不要将它们存储在 src/main/resources中,因为它们将由 Maven 绑定。当然,您可以排除它们(使用 Chkal提供的链接) ,但是最好创建另一个目录(例如 src/main/external-resources) ,以保持关于 src/main/resources目录的约定。

在后一种情况下,您必须将资源作为 JAR 文件独立地交付(这可以通过使用 Assembly 插件来实现)。如果需要在 Eclipse 环境中访问它们,请转到项目的 Properties,然后在 SourcesJava Build Path选项卡中添加文件夹(例如 src/main/external-resources)。然后,Eclipse 将在类路径中添加此目录。

将这些属性文件放入 src/test/resources。在 Eclipse 中,src/test/resources中的文件可以通过 eclipse:eclipse自动获得,但 Maven 不会将其包含在打包的 JAR 中。

当我创建一个具有依赖项的可执行 jar (使用本指南)时,所有属性文件也都打包到该 jar 中。如何阻止这一切发生?谢谢。

属性文件来自哪里? 您的主 jar? 依赖项?

在前一种情况下,按照建议将资源置于 src/test/resources下可能是最直接和最简单的选择。

在后一种情况下,您必须在 unpackOptions中创建一个具有特殊 excludes/exclude的自定义程序集描述符。

要从 jar/target 目录中排除任何文件,可以在 pom.xml 文件中使用 <excludes>标记。

在下一个示例中,不包括所有扩展名为 .properties的文件:

<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<excludes>
<exclude>*.properties</exclude>
</excludes>
<filtering>false</filtering>
</resource>
</resources>
</build>

这正好需要使用 Maven JAR Plugin 插件

例如,如果要从最后一个 jar 中排除 src/test/resources/下的所有内容,可以这样做:

<build>


<plugins>
<!-- configure JAR build -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.1</version>
<configuration>
<excludes>
<exclude>src/test/resources/**</exclude>
</excludes>
</configuration>
</plugin>


...

src/test/resources/下的文件在类路径上仍然可用,只是不会产生 JAR。

另一种可能性是使用 Maven Shade 插件,例如,排除只在 IDE 本地使用的日志记录属性文件:

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>${maven-shade-plugin-version}</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>log4j2.xml</exclude>
</excludes>
</filter>
</filters>
</configuration>
</execution>
</executions>
</plugin>

然而,这将从每个工件中排除文件,因此它可能不适用于所有情况。

在使用 maven-jar-plugin 创建 maven jar 期间排除特定的文件模式。

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3</version>
<configuration>
<excludes>
<exclude>**/*.properties</exclude>
<exclude>**/*.xml</exclude>
<exclude>**/*.exe</exclude>
<exclude>**/*.java</exclude>
<exclude>**/*.xls</exclude>
</excludes>
</configuration>
</plugin>

这是从 另一个解决办法排除资源文件夹中的所有文件,最后的配置是这样的:

<build>
<!-- exclude all files in resources-->
<resources>
<resource>
<directory>src/main/resources</directory>
<excludes>
<exclude>**/**</exclude>
</excludes>
<filtering>false</filtering>
</resource>
</resources>


<!-- other configurations/plugins in the pom.xml-->
</build>

或者我们可以 用途包括只打包一些文件或文件夹。 但这种方法有副作用。IDE 还将排除 target/classes文件夹中的资源文件。Maven-jar-plugin只影响 jar 文件。

我发现一个更好的解决方案,执行资源文件夹使用 Maven-jar-plugin,这里我们使用 包括:

<build>


<plugins>
<!-- configure JAR build -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.1</version>
<configuration>
<includes>
<include>**/*.class</include>
</includes>
</configuration>
</plugin>


...