【问题标题】:How to share a filtered resource at generate-sources phase in a multi module project?如何在多模块项目的生成源阶段共享过滤的资源?
【发布时间】:2016-02-12 09:51:31
【问题描述】:

我有一个包含 3 个子项目的父项目:

parent
   project-1
       /src/main/resources/config.xml
   project-2
       /src/main/resources/config.xml
   project-3
       /src/main/resources/config.xml

config.xml 配置在generate-sources 阶段使用。对于三个项目,config.xml 完全相同。不过这个config.xml的用法对于每个项目是不同的。

在project-X中,我指的是config.xml如下:

<build>
    <plugins>
        <plugin>
            <groupId>some-group</groupId>
            <artifactId>some-artifact</artifactId>
            <executions>
                <execution>
                    <goals>
                        <goal>some-goal</goal>
                    </goals>
                    <configuration>
                        <input>src/main/resources/config.xml</input>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

在所有 3 个项目之间共享这个共同的 config.xml 的最佳方式是什么?

【问题讨论】:

  • 基于意见。答案太多了,每个人都有自己的看法。一种可能的方法是创建第 4 个模块来分解config.xml
  • 作为一种解决方案,我可以建议将您的 config.xml 移动到“父”目录并使用相对路径引用子 pom 中的文件,例如“../configs/config.xml”。
  • 可以通过maven-remote-resources-plugin 引用其他模块,但问题是为什么在不同的模块中需要这样的资源,这意味着它在类路径上,所以定义一个包含它的模块并将适当的依赖项添加到需要它的模块中.. ?
  • @Tunaki 这里不需要第四个模块。见:stackoverflow.com/a/35366446/363573
  • @Tunaki 这让 OP 没有解决方案......

标签: maven maven-3 resource-files


【解决方案1】:

您可以在此处使用build-helper-maven-plugin

项目结构

shared-resources-project
  +-src
     +-main
        +-resources
           `config.xml
  +-project-A
     `pom.xml
  +-project-B
     `pom.xml
  +-project-C
     `pom.xml
  `pom.xml

shared-resources-project/pom.xml

<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>my</groupId>
    <artifactId>shared-resources-project</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <packaging>pom</packaging>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <modules>
        <module>project-A</module>
        <module>project-B</module>
        <module>project-C</module>
    </modules>

    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>build-helper-maven-plugin</artifactId>
                <version>1.10</version>
                <executions>
                    <execution>
                        <id>add-resource</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>add-resource</goal>
                        </goals>
                        <configuration>
                            <resources>
                                <resource>
                                    <filtering>true</filtering>
                                    <directory>${project.parent.basedir}/src/main/resources</directory>
                                    <includes>
                                        <include>config.xml</include>
                                    </includes>
                                </resource>
                            </resources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>some-group</groupId>
                <artifactId>some-artifact</artifactId>
                <executions>
                    <execution>
                        <id>some-plugin-job</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>some-goal</goal>
                        </goals>
                        <configuration>
                            <input>${project.build.outputDirectory}/config.xml</input>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

shared-resources-project/src/main/resources/config.xml

<config>
    <parameter>${custom-value}</parameter>
</config>

project-X/pom.xml

<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>

    <parent>
        <groupId>my</groupId>
        <artifactId>shared-resources-project</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

    <artifactId>project-X</artifactId>

    <properties>
        <custom-value>Project-X Value</custom-value>
    </properties>
</project>

现在,让我们构建项目:

D:\workspaces> cd shared-resources-project
D:\workspaces\java\shared-resources-project> mvn clean install

一些注意事项:

  • build-helper-maven-plugin 会将公共 config.xml 文件作为资源添加到 Project-X。
  • 然后Maven资源插件(MRP)会将config.xml复制到项目输出目录(默认target目录)。在复制过程中,MRP 还会将${custom-value} 替换为 Project-X 提供的具体值。
  • 只要另一个插件绑定到generate-source 阶段并且它的声明出现在build-helper-maven-plugin 声明之后,最终的config.xml 将可用于另一个插件。 Maven(至少 3.0.4+)按照它们在 pom.xml 中出现的顺序调用插件。

【讨论】:

  • 这并不是我想要的。在 project-X 中,使用了一个构建插件,它假定 src/main/resources/config.xml 存在。但是,该插件会抛出 FileNotFoundException。何时将父级的config.xml 复制到项目-X 中,是否可以被其他插件访问?
  • 我更新了我的问题以澄清 config.xml 的用法。
  • @SteveKelio 您想使用${project.build.outputDirectory}/config.xml 而不是src/main/resources/config.xml。在shared-resources-project/pom.xml 中查看我的笔记和我的编辑。请注意,在我的 anwser 中,构建插件在任何项目-X 中都被调用。
  • @SteveKelio 但是,当project-x中提到构建插件时,它不起作用。我认为这是另一个需要解决的问题在一个新的 SO 问题中解决。 maven-resources-plugin:2.6:resources (default-resources) 不是(这很糟糕)。 尝试在构建助手插件之间声明 maven-resources-plugin以及父 pom 中的构建插件。
  • @SteveKelio 您可以创建一个属性build.plugin.config,构建插件使用它来查找config.xml。构建插件将负责过滤配置文件。请参阅 Maven 过滤器 api。
猜你喜欢
  • 1970-01-01
  • 2013-11-03
  • 1970-01-01
  • 2013-07-03
  • 2021-09-24
  • 1970-01-01
  • 2012-09-19
  • 2019-10-07
相关资源
最近更新 更多