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 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:
into the package

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

    <version> 2.5 </version>
            <id> copy-resources </id>
            <phase> validate </phase>
                <goal> copy-resources </goal>
                <outputDirectory> ${basedir}/target/classes/intranet/com/mytroubleshootings/ </outputDirectory>
                        <directory> ${basedir}/src/main/java/intranet/com/mytroubleshootings/ </directory>
                            <include> </include>

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

No comments:

Post a Comment

(c) Copyright 2020 -