从maven shade插件创建的.jar在访问src / main / resources下的资源时会抛出错误,但是从爆炸的.jar运行main会有效吗?

时间:2016-02-10 09:05:23

标签: maven jar classpath maven-shade-plugin uberjar

更新了解决方案的执行摘要 根据Victor提供的答案,我实现了一个Java类,它列出了类路径中文件夹资源的内容。对我来说最关键的是,当从IDE执行时,从爆炸的uberjar或未爆炸的uberjar(我通常使用maven shade插件创建)中发现类路径资源时,这必须起作用。)类和相关单元测试可用here

原始问题

当我运行非常简单时,我看到了maven-shade-plugin和类路径资源的奇怪行为 java测试程序,它访问标准maven项目中的目录结构,如下所示:

src/main
    Test.java
    resources/
        resource-directory
            spark
                junk1
            zeppelin
                junk2

从IDE或爆炸的maven阴影.jar运行时(请参见下文) 它可以正常工作 ,这意味着它打印出来:。

result of directory contents as  classpath resource:[spark, zeppelin]

来源如下:

import org.apache.commons.io.IOUtils;
import java.io.IOException;
import java.io.InputStream;

public class Tester {
  public void test(String resourceName) throws IOException {
    InputStream in = this.getClass().getClassLoader().getResourceAsStream(resourceName);
    System.out.println("input stream: " + in);
    Object result = IOUtils.readLines(in);
    System.out.println("result of directory contents as  classpath resource:" + result);
  }
  public static void main(String[] args) throws IOException {
    new Tester().test("resource-directory");
  }
}

现在,如果我在我的项目中运行mvn clean install并运行
在$ {project.dir}目标下的maven shaded .jar,我看到以下异常:

> java -jar target/sample.jar 
Exception in thread "main" java.lang.NullPointerException
        at java.io.FilterInputStream.read(FilterInputStream.java:133)
        at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:284)
        at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:326)
        at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178)
        at java.io.InputStreamReader.read(InputStreamReader.java:184)
        at java.io.BufferedReader.fill(BufferedReader.java:161)
        at java.io.BufferedReader.readLine(BufferedReader.java:324)
        at java.io.BufferedReader.readLine(BufferedReader.java:389)
        at org.apache.commons.io.IOUtils.readLines(IOUtils.java:1030)
        at org.apache.commons.io.IOUtils.readLines(IOUtils.java:987)
        at org.apache.commons.io.IOUtils.readLines(IOUtils.java:968)
        at Tester.test(Tester.java:16)
        at Tester.main(Tester.java:24)

使用Exploded .jar

运行
> mkdir explode/
> cd explode/
> jar xvf ../sample.jar 
        ......
 inflated: META-INF/MANIFEST.MF
  created: META-INF/
            etc etc.

> ls      # look at contents of exploded .jar:
logback.xml  META-INF  org  resource-directory  Tester.class
#
#  now run class with CLASSPATH="."
(master) /tmp/maven-shade-non-working-example/target/explode > java Tester
input stream: java.io.ByteArrayInputStream@70dea4e
result of directory contents as  classpath resource:[spark, zeppelin]      # <<<-  works !

我在这里有整个项目:https://github.com/buildlackey/maven-shade-non-working-example 但是为了方便起见,这里是pom.xml(下面),我尝试了两个maven shade配置 注意:我不认为IncludeResourceTransformer会有任何用处,因为我的资源正在出现 在.jar文件中的适当级别。

<project xmlns="http://maven.apache.org/POM/4.0.0" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
  http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.foo.core</groupId>
  <artifactId>sample</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>sample</name>
  <url>http://maven.apache.org</url>

  <properties>
    <jdk.version>1.8</jdk.version>
    <junit.version>4.11</junit.version>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>${junit.version}</version>
      <scope>test</scope>
    </dependency>
      <dependency><!-- commons-io: Easy conversion  from stream to string list, etc.-->
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
        <version>2.4</version>
    </dependency>

  </dependencies>

  <build>
    <finalName>sample</finalName>
    <plugins>

      <!-- Set a compiler level -->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>2.3.2</version>
        <configuration>
          <source>${jdk.version}</source>
          <target>${jdk.version}</target>
        </configuration>
      </plugin>

    <!-- Maven Shade Plugin -->
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-shade-plugin</artifactId>
      <version>2.3</version>
      <executions>
         <!-- Run shade goal on package phase -->
        <execution>
      <phase>package</phase>
      <goals>
        <goal>shade</goal>
      </goals>
      <configuration>
        <transformers>
        <!-- add Main-Class to manifest file -->
                                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
          <mainClass>Tester</mainClass>
        </transformer>

        <!-- tried with the stanza below enabled, and also disabled:  in both cases, got exceptions from runs  -->
        <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                <resource>src/main/resources/</resource>
        </transformer>

        </transformers>
      </configuration>
          </execution>
      </executions>
    </plugin>

    </plugins>
  </build>

</project>

无论如何,提前感谢您提供的任何帮助〜     克里斯

更新

在我尝试使用Spring时,这对我来说并不适合(但是如果有人在Spring方法上取得成功,我会感兴趣)。我有一个工作替代方案,我将很快发布。但如果你想评论如何解决这个破碎的春季尝试,我会非常感兴趣。

    import org.springframework.core.io.Resource;
    import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
    import org.springframework.core.io.support.ResourcePatternResolver;

    import java.io.IOException;

    public class Tester {
      public void test(String resourceName) throws IOException {
        ResourcePatternResolver resourceResolver = new PathMatchingResourcePatternResolver();
        Resource[] resources = resourceResolver.getResources("resource-directory/*");
        for (Resource resource : resources) {
          System.out.println("resource: " + resource.getDescription());
        }
      }

      public static void main(String[] args) throws IOException {
        new Tester().test("resource-directory/*");
      }
    }

