73

When I create an executable jar with dependencies (using this guide), all properties files are packaged into that jar too. How to stop it from happening? Thanks.

UPDATE: I tried to exclude them using the Maven resources plugin, but then my application won't find the properties files when I run it in Eclipse (right click on the module -> Run As -> Java Application)

UPDATE: Thanks for your useful answers. I think I'd better spend time to learn Maven, for now I just choose the simplest solution.

Community
  • 1
  • 1

9 Answers9

135

To exclude any file from a jar / target directory you can use the <excludes> tag in your pom.xml file.

In the next example, all files with .properties extension will not be included:

<build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <excludes>
                <exclude>*.properties</exclude>
            </excludes>
            <filtering>false</filtering>
        </resource>
    </resources>
</build>
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
Damior
  • 1,449
  • 2
  • 10
  • 3
  • 24
    This answer (http://stackoverflow.com/a/3004684/363573) provides, in my opinion, a more elegant way to exclude unwanted resources with maven jar plugin. – Stephan Apr 12 '12 at 14:21
  • 9
    If I am not wrong, there is a problem with this solution, because it exclude these files from all the plugins. How about just excluding them from assembly plugin? I have tried the same configuration inside the configuration tag in the plugin tag, and it doesn't work – Afshin Moazami Dec 30 '13 at 15:48
  • Damior consider editing your answer. @AfshinMoazami has a solid point. The other answer linked by Stephan is more structured / reliable / accurate – Don Cheadle Apr 09 '15 at 21:03
  • 4
    This will exclue them even when IDEA make the module, I only want to exclude it when package, how? – JaskeyLam Jan 10 '17 at 08:03
17

By convention, the directory src/main/resources contains the resources that will be used by the application. So Maven will include them in the final JAR.

Thus in your application, you will access them using the getResourceAsStream() method, as the resources are loaded in the classpath.

If you need to have them outside your application, do not store them in src/main/resources as they will be bundled by Maven. Of course, you can exclude them (using the link given by chkal) but it is better to create another directory (for example src/main/external-resources) in order to keep the conventions regarding the src/main/resources directory.

In the latter case, you will have to deliver the resources independently as your JAR file (this can be achieved by using the Assembly plugin). If you need to access them in your Eclipse environment, go to the Properties of your project, then in Java Build Path in Sources tab, add the folder (for example src/main/external-resources). Eclipse will then add this directory in the classpath.

Romain Linsolas
  • 79,475
  • 49
  • 202
  • 273
  • 1
    I think I'm doing things wrong. Perhaps `src/main/resources` is not a place to put the application's configurations. I have a difficult situation in which I haven't known how to write unit tests yet. –  Nov 07 '10 at 03:16
  • 1
    To be OK with conventions, one can use the maven jar plugin. See this answer : http://stackoverflow.com/a/3004684/363573 – Stephan Apr 12 '12 at 14:22
  • src/main/resources is the default maven convention used to put resources, however another convention maven uses is that test property resources should go in src/test/resources. Anything placed there won't get packaged with a deliverable jar, when building in maven. – atom88 Dec 08 '17 at 15:42
  • This is the answer that gave me the best insight and allowed me to understand how to correctly structure my project. – Michaël Vreux May 17 '19 at 10:34
16

This calls exactly for the using the Maven JAR Plugin

For example, if you want to exclude everything under src/test/resources/ from the final jar, put this:

<build>

        <plugins>
            <!-- configure JAR build -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.3.1</version>
                <configuration>
                    <excludes>
                        <exclude>src/test/resources/**</exclude>
                    </excludes>
                </configuration>
            </plugin>

...

Files under src/test/resources/ will still be available on class-path, they just won't be in resulting JAR.

Don Cheadle
  • 5,224
  • 5
  • 39
  • 54
9

Put those properties files in src/test/resources. Files in src/test/resources are available within Eclipse automatically via eclipse:eclipse but will not be included in the packaged JAR by Maven.

Mads Hansen
  • 63,927
  • 12
  • 112
  • 147
swu
  • 382
  • 1
  • 2
  • Thanks a lot for a simple solution. I wish I could accept all answers here. –  Nov 06 '10 at 23:49
  • 64
    This is a bad answer as it is Eclipse specific, relying on an inability for Eclipse to implement separate classpaths for main and test. Other IDEs have separate classpaths, and so your code won't work in those IDEs. Hopefully Eclipse will have separate classpaths at some point too. – JodaStephen Jul 12 '13 at 21:29
  • Be sure to define build/outputDirectory to be different from Eclipse's default output folder, otherwise a jar built from mvn command line might include stuff Eclipse has put there (eg contents of src/test/resources) – JasonPlutext Nov 07 '13 at 00:36
  • 1
    So where do you actual test resources go? – Andrew Nov 26 '14 at 15:07
  • 1
    Generally speaking this is also a bad answer as it requires changing the setup given in the question. Maybe one needs their stuff in `src/main/resources`. – zb226 Feb 13 '15 at 15:17
  • see my answer below -- this is a great time to use the `Maven JAR plugin` – Don Cheadle Apr 09 '15 at 21:09
  • While the mention of eclipse is correct, I don't believe that the src/test/resources path is specific to eclipse, but rather a convention that maven follows by default. So, if you're using maven to build that solution will work regardless of which IDE you're using. – atom88 Dec 08 '17 at 15:40
9

Exclude specific pattern of file during creation of maven jar using maven-jar-plugin.

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-jar-plugin</artifactId>
  <version>2.3</version>
  <configuration>
    <excludes>
      <exclude>**/*.properties</exclude>
      <exclude>**/*.xml</exclude>
      <exclude>**/*.exe</exclude>
      <exclude>**/*.java</exclude>
      <exclude>**/*.xls</exclude>
    </excludes>
  </configuration>
</plugin>
Aleks Andreev
  • 7,016
  • 8
  • 29
  • 37
Radadiya Nikunj
  • 988
  • 11
  • 10
7

Do you mean to property files located in src/main/resources? Then you should exclude them using the maven-resource-plugin. See the following page for details:

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

chkal
  • 5,598
  • 21
  • 26
  • 1
    But then my application can't find the properties files when I run it in Eclipse (right click on the module -> Run As -> Java Application) –  Nov 06 '10 at 17:17
  • You could add a separate Maven profile for the generation of the executable JAR and exclude the property files on there. – chkal Nov 06 '10 at 17:52
  • rather than exclude the directory entirely, OP wants to exclude them when the JAR is being made. So it makes more sense to use `maven-jar-plug` and exclude within there. Like this answer http://stackoverflow.com/questions/3001713/where-in-maven-projects-path-should-i-put-configuration-files-that-are-not-cons?lq=1 – Don Cheadle Apr 09 '15 at 21:04
2

Another possibility is to use the Maven Shade Plugin, e.g. to exclude a logging properties file used only locally in your IDE:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>${maven-shade-plugin-version}</version>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>shade</goal>
            </goals>
            <configuration>
                <filters>
                    <filter>
                        <artifact>*:*</artifact>
                        <excludes>
                            <exclude>log4j2.xml</exclude>
                        </excludes>
                    </filter>
                </filters>
            </configuration>
        </execution>
    </executions>
</plugin>

This will however exclude the files from every artifact, so it might not be feasible in every situation.

jansohn
  • 2,246
  • 2
  • 28
  • 40
0

When I create an executable jar with dependencies (using this guide), all properties files are packaged into that jar too. How to stop it from happening? Thanks.

Properties files from where? Your main jar? Dependencies?

In the former case, putting resources under src/test/resources as suggested is probably the most straight forward and simplest option.

In the later case, you'll have to create a custom assembly descriptor with special excludes/exclude in the unpackOptions.

Pascal Thivent
  • 562,542
  • 136
  • 1,062
  • 1,124
0

here is another solution to exclude all files in resources folder, the final configuration looks like:

<build>
    <!-- exclude all files in resources-->    
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <excludes>
                <exclude>**/**</exclude>
            </excludes>
            <filtering>false</filtering>
        </resource>
    </resources>

    <!-- other configurations/plugins in the pom.xml-->
</build>

or we can use includes to only package some file or folder. But this method has a side effect. IDE will also exclude the resource files in target/classes folder. maven-jar-plugin only affect jar file.

I found a better solution to execludes resourses folder using maven-jar-plugin, here we use includes:

<build>

        <plugins>
            <!-- configure JAR build -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.3.1</version>
                <configuration>
                    <includes>
                        <include>**/*.class</include>
                    </includes>
                </configuration>
            </plugin>

...
kissLife
  • 307
  • 1
  • 2
  • 9