如何在Maven2模块之间共享过滤器文件? [英] How to share a filter file among Maven2 modules?

查看:125
本文介绍了如何在Maven2模块之间共享过滤器文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

父母
-filter.properties
- module1
    -resource1
- module2
   -resource2

这样,当我构建父模块时,它将在构建子模块时将过滤器应用于resource1和resource2.

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

解决方案

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 的复制依赖关系目标是下载文件:

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

I have a multi-module project and I would like to define a single filter file that will apply properties to all the child modules. For example, I would like to have the following structure:

parent
- filter.properties
- module1
    - resource1
- module2
   - resource2

such that when I build parent, it would apply the filter to resource1 and resource2 when the child modules are built.

If I create the structure above and define the filter in the parent's POM, the build fails because it expects the filter to be defined in each child module... Is there a simple way to do this that I'm overlooking?

解决方案

This answer describes how to extend the properties-maven-plugin to allow properties files to be shared between projects. If you use that plugin the properties would be made available to the build, and therefore can be used when filtering the resources.

Alternatively, you can specify a filter file as an attached artifact on some build, so that it is available in the repository, then use the dependency plugin to download the filter file, finally specify that the filter use the shared file.

To attach the filter to your parent build, use the build-helper-maven-plugin's attach goal:

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

When the project hosting the filter is deployed, the filter will now be attached alongside it.

To download the filter file into your project, use the maven-dependency-plugin's copy-dependencies goal to download the file:

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

If the dependency plugin configuration is defined in your parent project, all other projects can inherit the configuration and won't need to redefine it.

Then to use the downloaded filter:

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

这篇关于如何在Maven2模块之间共享过滤器文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