In Maven how to exclude resources from the generated jar?

后端 未结 8 1114
余生分开走
余生分开走 2020-11-30 01:46

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.

UPD

相关标签:
8条回答
  • 2020-11-30 01:59

    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>
    
    0 讨论(0)
  • 2020-11-30 02:01

    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

    0 讨论(0)
  • 2020-11-30 02:02

    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.

    0 讨论(0)
  • 2020-11-30 02:02

    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 讨论(0)
  • 2020-11-30 02:03

    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.

    0 讨论(0)
  • 2020-11-30 02:14

    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.

    0 讨论(0)
提交回复
热议问题