如何通过 Maven 将额外的自定义类路径条目添加到 Spring Boot 运行中?

Tra*_*ant 4 java spring maven

如何在从 Maven 运行的 Spring Boot 中添加额外的类路径条目?

我想我需要在我的 pom.xml 中添加这样的东西:

            <configuration>
                <additionalClasspathElements>
                    <additionalClasspathElement>C:/resources</additionalClasspathElement>
                </additionalClasspathElements>
            </configuration>
Run Code Online (Sandbox Code Playgroud)

但是,我不知道什么插件适用。

Max*_*ulz 5

我假设您想要的是将额外的资源/配置文件添加到 Spring Boot 生成的可执行 jar 中的类路径中。我发现的唯一现成解决方案是使用maven-jar-plugin除了spring-boot-maven-plugin将类路径添加到清单中,例如:

<!-- setup jar manifest to executable with dependencies -->
<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <version>2.0.3.RELEASE</version>
    <configuration>
        <fork>true</fork>
        <mainClass>Application</mainClass>
        <classifier>executable</classifier>
        <layout>JAR</layout>
    </configuration>
    <executions>
        <execution>
            <goals>
                <goal>repackage</goal>
            </goals>
        </execution>
    </executions>
</plugin>

<!-- add configuration to manifest -->
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>3.0.2</version>
    <configuration>
        <archive>
            <manifestEntries>
                <Class-Path>conf/</Class-Path>
            </manifestEntries>
        </archive>
    </configuration>
</plugin>
Run Code Online (Sandbox Code Playgroud)

请注意,类路径将相对于生成的可执行 JAR 文件。


oot*_*ero -3

不会:

java -cp <path to classpath entry> -jar <path to program.jar>
Run Code Online (Sandbox Code Playgroud)

为你工作?

  • 据我所知,从信息中我读到的 -cp 或 -classpath 选项只有在不使用 -jar 选项时才起作用,这就是我运行我的 Spring Boot 应用程序的方式 (2认同)