如何从spring boot jar中排除资源文件?

Mej*_*jmo 5 java maven spring-boot

我正在使用maven-spring-boot插件来生成jar.我有多个配置(application-production.yml, application-test.yml, application-development.yml)的资源文件.

事实上,当我为客户生成版本时,我想排除开发和测试文件.是否可以在maven-spring-boot插件中排除资源文件?

我试过这个:

        <build>
            <resources>
                <resource>
                    <directory>src/main/resources</directory>
                    <excludes>
                        <exclude>application-dev*</exclude>
                        <exclude>application-test*</exclude>
                    </excludes>
                </resource>
            </resources>
        </build>
Run Code Online (Sandbox Code Playgroud)

但maven插件使用自己的脚本进行资源管理(例如@ val @ replacement等),如果将它添加到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@
Run Code Online (Sandbox Code Playgroud)

没有它,它可以正常工作.

jfc*_*edo 6

而不是使用maven-spring-boot插件使用maven-resource插件和maven配置文件:

<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>
Run Code Online (Sandbox Code Playgroud)

确保<filtering>true</filtering>在资源元素中指定选项.

为每个环境创建一个配置文件并过滤这些文件.

确保使用正确的配置文件执行maven:

mvn clean install -P prod
Run Code Online (Sandbox Code Playgroud)

要查看maven-resource插件的更多示例,请查看maven-resource

如果您想了解有关配置文件的更多信息,请查看配置文件


Nic*_*tts 6

Spring Boot Maven 插件重新打包了 Maven JAR 插件创建的 JAR 文件。因此,您还可以选择在首次构建 JAR 时简单地排除文件,这可以防止 Spring Boot Maven 插件首先找到它们:

<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>
Run Code Online (Sandbox Code Playgroud)