2 个答案:

答案 0 :(得分:2)

问题是getResourceAsStream只能从jar文件中读取文件作为流而不是文件夹。

要从jar文件中读取文件夹内容,您可能需要使用该方法,如此问题的接受答案中所述:

How can I get a resource "Folder" from inside my jar File?

答案 1 :(得分:0)

为了补充我的好朋友Victor的答案,这是一个完整的代码解决方案。下面。完整项目可用here

import java.io.File;
import java.io.IOException;
import java.util.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * List entries of a subfolder of an entry in the class path, which may consist of file system folders and .jars.
 */
public class ClassPathResourceFolderLister {

  private static final Logger LOGGER = LoggerFactory.getLogger(ClassPathResourceFolderLister.class);

  /**
   * For each entry in the classpath, verify that (a) "folder" exists, and (b) "folder" has child content, and if
   * these conditions hold,  return the child entries (be they files, or folders).  If neither (a) nor (b) are true for
   * a particular class path entry, move on to the next entry and try again.
   *
   * @param folder the folder to match within the class path entry
   *
   * @return the subfolder items of the first matching class path entry, with a no duplicates guarantee
   */
  public static Collection<String> getFolderListing(final String folder) {
    final String classPath = System.getProperty("java.class.path", ".");
    final String[] classPathElements = classPath.split(System.getProperty("path.separator"));
    List<String> classPathElementsList = new ArrayList<String> ( Arrays.asList(classPathElements));

    return getFolderListingForFirstMatchInClassPath(folder, classPathElementsList);
  }

  private static Collection<String>
  getFolderListingForFirstMatchInClassPath(final String folder, List<String> classPathElementsList) {
    if (LOGGER.isDebugEnabled()) {
      LOGGER.debug("getFolderListing for " + folder + " with classpath elements " + classPathElementsList);
    }

    Collection<String> retval = new HashSet<String>();
    String cleanedFolder = stripTrailingAndLeadingSlashes(folder);
    for (final String element : classPathElementsList) {
      System.out.println("class path element:" + element);
      retval = getFolderListing(element, cleanedFolder);

      if (retval.size() > 0) {
        if (LOGGER.isDebugEnabled()) {
          LOGGER.debug("found matching folder in class path list. returning: " + retval);
        }
        return retval;
      }
    }
    return retval;
  }

  private static String stripTrailingAndLeadingSlashes(final String folder) {
    String stripped = folder;

    if (stripped.equals("/")) {  // handle degenerate case:
        return "";
    } else { // handle cases for strings starting or ending with "/", confident that we have at least two characters
      if (stripped.endsWith("/")) {
        stripped = stripped.substring(0, stripped.length()-1);
      }
      if (stripped.startsWith("/")) {
        stripped = stripped.substring(1, stripped.length());
      }
      if (stripped.startsWith("/") || stripped.endsWith("/")) {
        throw new IllegalArgumentException("too many consecutive slashes in folder specification: " + stripped);
      }
    }

    return stripped;
  }

  private static Collection<String> getFolderListing( final String element, final String folderName) {
    final File file = new File(element);
    if (file.isDirectory()) {
      return getFolderContentsListingFromSubfolder(file, folderName);
    } else {
      return getResourcesFromJarFile(file, folderName);
    }
  }

  private static Collection<String> getResourcesFromJarFile(final File file, final String folderName) {
    final String leadingPathOfZipEntry = folderName + "/";
    final HashSet<String> retval = new HashSet<String>();
    ZipFile zf = null;
    try {
      zf = new ZipFile(file);
      final Enumeration e = zf.entries();
      while (e.hasMoreElements()) {
        final ZipEntry ze = (ZipEntry) e.nextElement();
        final String fileName = ze.getName();
        if (LOGGER.isTraceEnabled()) {
          LOGGER.trace("zip entry fileName:" + fileName);
        }
        if (fileName.startsWith(leadingPathOfZipEntry)) {
          final String justLeafPartOfEntry = fileName.replaceFirst(leadingPathOfZipEntry,"");
          final String initSegmentOfPath = justLeafPartOfEntry.replaceFirst("/.*", "");
          if (initSegmentOfPath.length() > 0) {
            LOGGER.trace(initSegmentOfPath);
            retval.add(initSegmentOfPath);
          }
        }
      }
    } catch (Exception e) {
      throw new RuntimeException("getResourcesFromJarFile failed. file=" + file + " folder=" + folderName, e);
    }  finally {
      if (zf != null) {
        try {
          zf.close();
        } catch (IOException e) {
          LOGGER.error("getResourcesFromJarFile close failed. file=" + file + " folder=" + folderName, e);
        }
      }
    }
    return retval;
  }

  private static Collection<String> getFolderContentsListingFromSubfolder(final File directory, String folderName) {
    final HashSet<String> retval = new HashSet<String>();
    try {
      final String fullPath = directory.getCanonicalPath() + "/" + folderName;
      final File subFolder = new File(fullPath);
      System.out.println("fullPath:" + fullPath);
      if (subFolder.isDirectory()) {
        final File[] fileList = subFolder.listFiles();
        for (final File file : fileList) {
          retval .add(file.getName());
        }
      }
    } catch (final IOException e) {
      throw new Error(e);
    }
    return retval;
  }
}