Java getClass().getClassLoader().getResource(path) 在 Maven 阴影 .jar 中失败
Java getClass().getClassLoader().getResource(path) fails inside Maven shaded .jar
当 运行 我的项目直接使用 java
时,我可以确认特定方法能够通过 getClass().getClassLoader().getResource( path),其中 path 是 docs/info.md
并且在 src/main/resources/docs/info.md
处存在一个文件
项目用Maven打包成最终的可执行文件.jar后,通过jar tf myproject.jar
确认路径docs/info.md
已经被包含
但是,当执行该 .jar 时,相同的 getClass().getClassLoader().getResource(path) returns 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
而不是 getResource
。 getResource
returns 文件的 URL,而不是文件本身。然后将 InputStream
写入文件,并使用该文件。
ClassLoader classLoader = getClass().getClassLoader();
if (classLoader.getResourceAsStream(filePath) == null) {
// throw error
}
InputStream inputStream = classLoader.getResourceAsStream(filePath);
// write input stream to a file
我遇到了同样的问题。这可能会有所帮助:
当 运行 我的项目直接使用 java
时,我可以确认特定方法能够通过 getClass().getClassLoader().getResource( path),其中 path 是 docs/info.md
并且在 src/main/resources/docs/info.md
项目用Maven打包成最终的可执行文件.jar后,通过jar tf myproject.jar
docs/info.md
已经被包含
但是,当执行该 .jar 时,相同的 getClass().getClassLoader().getResource(path) returns 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
而不是 getResource
。 getResource
returns 文件的 URL,而不是文件本身。然后将 InputStream
写入文件,并使用该文件。
ClassLoader classLoader = getClass().getClassLoader();
if (classLoader.getResourceAsStream(filePath) == null) {
// throw error
}
InputStream inputStream = classLoader.getResourceAsStream(filePath);
// write input stream to a file
我遇到了同样的问题。这可能会有所帮助: