gpt4 book ai didi

maven - 在 Maven 配置文件中使用属性标签

转载 作者:行者123 更新时间:2023-12-03 23:24:46 26 4
gpt4 key购买 nike

我指的是“ Maven:完整引用”,尤其是关于记录使用<properties... 的配置文件的部分<profile... 内的标签在这里标记: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文件?
  • 如果不是在开发中测试应用程序时,tomcat(例如)将如何以及在哪里读取单个属性?
  • 最佳答案

    Will this create a .properties file?



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

    If not how and where will tomcat (for instance) read the individual properties when the app is tested in dev?



    事情是告诉 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}
    #...

    关于maven - 在 Maven 配置文件中使用属性标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12068758/

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