Spring Boot Maven插件包含资源

10

示例

是否可以通过配置Spring Boot Maven插件来包含依赖项中的资源。

例如,如果在我的Spring Boot项目中有:

  <dependency>
    <groupId>co.company</groupId>
    <artifactId>example</artifactId>
  </dependency>

在该JAR文件中,有一个名为 example.properties 的属性文件。

jar -tf example.jar | grep example.properties | wc -l

结果为1。

然而,当我构建Spring Boot JAR时,该属性文件未添加到src/main/resources中,包含它的JAR被包括在BOOT-INF/lib/example.jar中。

但是,在我的情况下,我想将src/main/resources的内容提取到Spring Boot JAR的boot BOOT-INF/classes/目录中,以便像自动配置这样的东西可以将其选起来。

现实世界

在现实世界中,我正在尝试使用以下方法:

  • thymeleaf模板(例如,我的依赖JAR提供HTML模板文件,但在部署的引导jar中,这些模板没有解析)
  • liquibase changelog文件(我的依赖项包括changelog文件,但这些文件未被执行 - 我认为liquibase autoconfig找不到changelog文件,因为它不在boot JAR的src/main/resources中)。
1个回答

12

我认为这个问题的解决方案与你之前提出的另一个问题的解决方案非常相似。

您可以在Spring Boot模块中使用maven-dependency-pluginunpack目标:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <executions>
        <execution>
            <goals>
                <goal>unpack</goal>
            </goals>
            <configuration>
                <artifactItems>
                    <artifactItem>
                        <groupId>${project.groupId}</groupId>
                        <artifactId>module-a</artifactId>
                        <version>${project.version}</version>
                        <includes>**/*.yaml</includes>
                    </artifactItem>
                </artifactItems>
                <outputDirectory>${project.build.directory}/classes/BOOT-INF/</outputDirectory>
            </configuration>
        </execution>
    </executions>
</plugin>

这将会把 module-a 的资源复制到你的 boot-moduleBOOT-INF 目录中。我已经在 GitHub 上发布了一个更完整的例子。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接