15

I'm converting an Ant webapp project over to Maven. I have most of it working, but I'm stuck trying to figure out how to copy some resource files from different sources based on the profile.

I have src/main/resources/persistence-{dev, prod}.xml. One of these needs to be included in the war file as WEB-INF/classes/META-INF/persistence.xml.

I would like the dev version to be copied when the dev profile is active, and the prod version when prod is active.

George Armhold
  • 30,824
  • 50
  • 153
  • 232
  • 1
    I don't think the maven resources plugin solve his problem because as far as i know this plugin can only include or exludes files, not copy and rename them. – allaf Mar 13 '13 at 16:29

2 Answers2

13

Just use the maven resources plugin like so http://maven.apache.org/plugins/maven-resources-plugin/examples/include-exclude.html and have a property for the file name or extension set in a profile.

Manfred Moser
  • 29,539
  • 13
  • 92
  • 123
7

If you are not wedded to the paradigm of having 3 separate persistence.xml files and copying one or the other selectively, you can use maven profiles with filtering like this (just implemented this the other day and today came across your post):

In persistence.xml:

<property name="hibernate.show_sql" value="${hibernate.debug}" />
<property name="hibernate.format_sql" value="${hibernate.debug}" />

In pom.xml create a profile and define the variable:

<profiles>
  <profile>
    <id>hib-debug</id>
    <properties>
      <hibernate.debug>true</hibernate.debug>
    </properties>
  </profile>
</profiles>

define a default for when you build without specifying a profile:

<properties>
  <hibernate.debug>false</hibernate.debug>
</properties>

and.... make sure you turn on resource filtering:

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

Then you build with mvn -Phib-debug and voila! Substitution is done.

april26
  • 833
  • 3
  • 14
  • 27