Maven: include resource file based on profile
Asked Answered
S

2

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.

Sherr answered 8/9, 2011 at 19:45 Comment(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.Quass
W
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.

Woebegone answered 8/9, 2011 at 19:49 Comment(4)
Your answer does not cover renaming of the persistence.xml file.Upbraid
Rather than having different source file names, you can have different folders where you load the resource from and have that as a property so you do not have to rename the file..Woebegone
@ManfredMoser - I think using custom properties within one persistence.xml is the appropriate way.Pharyngo
That might or might not be the case. In any case it is not what the questions is about.Woebegone
V
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.

Vanhook answered 13/1, 2014 at 9:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.