简体   繁体   中英

Java getClass().getClassLoader().getResource(path) fails inside Maven shaded .jar

When running my project directly with java , I can confirm that a particular method is able to fetch a resource via getClass().getClassLoader().getResource(path) , where path is docs/info.md and there exists a file at src/main/resources/docs/info.md

After the project is packaged with Maven into the final executable .jar, I can confirm that the path docs/info.md has been included via jar tf myproject.jar

However, when that .jar is executed, the same getClass().getClassLoader().getResource(path) returns null.

Here's 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>

Use getResourceAsStream instead of getResource . getResource returns the URL of file, not the file itself. Then write the InputStream to a file, and use that file.

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

I faced the same issue. This might help: Get directory path of maven plugin in its own Mojo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM