Maven default Resources not copying resources

Luke picture Luke · Mar 15, 2018 · Viewed 8.6k times · Source

this is my project directory structure:

enter image description here

And this is my pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>borsa</groupId>
<artifactId>borsa</artifactId>
<version>1</version>
<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
<build>
    <finalName>aggiornamento</finalName>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.7.0</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <executions>
                <execution>
                    <id>copy-dependencies</id>
                    <phase>prepare-package</phase>
                    <goals>
                        <goal>copy-dependencies</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>
                            ${project.build.directory}/dist/libs
                        </outputDirectory>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.6</version>
            <configuration>
                <outputDirectory>${basedir}/target/dist</outputDirectory>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <classpathPrefix>libs/</classpathPrefix>
                        <mainClass>
                            borsa.Application
                        </mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.20.1</version>
            <configuration>
                <skipTests>true</skipTests>
            </configuration>
        </plugin>
    </plugins>
</build>
<dependencies>
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi</artifactId>
        <version>3.17</version>
    </dependency>
    <dependency>
        <groupId>org.jsoup</groupId>
        <artifactId>jsoup</artifactId>
        <version>1.11.2</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
    </dependency>
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml</artifactId>
        <version>3.17</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>5.0.2.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>ethereal</groupId>
        <artifactId>ethereal</artifactId>
        <version>1</version>
    </dependency>
    <dependency>
        <groupId>common-parser</groupId>
        <artifactId>common-parser</artifactId>
        <version>1</version>
    </dependency>
    <dependency>
        <groupId>common-util</groupId>
        <artifactId>common-util</artifactId>
        <version>1</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-api</artifactId>
        <version>2.10.0</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
        <version>2.10.0</version>
    </dependency>
</dependencies>
</project>

If I run maven package or maven install resources are not copied to target folder, even if in the console appears the output:

[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ borsa ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 2 resources

However if I add in the pom the manual configuration of the resources plugin:

<plugin>
    <artifactId>maven-resources-plugin</artifactId>
    <version>3.0.2</version>
    <executions>
        <execution>
            <id>process-resources</id>
            <phase>process-resources</phase>
            <goals>
                <goal>resources</goal>
            </goals>
            <configuration>
        <outputDirectory>${project.build.directory}/dist/resources</outputDirectory>
                <resources>
                    <resource>
                        <directory>${basedir}/src/main/resources</directory>
                        <includes>
                            <include>*</include>
                        </includes>
                    </resource>
                </resources>
            </configuration>
        </execution>
    </executions>
</plugin>

resources get copied as expected.

enter image description here

So the question is: shouldn't the resources-plugin with goal resources run automatically?

Shouldn't the scr/main/resources folder be scanned automatically for resources and resources copied to target folder?

Why does it work if I explicitly configure the resources-plugin and why doesn't it work if I don't?

Answer

Luke picture Luke · Mar 16, 2018

Found the answer. The default behaviour of the maven-resources-plugin is to package the resources inside the jar. If you open the jar with a zip program you will see the resources inside it. To change the behaviour of the default resources plugin (maven-resources-plugin ) in the default phase (process-resources) with the default goal (resources) you must use the tag <resources> as child of <build>:

EDIT: the default behaviour of the maven-resources-plugin is to package the resources inside the jar alongside the target/classes folder.

<resources>
    <resource>
        <directory>src/main/resources</directory>
        <targetPath>${project.build.directory}/dist/resources</targetPath>
    </resource>
</resources>

In this way resources will go outside the jar in the specified folder. If you want to send some resources in the jar and some outside:

<resources>
    <resource>
        <directory>src/main/resources</directory>
        <targetPath>${project.build.directory}/dist/resources</targetPath>
        <includes>
            <include>this_file_will_go_outside_the_ jar_in_the_folder.txt</include>
            <include>this_too.txt</include>
        </includes>
    </resource>
    <resource>
        <directory>src/main/resources</directory>
        <!-- without targetPath everything not excluded will be packaged inside the jar -->
        <excludes>
            <exclude>this_file_will_go_outside_the_ jar_in_the_folder.txt</exclude>
            <exclude>this_too.txt</exclude>
        </excludes>
    </resource>
</resources>

EDIT: note that with the first option resources will still be copied into the target/classes folder (and in the jar/artifact). Excluded resources will not be present in the classpath so you will not be able to access them by running the code from Eclipse. You will only be able to access them from an executable jar with the targetPath folder specified as classpath in the manifest file. A better option is to confure the mave-jar-plugin and the maven-resources-plugin. See this and this answer.