从外部配置文件获取 Maven 属性

Get maven properties from external configuration file

我用这个属性插入了 wildfly 以在外部配置主机名、用户等。

<plugin>
    <groupId>org.wildfly.plugins</groupId>
    <artifactId>wildfly-maven-plugin</artifactId>
    <version>1.0.0.Final</version>
    <configuration>
        <hostname>${wildfly.hostname}</hostname>
        <port>${wildfly.port}</port>
        <username>${wildfly.username}</username>
        <password>${wildfly.password}</password>
    </configuration>
    <executions>
        <execution>
            <!-- <phase>package</phase> -->
            <!-- <goals> -->
            <!-- <goal>deploy</goal> -->
            <!-- </goals> -->
        </execution>
    </executions>
</plugin>

然后我创建了一个具有属性 (file.properties)

的文件
wildfly.hostname=127.0.0.1
wildfly.port=19990
wildfly.username=user
wildfly.password=pass

我希望在调用 mvn wildfly:deploy 时从文件中获取变量。我尝试了几种方法:

  1. 使用 <properties> 并且有效,但它在 pom.xml
  2. 使用properties-maven-plugin加载外部文件使用<phase>pre-clean</phase>但它不起作用。

这里的问题是,最后我想创建一个具有不同配置文件的配置文件夹,即不同的用户、密码等。

知道怎么做吗?

不要将 properties-maven-plugin 绑定到 pre-clean 阶段。将它绑定到 initialize 阶段会更合适。来自 lifecycle reference:

initialize: initialize build state, e.g. set properties or create directories.

但是,问题是当您直接调用 mvn wildfly:deploy 时,此阶段不会 运行。通过这样做,您告诉 Maven 仅 运行 执行 wildfly-maven-plugin。因此,它不会执行默认或干净生命周期的任何内置阶段。

在您的情况下,您实际上需要设置这些属性,因此您需要 initialize 阶段到 运行。为此,您可以使用 mvn initialize wildfly:deploy。在这里使用 initialize 的好处是默认生命周期的其余阶段不会 运行 所以它会和以前一样快。

在 Maven 中,<filters> 有不同的用途:它们用于通过将占位符替换为来自属性文件的特定值来修改资源的内容。所以 <filters> 不适合这个用例,properties-maven-plugin 非常适合它。