Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to deliver properties files in addition to artifacts?

I am using Maven2 to build a WAR project. Some properties files are dependent on the environment targeted for a release.

I want to deliver, in addition of the WAR, a file called datasource.xml. This file already exists in my project directory, but contains properties that will be filtered during the build (i.e. some ${foo.bar}).

In others words, after running the command mvn clean install, I want to see in my target/ directory two files, my-webapp.war and datasource.xml.

Note that datasource.xml must not be included in the my-webapp.war artifact!

How can I do this?

like image 288
Romain Linsolas Avatar asked Aug 28 '09 09:08

Romain Linsolas


1 Answers

You can attach additional artifacts using the build-helper-maven-plugin. The configuration below would attach datasource.xml as an additional artifact during the package phase. If that artifact is defined outside of src/main/resources and src/main/webapp it will not be included in the war.

Update: to ensure resource filtering is applied per your comment, you can specify an execution of the resource-plugin's copy-resources goal, specifying filtering to be applied. You can then still attach that filtered artifact using the build-helper-maven-plugin by referencing the corresponding target directory. I've updated the example below to show this usage.

<plugin>
  <artifactId>maven-resources-plugin</artifactId>
  <version>2.4</version>
  <executions>
    <execution>
      <id>copy-resources</id>
      <phase>validate</phase>
      <goals>
        <goal>copy-resources</goal>
      </goals>
      <configuration>
        <outputDirectory>${project.build.outputDirectory}/datasource</outputDirectory>
        <resources>          
          <resource>
            <directory>src/main/datasource</directory>
            <filtering>true</filtering>
          </resource>
        </resources>              
      </configuration>            
    </execution>
  </executions>
</plugin>
<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>build-helper-maven-plugin</artifactId>
  <version>1.3</version>
  <executions>
    <execution>
      <id>attach-artifacts</id>
      <phase>package</phase>
      <goals>
        <goal>attach-artifact</goal>
      </goals>
      <configuration>
        <artifacts>
          <artifact>
            <file>${project.build.outputDirectory}/datasource/datasource.xml</file>
            <type>xml </type>
            <classifier>datasource</classifier>
          </artifact>
        </artifacts>
      </configuration>
    </execution>
  </executions>
</plugin>

This won't appear in the target folder, but it will be deployed/installed to the repository alongside the war.

The attached artifact can be referenced by defining a dependency with the classifier "datasource". For example:

<dependency>
  <groupId>my.group.id</groupId>
  <artifactId>my-artifact-id/artifactId>
  <version>1.0.0</version>
  <classifier>datasource</classifier>
  <type>xml</type>
</dependency>

You could use the the dependency plugin's copy goal to retrieve the artifact and put it wherever it is required as part of your deployment process.

like image 69
Rich Seller Avatar answered Oct 15 '22 04:10

Rich Seller