Sunday, September 13, 2015

Maven copy configuration file or resource in target classes directory

Although building a maven project you have to use a specific directory for resources, there are some cases that you have to keep a resource in a different directory. If you put a .properties file in src or into a java package, maven will not copy that file in the target classes directory, so in your deployment package it will not be present. Here is the procedure to force the copy and make it works.

Here I want to take a real case. If you want to change the JAXB provider, you have to put a file named jab.properties in the folder containing jaxb annotated classes. But maven will not copy this file in target by default while building the project.

So, suppose you have put your configuration file into a package, for example the file named:
jaxb.properties
into the package
com.mytroubleshootings

Then you have to add a plugin in your pom.xml, between the <plugins></plugins> tags, in this way:

<plugin>
    <artifactId>maven-resources-plugin</artifactId>
    <version> 2.5 </version>
    <executions>
        <execution>
            <id> copy-resources </id>
            <phase> validate </phase>
            <goals>
                <goal> copy-resources </goal>
            </goals>
            <configuration>
                <outputDirectory> ${basedir}/target/classes/intranet/com/mytroubleshootings/ </outputDirectory>
                <resources>
                    <resource>
                        <directory> ${basedir}/src/main/java/intranet/com/mytroubleshootings/ </directory>
                        <includes>
                            <include> jaxb.properties </include>
                        </includes>
                    </resource>
                </resources>
            </configuration>
        </execution>
    </executions>
</plugin>

You can change the resource directory, the filename, and the output directory as needed.

No comments:

Post a Comment

(c) Copyright 2020 - MyTroubleshooting.com