34

I have \src\main\resources\logback.xml file. When I run mvn package it is placed into the jar by default. How do I make Maven place it next to jar, not inside?

So, well, I just want Maven to copy the resource to the same folder where jar will be.

Vladislav Rastrusny
  • 29,378
  • 23
  • 95
  • 156

3 Answers3

85

No plugin definitions needed, just edit the build resources:

<build>
    <resources>
        <!-- regular resource processsing for everything except logback.xml -->
        <resource>
            <directory>src/main/resources</directory>
            <excludes>
                <exclude>logback.xml</exclude>
            </excludes>
        </resource>
        <!-- resource processsing with a different output directory
             for logback.xml -->
        <resource>
            <directory>src/main/resources</directory>
            <includes>
                <include>logback.xml</include>
            </includes>
            <!-- relative to target/classes
                 i.e. ${project.build.outputDirectory} -->
            <targetPath>..</targetPath>
        </resource>
    </resources>
</build>

Reference:

Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
  • In what `phase` are the resources being copied? In my case I use `maven-dependency-plugin` to copy a `war` file into the depending project - from there I want it to be loaded into the final `.jar` but I'd like to guarantee that I always take the latest build of that `war` file. Btw: The first link appears to be dead. – Stefan Falk Feb 06 '17 at 07:56
  • @displayname fixed link. and process-resources is the phase you are looking for. – Sean Patrick Floyd Feb 06 '17 at 07:58
  • Hello sir , how can i place the folders of resources folder under `src/main` in final jar ? – GOXR3PLUS Sep 22 '18 at 12:21
  • @GOXR3PLUS set `` to the destination where you want your resources copied. See [docs](https://maven.apache.org/ref/3.5.0/apidocs/org/apache/maven/model/Resource.html#setTargetPath(java.lang.String)). – Sean Patrick Floyd Sep 27 '18 at 17:12
  • thank you. but my question is how the jar file should be executed because resources cannot be found by using "java -jar file.jar"? – Amin Heydari Alashti Apr 22 '19 at 06:48
  • @epcpu you should ask that as a separate question – Sean Patrick Floyd Apr 23 '19 at 20:36
  • Note: Remove the spring-boot plugin else it will create the file inside BOOT_INF – Chinmoy Mar 12 '21 at 18:35
0

You can build a ZIP file containing both using the Maven Assembly plugin.

You can also use Maven Antrun plugin or similar to put whatever file you want but the idea of single artifact per project is built deep into Maven internals so there is a chance it will hunt you down elsewhere.

Sasha O
  • 3,710
  • 2
  • 35
  • 45
0

Exclude that file: http://maven.apache.org/plugins/maven-resources-plugin/examples/include-exclude.html

And then use the Maven Antrun plugin to copy the file.

However, the latter part does not make much sense. If it is a configuration file to put in a server, simply manually copy it.

apetrelli
  • 718
  • 4
  • 18