Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to copy resource to src target directory with Maven?

Tags:

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.

like image 624
stankoua Avatar asked Apr 25 '14 09:04

stankoua


People also ask

Where is target folder in Maven?

The maven targets are located in your project config/ folder.

What is the target file in Maven?

Target. The target folder is the maven default output folder. When a project is build or packaged, all the content of the sources, resources and web files will be put inside of it, it will be used for construct the artifacts and for run tests. You can delete all the target folder content with mvn clean command.

Where do I put resources in POM xml?

Via the resources area in the pom you can filter files from their way src/main/resources to the target/classes folder.

How do I add resources to Maven?

To include a resource, we only need to add an <includes> element. And to exclude a resource, we only need to add an <excludes> element. For example, if we want to include all text and RTF files under our src/my-resources directory and in all its subdirectories, we can do the following: <project>


2 Answers

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>
like image 128
zpontikas Avatar answered Sep 30 '22 08:09

zpontikas


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>
like image 42
aberes Avatar answered Sep 30 '22 08:09

aberes