首页 > 解决方案 > 在 Spring Boot 中将更改日志文件添加到类路径

问题描述

在我的 Spring Boot 项目中,我有一个读取CHANGELOG.md文件内容的端点。我想从类路径中读取我的 CHANGELOG 文件,因为我的代码中有这样的内容:

ClassPathResource myChangeLogFile = new ClassPathResource("CHANGELOG.MD");

标签: spring-bootmavenclasspath

解决方案


您必须将其包含在生成的 JAR 文件中,或者将其放在默认包含在类路径中的目录中(例如 src/main/resources)。

要包含在 JAR 文件中,您可以执行以下操作:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>build-helper-maven-plugin</artifactId>
    <version>${build-helper-maven-plugin.version}</version>
    <executions>
        <execution>
            <id>attach-artifacts</id>
            <phase>package</phase>
            <goals>
                <goal>attach-artifact</goal>
            </goals>
            <configuration>
                <artifacts>
                    <artifact>
                        <file>CHANGELOG.md</file>
                        <type>md</type>
                        <classifier>changelog</classifier>
                    </artifact>
                </artifacts>
            </configuration>
        </execution>
    </executions>
</plugin>

推荐阅读