maven - Maven 配置文件中 "properties-maven-plugin"和 "filter"的用途是什么

标签 maven configuration

我想解释一下 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 标签中定义的文件中提供这些变量?

最佳答案

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

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

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

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

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

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

关于maven - Maven 配置文件中 "properties-maven-plugin"和 "filter"的用途是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11307124/

相关文章:

java - 在 Grails 应用程序中使用 Mailjet API 发送邮件

eclipse - 更新到 m2e 1.6 导致 "Conflicting lifecycle mapping"错误

maven - 将 maven nexus repo 添加到我的 pom.xml

maven - 特定环境下的 Liquibase 执行(maven + spring boot)

hibernate - 从 persistence.xml 引用 Maven 依赖项 jar 的artifactId

configuration - 使用Jetty设置HTTP header

configuration - 在TortoiseHG中设置用户名

linux - Linux/Hadoop集群的配置管理

windows - 我怎样才能使窗口 sdk 的 setenv.cmd 全局工作,而不是单个 cmd 窗口?

maven - -dmaven.multiModuleProjectDirectory系统属性未设置。检查$ M2_HOME环境变量和mvn脚本是否匹配