Maven - 如何自定义依赖项的构建?

时间:2013-09-24 09:53:43

标签: java maven build m2eclipse eclipse-wtp

我有一个像这样的多模块maven项目:

project
  project-core
  project-webapp

project-coreproject-webapp的依赖关系。在构建project-core时,我从最终的jar中排除了一些元素。当我用maven构建和部署整个项目时(通过m2eclipse),一切都很好。

但是,如果我使用WTP构建和部署,则会跳过资源过滤。

到目前为止,我发现的唯一工作是禁用m2eclipse的工作空间分辨率。这样,WTP用maven构建的jar建立了最后的战争。此解决方法的主要缺点是每次修改project-core源代码时都必须安装,更新项目。

如何强制WTP过滤掉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>

修改

  

但是,如果我使用WTP构建和部署,则会跳过资源过滤。

实际上我没有在正确的阶段进行过滤。资源仅在jar构建期间被过滤。感谢@Samuel EUSTACHI,我将资源复制到target文件夹时立即过滤。我是怎么做到的:

项目/项目核/ 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>

1 个答案:

答案 0 :(得分:1)

我的猜测是WTP不执行实际的jar构建,而是使用可用资源(可能在目标中)。

从jar中排除一些文件并不常见。您可以从前面的阶段(process-resources?)中排除这些文件,或者,如果您只需要这些文件进行单元测试,请在测试中移动它们。

这真的取决于你为什么决定将它们从罐子里排除(你必须有充分的理由,但这有助于了解原因)。

相关问题