A maven project that have multiple property files via maven antrun plugin

This tutorial will answer several questions (below) within a maven project that has several configuration files (dataset, property, persistence).

1.) How to have multiple persistence, datasource and property files depending on the profile or environment variable (xxx-dev, xxx-integr, xxx-prod)?

2.) How to execute a maven copy/rename/move in a pom?

3.) How to execute an ant if inside a maven pom?

It's best to answer this question in this simple chunk of code :-)


<plugin>
 <artifactId>maven-antrun-plugin</artifactId>
 <version>1.7</version>
 <executions>
  <execution>
   <phase>install</phase>
   <configuration>
    <target>
     <echo>Execute ant tasks</echo>
     <taskdef resource="net/sf/antcontrib/antcontrib.properties">
     </taskdef>
     <if>
      <isset property="local.tomcat.home" />
      <then>
       <copy
        file="${project.basedir}/src/main/resources/ipiel-${env}.properties"
        tofile="${local.tomcat.home}\conf\ipiel.properties" />
      </then>
     </if>
     <copy
      file="${project.basedir}/src/main/resources/META-INF/persistence-${env}.xml"
      tofile="${project.build.directory}/${project.artifactId}-${project.version}/META-INF/persistence.xml" />
    </target>

   </configuration>
   <goals>
    <goal>run</goal>
   </goals>
  </execution>
 </executions>
 <dependencies>
  <dependency>
   <groupId>ant-contrib</groupId>
   <artifactId>ant-contrib</artifactId>
   <version>20020829</version>
  </dependency>
 </dependencies>
</plugin>

Some notes:
1.) We need to define a dependency to ant-contrib to be able to use the ant if tag.
2.) The copy property file will execute if property local.tomcat.home is set.

Note, using -P production will only activate that profile but will not replace any variable in the actual property file. To be able to get the actual file we need to specify -Denv=prod. Also we need to bind it to the test phase and not the install phase.
A maven project that have multiple property files via maven antrun plugin A maven project that have multiple property files via maven antrun plugin Reviewed by Edward Legaspi on Saturday, August 18, 2012 Rating: 5

No comments:

Powered by Blogger.