maven, xml files for different profiles
Asked Answered
S

1

6

I have maven pom with 2 profiles: dev and production

I have some xml files in my project. For example persistence.xml . Settings for dev and production environments are different

I need a way to have right files in dev and production assemblies

Maybe possible to have 2 copies of each xml file and put into assemblies right one? Or maybe possible to use settings from pom file inside xml file ?

Any other ideas or best practices?

Sassy answered 1/12, 2012 at 9:57 Comment(2)
Study this article for example. Filtering would be a good idea, I think.Millisecond
I would suggest to take a look at this article. #11065773Stocker
F
11

What you are looking for was already answered here: Maven: include resource file based on profile

Instead of having two files, another solution would be to use properties directly inside the 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"/>

In your pom.xml, define a value for each property for each environment:

<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>

You could then use filtering to enable token replacement by the right value in each environement:

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

For mode details on this solution look at this page.

If you really need to have two copy of the same file, you could also use the

Furtherance answered 1/12, 2012 at 11:16 Comment(2)
use the... what? Can you please complete the sentence? thanks!Falster
This is kinda old already, but I guess I was refering to the copy-resources goal of the maven-resources-plugin (maven.apache.org/plugins/maven-resources-plugin/examples/…). HTHFurtherance

© 2022 - 2024 — McMap. All rights reserved.