【问题标题】:Using the properties tag within maven profiles在 Maven 配置文件中使用属性标签
【发布时间】:2012-08-17 14:51:14
【问题描述】:

我指的是“Maven: The Complete Reference”,尤其是关于配置文件的部分,该部分记录了<profile... 标记中<properties... 标记的使用:see here

 <profile>
            <id>development</id>
            <activation>
                <activeByDefault>true</activeByDefault>
                <property>
                    <name>environment.type</name>
                    <value>dev</value>
                </property>
            </activation>
            <properties>
                <database.driverClassName>com.mysql.jdbc.Driver</database.driverClassName>
                <database.url>
                    jdbc:mysql://localhost:3306/app_dev
                </database.url>
                <database.user>development_user</database.user>
                <database.password>development_password</database.password>
            </properties>
        </profile>

我不确定的是当mvn install -Denvironment.type=dev 命令运行时会发生什么:

  • 这会创建一个.properties 文件吗?
  • 如果在 dev 中测试应用程序时,tomcat(例如)将如何以及在何处读取各个属性?

【问题讨论】:

  • 刚刚尝试通过 maven 和 spring 来跟踪这里的链条,这正是我需要的问题和答案!不错!

标签: maven properties maven-profiles


【解决方案1】:

这会创建一个 .properties 文件吗?

不,不会。这将设置 maven 使用的属性。也就是说,mvn install -Denvironment.type=development maven 将使用值 'development_user' 作为变量 'database.user'(您可以在 poms 和过滤资源中用作 ${database.user})。

如果不是,当应用程序在 dev 中测试时,tomcat(例如)将如何以及在哪里读取各个属性?

事情是告诉 maven 根据配置文件(properties.files)过滤(和修改)您想要自定义的资源。

所以,首先你得说 maven 来过滤资源:

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

然后修改您的属性文件以使用 Maven 变量。例如,您的 db 属性文件如下所示:

database.driverClassName=${database.driverClassName}
database.url=${database.url}
#...

【讨论】:

  • 我发现 the docs 在阅读此答案后很有帮助 - 似乎“过滤”基本上意味着 maven 会通过并用它在构建时的值替换 ${somename} 在任何文件中。您可以在构建输出中看到这一点。很好的答案 - 真的填补了我的空白。
猜你喜欢
  • 2018-04-28
  • 1970-01-01
  • 2017-10-11
  • 2013-10-08
  • 1970-01-01
  • 2019-02-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-26
相关资源
最近更新 更多