Mejmo
Mejmo

Reputation: 2593

How to exclude resource file from spring boot jar?

I am using maven-spring-boot plugin for jar generating. I have multiple resource files with configuration (application-production.yml, application-test.yml, application-development.yml).

Thing is, when I generate the release for our customers, I would like to exclude development and test files. Is it possible to exclude resource file in maven-spring-boot plugin?

I tried this:

        <build>
            <resources>
                <resource>
                    <directory>src/main/resources</directory>
                    <excludes>
                        <exclude>application-dev*</exclude>
                        <exclude>application-test*</exclude>
                    </excludes>
                </resource>
            </resources>
        </build>

but the maven plugin uses its own scripts for resource management (for example @val@ replacing etc.) and it fails during packaging if it is added to pom:

Caused by: org.yaml.snakeyaml.scanner.ScannerException: while scanning for the next token
found character @ '@' that cannot start any token. (Do not use @ for indentation)
 in 'reader', line 4, column 18:
    project.version: @project.version@

without it, it works ok.

Upvotes: 8

Views: 29402

Answers (3)

Nick A. Watts
Nick A. Watts

Reputation: 979

The Spring Boot Maven plugin repackages the JAR file created by the Maven JAR Plugin. So you also have the option to simply exclude files when the JAR is first built, which keeps the Spring Boot Maven plugin from finding them in the first place:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>3.1.0</version>
    <configuration>
        <excludes>
             <exclude>application-dev*</exclude>
             <exclude>application-test*</exclude>
        </excludes>
    </configuration>
</plugin>

Upvotes: 12

jfcorugedo
jfcorugedo

Reputation: 10051

Instead of using maven-spring-boot plugin use maven-resource plugin and maven profiles:

<profiles>
  <profile>
    <id>prod</id>
    <build>
      <resources>
        <resource>
          <filtering>true</filtering>
          <directory>[your directory]</directory>
          <excludes>
            <exclude>[non-resource file #1]</exclude>
            <exclude>[non-resource file #2]</exclude>
            <exclude>[non-resource file #3]</exclude>
            ...
            <exclude>[non-resource file #n]</exclude>
          </excludes>
        </resource>
      </resources>
    </build>
  </profile>
</profiles>

Make sure you specify <filtering>true</filtering> option inside resource element.

Create one profile for each environment and filter those files.

Make sure to execute maven with the proper profile:

mvn clean install -P prod

To view more examples of maven-resource plugin take a look at maven-resource

If you want to learn more about profiles, take a look at profiles

Upvotes: 8

Feiyu Zhou
Feiyu Zhou

Reputation: 4554

Using maven-resources-plugin, it support excludes tag.

By the way, why you need to use three yaml files? you can write those configuration in one application.yaml file with "---" and spring.profiles, like:

memcached.addresses: test03:11211
spring.profiles.active: dev
---
# dev
spring:
    profiles: dev
logging.access.enabled: false
---
# test
spring:
    profiles: test
logging.config: classpath:log4j.test.properties
logging.access.dir: /home/shared/log
---
# online
spring:
    profiles: online
logging.config: classpath:log4j.online.properties
logging.access.dir: /home/shared/log

Upvotes: 0

Related Questions