我有一个像这样的多模块 Maven 项目:
project
project-core
project-webapp
project-core
是 project-webapp
的依赖项。在构建 project-core
时,我从最终的 jar 中排除了一些元素。当我使用 Maven(通过 m2eclipse)构建和部署整个项目时,一切都很好。
但是,如果我使用 WTP 构建和部署,则会跳过资源过滤。
到目前为止,我发现的唯一解决方法是禁用 m2eclipse 的工作区分辨率。这样,WTP就与maven构建的jar进行了最后的 war 。此解决方法的主要缺点是每次修改 project-core
源代码时都必须安装、更新项目。
如何强制 WTP 过滤掉 project-core
中的资源?
project/project-core/pom.xml
<?xml version="1.0"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>project</artifactId>
<groupId>com.company.project</groupId>
<version>1.0.0</version>
</parent>
<artifactId>project-core</artifactId>
<name>project-core</name>
<version>${version.project}</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
// (huge) dependencies list ...
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.1</version>
<configuration>
<excludes>
<exclude>file.properties</exclude>
<exclude>DIR/file.xml</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
<repositories>
// repositories ...
</repositories>
</project>
编辑:
However, if I build and deploy with WTP, the resource filtering is skipped.
实际上我没有在正确的阶段进行过滤。仅在 jar 构建过程中过滤资源。感谢@Samuel EUSTACHI,我现在在将资源复制到 target
文件夹时过滤资源。这是我的做法:
project/project-core/pom.xml
<build>
<!--
I removed completely the plugins node and
replace it with the resources node
-->
<resources>
<resource>
<directory>src/main/resources</directory>
<excludes>
<exclude>file.properties</exclude>
<exclude>DIR/file.xml</exclude>
</excludes>
</resource>
</resources>
</build>
最佳答案
我的猜测是,WTP 不会执行实际的 jar 构建,而是使用可用资源(可能在目标中)。
从 jar 中排除某些文件的情况并不常见。您可以从前面的阶段(流程资源?)中排除这些文件,或者,如果您只需要这些文件进行单元测试,请将它们移至测试中。
这实际上取决于您决定将它们排除在 jar 之外的原因(您必须有充分的理由,但了解原因会有所帮助)。
关于java - Maven - 如何自定义依赖项的构建?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18978288/