Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 254
Mejmo Avatar asked Sep 29 '15 09:09

Mejmo


Video Answer


2 Answers

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>
like image 158
Nick A. Watts Avatar answered Sep 28 '22 07:09

Nick A. Watts


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

like image 26
jfcorugedo Avatar answered Sep 28 '22 07:09

jfcorugedo