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.

2
  • Could you be more precise about these resources? Why do you want to exclude them from the JAR? When do you need them? Nov 6, 2010 at 17:45
  • 1
    I want to exclude them because they store the application's configurations
    – user168237
    Nov 6, 2010 at 18:22

9 Answers 9

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>
4
  • 24
    This answer (stackoverflow.com/a/3004684/363573) provides, in my opinion, a more elegant way to exclude unwanted resources with maven jar plugin.
    – Stephan
    Apr 12, 2012 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 Dec 30, 2013 at 15:48
  • Damior consider editing your answer. @AfshinMoazami has a solid point. The other answer linked by Stephan is more structured / reliable / accurate Apr 9, 2015 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, 2017 at 8: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.

4
  • 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.
    – user168237
    Nov 7, 2010 at 3:16
  • 1
    To be OK with conventions, one can use the maven jar plugin. See this answer : stackoverflow.com/a/3004684/363573
    – Stephan
    Apr 12, 2012 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 8, 2017 at 15:42
  • This is the answer that gave me the best insight and allowed me to understand how to correctly structure my project. May 17, 2019 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.

1
  • 10
    <exclude>src/test/resources/**</exclude> doesn't work. Exclude will be applied on jar final path and should be <exclude>*.properties</exclude> Dec 24, 2015 at 12:43
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.

7
  • Thanks a lot for a simple solution. I wish I could accept all answers here.
    – user168237
    Nov 6, 2010 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. Jul 12, 2013 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) Nov 7, 2013 at 0:36
  • 1
    So where do you actual test resources go?
    – Andrew
    Nov 26, 2014 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, 2015 at 15:17
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>
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

3
  • 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)
    – user168237
    Nov 6, 2010 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 6, 2010 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 stackoverflow.com/questions/3001713/… Apr 9, 2015 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.

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.

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>

...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.