Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven default Resources not copying resources

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?

like image 967
Luke Avatar asked Mar 15 '18 14:03

Luke


People also ask

How do I add resources to my jar 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>

What is resource filtering in Maven?

You can use Maven to perform variable replacement on project resources. When resource filtering is activated, Maven will scan resources for property references surrounded by ${ and }.

What is Maven remote resources plugin?

This plugin is used to retrieve JARs of resources from remote repositories, process those resources, and incorporate them into JARs you build with Maven. A very common use-case is the need to package certain resources in a consistent way across your organization.


1 Answers

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.

like image 85
Luke Avatar answered Sep 28 '22 23:09

Luke