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?

Victor Mezrin
  • 2,797
  • 2
  • 32
  • 48
  • 1
    Study [this article](http://www.manydesigns.com/en/portofino/portofino3/tutorials/using-maven-profiles-and-resource-filtering) for example. Filtering would be a good idea, I think. – Andrew Logvinov Dec 01 '12 at 10:19
  • I would suggest to take a look at this article. http://stackoverflow.com/questions/11065273/specifying-runtime-configuration-parameters-to-a-maven-built-web-application/11065391#11065391 – khmarbaise Dec 01 '12 at 12:38

1 Answers1

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

Community
  • 1
  • 1
Olivier.Roger
  • 4,241
  • 5
  • 40
  • 68
  • 3
    use the... what? Can you please complete the sentence? thanks! – Rafael Sisto Oct 19 '13 at 13:20
  • This is kinda old already, but I guess I was refering to the copy-resources goal of the maven-resources-plugin (http://maven.apache.org/plugins/maven-resources-plugin/examples/copy-resources.html). HTH – Olivier.Roger Oct 20 '13 at 13:36