Java getClass()。getClassLoader()。getResource(path)在Maven shaded .jar

时间:2017-05-05 18:38:45

标签: java maven jar maven-shade-plugin maven-jar-plugin

使用java直接运行我的项目时,我可以确认某个特定方法是否能够通过 getClass()获取资源.getClassLoader()。getResource(path),其中路径docs/info.mdsrc/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>

1 个答案:

答案 0 :(得分:2)

使用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