Question

I'm currently working on an existing project which has a pom.xml file with the following:

<resources>
    <resource>
        <filtering>false</filtering>
        <directory>src</directory>
        <includes>
            <include>**/*.properties</include>
        </includes>
    </resource>
</resources>

I have in the base path a directory called properties which contains properties files. I want to copy when packaging all the properties files contains under properties/ in my src directory (otherwise the program will crash due to missing configuration files).

So my question is:

How can i, with Maven include resource files that are not located under src directory?

I try this one but it doesn't seem to work:

<resources>
    <resource>
        <filtering>false</filtering>
        <directory>src</directory>
        <includes>
            <include>**/*.properties</include>
            <include>../properties/**</include>
        </includes>
    </resource>
</resources>

Thank's for your help.

Was it helpful?

Solution

If your file structure is like this: Standard Directory Layout

enter image description here

Then you dont have to add the resources elemt. Maven copies by default all the files and folders that are located in your /src/main/resources folder to your build folder and locates them in the root of your compiled classpath files.
if you have for example a file called configuration.properties located in /src/main/resources/configuration.properties then when running mvn clean compile this file will be copied to your /target/classes/configuration.properties So if you remove that part the files will be located where u want them

<resource>
    <filtering>false</filtering>
    <directory>src</directory>
    <includes>
        <include>**/*.properties</include>
    </includes>
</resource>

OTHER TIPS

By migrating of projects from ant to maven without changing project structure set your sourceDirectory testSourceDirectory in the build and use the maven-resource-plugin as folowing take care in wich phase you execute the goals.

        <plugin>
            <artifactId>maven-resources-plugin</artifactId>
            <version>2.6</version>
            <executions>
                <execution>
                    <id>copy-resources01</id>
                    <phase>process-classes</phase>
                    <goals>
                        <goal>copy-resources</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${basedir}/target/classes</outputDirectory>
                        <encoding>UTF-8</encoding>
                        <resources>
                            <resource>
                                <directory>${basedir}/src</directory>
                                <includes>
                                    <include>**/*.properties</include>
                                </includes>
                            </resource>
                        </resources>
                    </configuration>
                </execution>
                <execution>
                    <id>copy-resources02</id>
                    <phase>verify</phase>
                    <goals>
                        <goal>copy-resources</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${basedir}/build/lib</outputDirectory>
                        <encoding>UTF-8</encoding>
                        <resources>
                            <resource>
                                <directory>${basedir}/target/</directory>
                                <include>*.jar</include>
                            </resource>
                        </resources>
                    </configuration>
                </execution>
            </executions>
        </plugin>

To copy everything from source to destination, I used following

        <plugin>
            <artifactId>maven-resources-plugin</artifactId>
            <version>2.6</version>
            <executions>
                <execution>
                    <id>copy-resources</id>
                    <phase>process-classes</phase>
                    <goals>
                        <goal>copy-resources</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${basedir}/target/classes/static/</outputDirectory>
                        <resources>
                            <resource>
                                <directory>${basedir}/gui/build/</directory>
                                <includes>
                                    <include>**/*.*</include>
                                </includes>
                            </resource>
                        </resources>
                    </configuration>
                </execution>
            </executions>
        </plugin>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top