不同配置文件的maven,xml文件

Vic*_*rin 6 profiles maven

我有2个配置文件的maven pom:dev和production

我的项目中有一些xml文件.例如persistence.xml.开发和生产环境的设置不同

我需要一种在开发和生产程序集中拥有正确文件的方法

也许可以拥有每个xml文件的2个副本并将其放入程序集中?或者也许可以在xml文件中使用pom文件中的设置?

还有其他想法或最佳做法吗?

Oli*_*ger 11

您正在寻找的内容已在此处得到解答:Maven:包含基于个人资料的资源文件

而不是有两个文件,另一个解决方案是直接在properties.xml中使用属性:

    <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>
    <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
    <property name="hibernate.connection.username" value="${db.username}"/>
    <property name="hibernate.connection.password" value="${db.password}"/>
    <property name="hibernate.connection.url" value="${db.connectionURL}/database"/>
Run Code Online (Sandbox Code Playgroud)

在pom.xml中,为每个环境定义每个属性的值:

<profile>
  <id>development</id>
  <properties>
    <db.username>dev</db.username>
    <db.password>dev_password</db.password>
    <db.connectionURL>http://dev:3306/</db.connectionURL>
  </properties>
</profile>
<profile>
  <id>production</id>
  <properties>
    <db.username>prod</db.username>
    <db.password>prod_password</db.password>
    <db.connectionURL>http://prod:3306/</db.connectionURL>
  </properties>
</profile>
Run Code Online (Sandbox Code Playgroud)

然后,您可以使用过滤来通过每个环境中的正确值启用令牌替换:

<build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
        </resource>
    </resources>
</build>
Run Code Online (Sandbox Code Playgroud)

有关此解决方案的模式详细信息,请查看此页面.

如果你真的需要同一个文件的两个副本,你也可以使用

  • 用...什么?你能完成这句话吗?谢谢! (3认同)