【问题标题】:Whats the purpose of "properties-maven-plugin" and "filter" in a maven profilemaven配置文件中“properties-maven-plugin”和“filter”的目的是什么
【发布时间】:2020-03-19 10:57:19
【问题描述】:

我想解释一下 maven 过滤器标签以及它与插件 properties-maven-plugin 中的文件标签的对应关系。

有问题的个人资料:

<profile>
    <id>local-spm-caefeeder-preview</id>
    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>properties-maven-plugin</artifactId>
                <version>1.0-alpha-1</version>
                <executions>
                    <execution>
                        <phase>initialize</phase>
                        <goals>
                            <goal>read-project-properties</goal>
                        </goals>
                        <configuration>
                        <quiet>true</quiet>
                        <files>
                            <file>${main.basedir}/src/config/global.properties</file>
                            <file>${main.basedir}/src/config/caefeeder/caefeeder_global.properties</file>
                            <file>${main.basedir}/src/config/caefeeder/caefeeder_preview.properties</file>
                            <file>${main.basedir}/src/config/local.properties</file>
                            <file>${main.basedir}/src/config/${user.name}.properties</file>
                        </files>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
        <filters>
            <filter>${main.basedir}/src/config/caefeeder/caefeeder_global.properties</filter>
            <filter>${main.basedir}/src/config/caefeeder/caefeeder_preview.properties</filter>
            <filter>${main.basedir}/src/config/local.properties</filter>
            <filter>${main.basedir}/src/config/${user.name}.properties</filter>
        </filters>
    </build>
</profile>

根据我的研究过滤器定义了其中包含需要替换的变量的文件。 “properties-maven-plugin”是否从 files 标签中定义的文件中提供这些变量?

【问题讨论】:

  • 请注意,properties-maven-plugin 不是官方的 maven 插件,在 IDE 中工作时可能得不到很好的支持。

标签: maven configuration


【解决方案1】:

首先,要让 Maven 过滤器真正起作用,您需要为某些目录启用资源过滤,例如

  <resource>
    <directory>src/main/resources</directory>
    <filtering>true</filtering>
  </resource>

仅列出一些过滤器文件没有任何作用,因为这些读取值不会在任何地方使用。

现在,这里解释不同之处。 properties-maven-plugin 的目标 read-project-properties 从属性文件中读取一些值并将它们存储为 Maven 变量。它在 Maven 生命周期 (&lt;phase&gt;initialize&lt;/phase&gt;) 的早期执行,因此它们几乎从一开始就可用。因此,我们可以说这是一种将 Maven 属性外部化的方法,方法是将它们放入一些常规属性文件中,而不是 pom.xml 中可能存在巨大的 &lt;properties&gt; 块。这里重要的是,Maven 变量用于资源过滤。

现在,&lt;filters&gt; 标签。这些文件不提供 Maven 项目变量。它们提供仅用于资源过滤的变量。

因此,在资源过滤的上下文中,实际行为几乎相同。 properties-maven-plugin 读取的属性将用于资源过滤以及从过滤器文件中读取的属性。但是,properties-maven-plugin 读取的那些也将作为 Maven 项目属性提供,因此可以使用它们,例如在插件的配置或任何其他与 POM 相关的东西中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-12
    • 2012-10-19
    • 2018-09-30
    • 1970-01-01
    • 1970-01-01
    • 2016-10-13
    • 2018-06-16
    相关资源
    最近更新 更多