【问题标题】:cannot get resources folder into jar when using spring boot maven plugin使用spring boot maven插件时无法将资源文件夹放入jar
【发布时间】:2018-07-16 01:10:28
【问题描述】:

我正在尝试构建一个与 spring boot 一起运行的可执行 jar,但我无法从资源文件夹中获取 spring xml 到 jar 中。看起来我的 outputDirectory 是错误的。定义它以便将其打包在 jar 中的正确方法是什么?

这是我的 pom 文件

<build>
    <resources>
        <resource>
            <directory>${basedir}/src/main/resources/my-resources</directory>
            <includes>
                <include>*xml</include>
            </includes>
        </resource>
    </resources>

    <plugins>

        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <mainClass>netexchange.exchange.main.ExchangeMain</mainClass>
                    </manifest>
                </archive>
                <outputDirectory>${basedir}/target</outputDirectory>
                <finalName>Matcher-with-config</finalName>
                <addResources>true</addResources>
            </configuration>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
    <defaultGoal>install</defaultGoal>
    <directory>${basedir}/target</directory>
</build>

【问题讨论】:

  • 为什么会有 Spring XML 文件?您是否尝试过根本不包含任何此配置并让默认魔法发挥作用?
  • 我的目标是最终使用不同的 spring XML 文件从同一来源创建许多 jar,所以我需要它来工作
  • 你想要“不同的 Spring XML 文件”,为什么?您似乎还没有阅读过引导配置系统(它相当全面,几乎可以肯定地以更灵活的安排涵盖了您的用例,尤其是外部化配置)。
  • 因为每个罐子需要不同的豆子。生病阅读更多关于外部化配置

标签: java maven spring-boot


【解决方案1】:

所以我想出了一个解决方案,将特定资源复制到文件夹“src/main/resources”中。 Spring boot build 自动包含该文件夹中的所有文件,然后您可以使用注解“@ImportResource({ "classpath:config.xml" })" 导入它们

我更新后的 pom 如下所示:

<build>
    <resources>
        <resource>
            <directory>${basedir}/src/main/resources/bitcoin-ethereum</directory>
            <includes>
                <include>*xml</include>
            </includes>
        </resource>
    </resources>

    <plugins>
        <plugin>
            <artifactId>maven-resources-plugin</artifactId>
            <version>3.0.2</version>
            <executions>
                <execution>
                    <id>copy-resources</id>
                    <phase>compile</phase>
                    <goals>
                        <goal>copy-resources</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${basedir}/src/main/resources</outputDirectory>
                        <resources>
                            <resource>
                                <directory>${basedir}/src/main/resources/matcher</directory>
                                <includes>
                                    <include>*xml</include>
                                </includes>
                            </resource>
                            <resource>
                                <directory>${basedir}/src/main/resources/common</directory>
                                <includes>
                                    <include>*xml</include>
                                </includes>
                            </resource>
                        </resources>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <mainClass>netexchange.exchange.main.ExchangeMain</mainClass>
                    </manifest>
                </archive>
                <outputDirectory>${basedir}/target</outputDirectory>
                <finalName>Matcher</finalName>
                <addResources>true</addResources>
            </configuration>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
    <defaultGoal>install</defaultGoal>
    <directory>${basedir}/target</directory>
</build>

【讨论】:

    猜你喜欢
    • 2016-01-16
    • 2018-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-25
    • 1970-01-01
    • 2023-04-03
    相关资源
    最近更新 更多