【问题标题】:Using property file in maven在maven中使用属性文件
【发布时间】:2013-05-10 07:06:29
【问题描述】:

我不太明白如何使用它。文件中定义了一个属性。我尝试使用 maven 属性插件来读取并保存。该属性在 liquibase 插件中使用:

<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>
       <files>
          <file>src/main/resources/properties/app.properties</file>
        </files>
      </configuration>
    </execution>
  </executions>
</plugin>
<plugin>
    <groupId>org.liquibase</groupId>
    <artifactId>liquibase-maven-plugin</artifactId>
    <version>2.0.5</version>
    <configuration>
        <propertyFile>src/main/resources/db/config/${env}-data-access.properties</propertyFile>
        <changeLogFile>src/main/resources/db/changelog/db.changelog-master.xml</changeLogFile>
        <migrationSqlOutputFile>src/main/resources/db/gen/migrate.sql</migrationSqlOutputFile>
        <!--<logging>debug</logging>-->
        <logging>info</logging>
        <promptOnNonLocalDatabase>false</promptOnNonLocalDatabase>
        <!--<verbose>false</verbose>-->
        <dropFirst>true</dropFirst>
    </configuration>
</plugin>
  1. 根据文档,为了读取属性并保存它,我必须运行:mvn properties:read-project-properties。但在这种情况下,我收到以下错误:

    [错误] 无法在项目 SpringWebFlow 上执行目标 org.codehaus.mojo:properties-maven-plugin:1.0-alpha-2:read-project-properties (default-cli): 目标 org.codehaus.mojo:properties-maven-plugin:1.0-alpha-2:read-project-properties 的参数“文件”丢失或无效 -> [帮助 1]

我更改了 pom.xml,删除了 &lt;execution&gt; 部分并移动了 &lt;configuration&gt; 部分:

<groupId>org.codehaus.mojo</groupId>
  <artifactId>properties-maven-plugin</artifactId>
  <version>1.0-alpha-1</version>
  <configuration>
    <files>
        <file>src/main/resources/properties/app.properties</file>
    </files>
  </configuration>

好的。现在,当我运行 mvn properties:read-project-properties 时,错误消失了。但是在这种情况下,属性保存在哪里?原因当我开始以下 maven 目标时:

mvn liquibase:update

我可以看到 ${env} 属性没有定义。 Liquibase 尝试使用 src/main/resources/db/config/${env}-data-access.properties 文件。

我做错了什么?如何从文件中读取属性,以便可以从不同的 maven 插件访问?

【问题讨论】:

    标签: java maven properties


    【解决方案1】:

    问题在于“mvn liquibase:update”是一个特殊的插件目标,不是 maven 生命周期的一部分。所以它永远不会通过初始化阶段,因此不会执行属性插件。

    以下将起作用

    mvn initialize liquibase:update
    

    一种解决方案是调用 liquibase:update 在 maven lifecylce 阶段之一,如编译、打包...,但随后它将在每次构建时执行。

    或者你使用 maven-exec 插件从 maven 调用“initialize liquibase:update”。或者您创建一个配置文件,如果您将 liquibase:update 绑定到生命周期初始化阶段,并且在您调用

    时执行 udate
    mvn initialize -Pliquibase
    

    我不知道这个问题的更好解决方案,也找不到合适的解决方案。

    供参考: Maven lifecycle

    【讨论】:

    • 感谢您的回答。难道你不知道,如果没有定义属性,有没有办法在 maven 中抛出异常?用户可能会运行 mvn liquibase:update 并且我想停止构建过程并显示问题所在。否则可能需要很长时间才能解决问题。
    • 我知道maven-enforcer-plugin,但这应该和上面有同样的问题。您将其绑定到 maven 的生命周期阶段,而不是 liquibase 插件的特定目标。所以我唯一的想法是通过 maven-antrun-plugin 调用 liquibase 并使用 ant "" 或创建一个您的用户的包装脚本用于数据库迁移,不允许他们直接调用 mvn liquibase:*.
    猜你喜欢
    • 1970-01-01
    • 2018-04-28
    • 1970-01-01
    • 1970-01-01
    • 2012-08-17
    • 2013-01-20
    • 1970-01-01
    • 2012-05-30
    • 1970-01-01
    相关资源
    最近更新 更多