0

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>
Charney Kaye
  • 3,667
  • 6
  • 41
  • 54
  • see this answer maybe: http://stackoverflow.com/a/43672609/180100 –  May 05 '17 at 18:47
  • 2
    Not sure if this is the issue, but we would normally use a "/" at the beginning of the path to the resource. So: ...getResource("/docs/info.md"). – Rob May 05 '17 at 18:48
  • @Rob no. The leading / is used when calling getResource on a Class, otherwise the path is search relative to the class package. When called on a ClassLoader, the path must not start with a /. – JB Nizet May 05 '17 at 19:54
  • It turns out, per @RC comment and stackoverflow.com/a/43672609/180100 , my solution was to use (instead of the ClassLoader) **getClass().getResourceAsStream(path);**. – Charney Kaye May 05 '17 at 21:35
  • Also per @Rob comment (in addition to ^^^) I used the leading "/" at the beginning of the path to the resource. – Charney Kaye May 05 '17 at 21:38

1 Answers1

5

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

Community
  • 1
  • 1
dc95
  • 1,319
  • 1
  • 22
  • 44
  • +1 `getResourceAsStream` is it. This answer is very close to acceptable-as-correct... However I'd like to edit out the `mavenProject` reference and file-writing. The original question assumes that the file is manually included in `src/main/resources` and that would make the correct answer a little shorter (and less proprietary than this one) – Charney Kaye May 08 '17 at 18:20
  • @charneykaye removed the unnecessary `mavenProject` reference and file-writing. – dc95 May 08 '17 at 19:28