gpt4 book ai didi

Java getClass().getClassLoader().getResource(path) 在 Maven 阴影 .jar 中失败

转载 作者:行者123 更新时间:2023-12-02 02:51:15 27 4
gpt4 key购买 nike

直接使用 java 运行我的项目时,我可以确认特定方法能够通过 getClass().getClassLoader().getResource(path) 获取资源,其中 pathdocs/info.md并且在 src/main/resources/docs/info.md 处存在一个文件

项目用Maven打包成最终的可执行.jar后,我可以确认路径docs/info.md已通过 jar tf myproject.jar 包含

但是,当执行该 .jar 时,相同的 getClass().getClassLoader().getResource(path) 返回 null。

这是myproject.pom:

<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>

<!-- io.outright.myproject [parent] -->
<parent>
<groupId>io.outright</groupId>
<artifactId>myproject</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>

<!-- io.outright.myproject -->
<groupId>io.outright.myproject</groupId>
<artifactId>hub</artifactId>
<version>1.0-SNAPSHOT</version>
<name>hub</name>

<!-- props -->
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<!-- deps -->
<dependencies>

<!-- ....removed for readability.... -->

</dependencies>

<!-- build -->

<build>

<resources>
<resource>
<directory>${basedir}/src/main/resources</directory>
<includes>
<include>**/*</include>
</includes>
</resource>
</resources>

<plugins>

<!-- Integration tests -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.19</version>
<executions>
<execution>
<id>integration-test</id>
<phase>integration-test</phase>
<goals>
<goal>integration-test</goal>
</goals>
</execution>
<execution>
<id>verify</id>
<phase>verify</phase>
<goals>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>

<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<inherited>true</inherited>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
</configuration>
</plugin>

<plugin>
<!-- Build an executable JAR -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<!--<classpathPrefix>lib/</classpathPrefix>-->
<mainClass>io.outright.myproject.Main</mainClass>
</manifest>
</archive>
</configuration>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<artifactSet>
<excludes>
<exclude>junit:junit</exclude>
<!--<exclude>log4j:log4j:jar:</exclude>-->
</excludes>
</artifactSet>
</configuration>
</execution>
</executions>
</plugin>

</plugins>
</build>

</project>

最佳答案

使用getResourceAsStream而不是getResourcegetResource 返回文件的 URL,而不是文件本身。然后将 InputStream 写入文件,并使用该文件。

ClassLoader classLoader = getClass().getClassLoader();
if (classLoader.getResourceAsStream(filePath) == null) {
// throw error
}
InputStream inputStream = classLoader.getResourceAsStream(filePath);
// write input stream to a file

我也遇到了同样的问题。这可能有帮助:Get directory path of maven plugin in its own Mojo

关于Java getClass().getClassLoader().getResource(path) 在 Maven 阴影 .jar 中失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43811764/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com