【问题标题】:How to share a filter file among Maven2 modules?如何在 Maven2 模块之间共享过滤器文件?
【发布时间】:2010-12-04 10:28:23
【问题描述】:

我有一个多模块项目,我想定义一个过滤器文件,它将属性应用于所有子模块。例如,我想有以下结构:

父母
- filter.properties
- 模块1
- 资源1
- 模块2
- 资源2

这样当我构建父模块时,它会在构建子模块时将过滤器应用于资源 1 和资源 2。

如果我创建上面的结构并在父 POM 中定义过滤器,则构建失败,因为它希望在每个子模块中定义过滤器......有没有一种我忽略的简单方法来做到这一点?

【问题讨论】:

    标签: maven-2


    【解决方案1】:

    This answer 描述了如何扩展 properties-maven-plugin 以允许在项目之间共享属性文件。如果您使用该插件,这些属性将对构建可用,因此可以在过滤资源时使用。

    或者,您可以将过滤器文件指定为某些构建的附加工件,以便它在存储库中可用,然后使用依赖插件下载过滤器文件,最后指定过滤器使用共享文件。

    要将过滤器附加到您的父版本,请使用build-helper-maven-plugin 的附加目标:

      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>build-helper-maven-plugin</artifactId>
        <version>1.3</version>
        <executions>
          <execution>
            <id>attach-artifacts</id>
            <phase>package</phase>
            <goals>
              <goal>attach-artifact</goal>
            </goals>
            <configuration>
              <artifacts>
                <artifact>
                  <file>src/main/resources/shared-filter.properties</file>
                  <type>properties</type>
                  <classifier>filter</classifier>
                </artifact>
              </artifacts>
            </configuration>
          </execution>
        </executions>
      </plugin>
    

    部署托管过滤器的项目后,过滤器现在将附加到它旁边。

    要将过滤器文件下载到您的项目中,请使用maven-dependency-plugin's copy-dependencies 目标下载文件:

      <plugin>
        <artifactId>maven-dependency-plugin</artifactId>
        <executions>
          <execution>
            <id>copy</id>
            <phase>generate-sources</phase>
            <goals>
              <goal>copy</goal>
            </goals>
            <configuration>
              <artifactItems>
                <artifactItem>
                  <groupId>name.seller.rich</groupId>
                  <artifactId>shared</artifactId>
                  <version>1.0.0</version>
                  <classifier>filter</classifier>
                  <type>properties</type>
                  <overWrite>false</overWrite>
                  <destFileName>shared-filter.properties</destFileName>
                </artifactItem>
              </artifactItems>
              <outputDirectory>
                ${project.build.directory}/filters
              </outputDirectory>
            </configuration>
          </execution>
        </executions>
      </plugin>
    

    如果在你的父项目中定义了依赖插件配置,其他所有项目都可以继承该配置,无需重新定义。

    然后使用下载的过滤器:

    <filters>
      <filter>${project.build.directory}/filters/shared-filter.properties</filter>
    </filters>
    <resources>
      <resource>
        <directory>src/main/resources</directory>
        <filtering>true</filtering>
      </resource>
    </resources>
    

    【讨论】:

    • +1 不错的答案 丰富。听说过这个:mail-archive.com/announce@maven.apache.org/msg00202.html
    • 谢谢 Rich,这应该对我有用。虽然不像我希望的那样简单:)
    • 我尝试了上述解决方案,它部分工作。当我尝试执行 mvn install 时,它会复制资源,但最后它也会尝试将它们部署到存储库,并且在此期间它会在子模块的 src/main/resource 中查找资源,我得到以下异常
    • 安装工件时出错:文件/media/.../certificates/src/main/resources/filters/mr.filter.properties 不存在
    • 这是我要采用的方法,尽管我遇到了一个小问题。这个答案建议将 .properties 文件附加到您的父 POM 并在那里配置 maven-dependency-plugin 。这样做的问题是,当父 POM 正在构建时,它将寻找尚未安装的 .properties 工件。一种解决方法是在子模块中配置 maven-dependency-plugin。这是不幸的,因为该配置将被复制。
    【解决方案2】:

    我不知道这是否是一个选项,但除了在外部文件中定义属性之外,您还可以在 pom.xml 的属性部分中定义它们,并且您会得到相同的效果:

    <project>
      ...
      <properties>
        <my.filter.value>hello</my.filter.value>
      </properties>
    </project>
    

    因此,在父 pom 中定义 &lt;properties&gt; 应该可以让您轻松过滤子模块。

    【讨论】:

      【解决方案3】:

      您还可以通过在父 pom.xml 中使用过滤器的绝对路径来共享过滤器

      由于路径取决于环境(eclipse windows / eclipse linux / buildserver),您可以将路径前缀或其一部分放在 settings.xml 配置文件中

      【讨论】:

        猜你喜欢
        • 2011-06-02
        • 2016-04-20
        • 2011-05-07
        • 1970-01-01
        • 1970-01-01
        • 2017-08-22
        • 2017-01-21
        • 2012-02-25
        • 1970-01-01
        相关资源
        最近更新 更多