gpt4 book ai didi

java - Maven:插件部分中使用的基于配置文件的属性

转载 作者:行者123 更新时间:2023-11-30 08:18:38 25 4
gpt4 key购买 nike

我想提一下,我在 Maven 配置方面相对较新。

我的情况:

  • 我使用 Maven 3.0.5 构建 J2E 应用程序
  • 应用程序部署在四种不同的环境中:本地、开发、测试和生产
  • 我使用 Maven 配置文件来配置特定于环境的配置
  • 我已在 properties 中定义了这些配置文件系统中的文件。

这是用于以下内容的文件系统:

<my-project-root>
---profiles
------local
---------app.properties
------dev
---------app.properties
------test
---------app.properties

我在我的 pom.xml 中使用以下逻辑加载相应的属性文件:

<profiles>
<profile>
<id>local</id>
<!-- The development profile is active by default -->
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<build.profile.id>local</build.profile.id>
</properties>
</profile>
<profile>
<id>dev</id>
<properties>
<build.profile.id>dev</build.profile.id>
</properties>
</profile>
<profile>
<id>prod</id>
<properties>
<build.profile.id>prod</build.profile.id>
</properties>
</profile>
<profile>
<id>test</id>
<properties>
<build.profile.id>test</build.profile.id>
</properties>
</profile>
</profiles>
<build>
<finalName>MyProject</finalName>
<plugins>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
<resource>
<directory>profiles/${build.profile.id}</directory>
</resource>
</resources>
</build>

通过此配置,我几乎可以在任何地方使用当前配置文件的相应属性。到处都是,但<plugins>部分。我非常想从此类属性文件中加载例如我的数据库 URL 或凭据,但如果我将它们包含在 app.properties 中它们不会在插件部分中进行评估(例如,我将 ${endpoint} 的值作为数据库端点)。

如何获取从 <plugins> 中可访问的配置文件的文件加载的属性部分?

PS:是的,如果我直接在pom.xml中添加这些属性作为 <profiles> 下的属性标签,它们是可以访问的,但我宁愿把我的密码放在 pom 之外。

最佳答案

我能够做我想做的事。我使用了从 this answer 链接的 properties-maven-plugin .

我所做的如下:

  • 我添加了properties-maven-plugin来读取我需要加载的文件

    <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>properties-maven-plugin</artifactId>
    <version>1.0-alpha-2</version>
    <executions>
    <execution>
    <phase>initialize</phase>
    <goals>
    <goal>read-project-properties</goal>
    </goals>
    <configuration>
    <files>
    <file>profiles/${build.profile.id}/app.properties</file>
    </files>
    </configuration>
    </execution>
    </executions>
    </plugin>

    遗憾的是,在这里我无法让插件读取目录中的所有属性文件,但我发现这已经足够好了。

  • 我还需要删除上面的插件定义在 Eclipse 中给我带来的错误(生命周期配置未涵盖插件执行)。为此,我按照 following post 中的说明进行操作。 .

通过这些步骤,我需要的属性对于使用它们的插件来说是可用的。

注意:实际上属性是在 compile maven 命令之后加载的,但这对我来说已经足够了,因为我所有与属性相关的目标都将在 compile 之后执行> 在我的所有情况下,目标按目标调用顺序排列。

关于java - Maven:插件部分中使用的基于配置文件的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29305926/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com