1

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

原始问题

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

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 阴影 .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)

使用爆炸的 .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 阴影配置。
注意:我认为 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 方法上取得成功,我会很感兴趣)。我有一个可行的替代方案,我将很快发布。但是,如果您愿意评论如何修复这个失败的 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/*");
      }
    }
4

2 回答 2

2

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

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

如何从我的 jar 文件中获取资源“文件夹”?

于 2016-02-10T19:41:59.683 回答
0

为了补充我的好朋友 Victor 的答案,这里有一个完整的代码解决方案。以下。完整的项目可在此处获得

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;
  }
}
于 2016-02-10T21:23:06.193 回答