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

从maven shade插件创建的.jar在访问src/main/resources下的资源时抛出错误,但是从exploded.jar运行main是否有效?,maven,jar,classpath,maven-shade-plugin,uberjar,Maven,Jar,Classpath,Maven Shade Plugin,Uberjar,更新了解决方案的执行摘要 根据Victor提供的答案,我实现了一个Java类,它在类路径中列出文件夹资源的内容。对我来说,最关键的是,当从IDE、分解的uberjar或未分解的uberjar(我通常使用maven shade插件创建)中执行时发现类路径资源时,这必须起作用。类和相关的单元测试可用 原始问题 当我运行simple时,我看到maven shade插件和类路径资源的奇怪行为 在标准maven项目中访问目录结构的java测试程序,如下所示: src/main Test.java

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

原始问题

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

src/main
    Test.java
    resources/
        resource-directory
            spark
                junk1
            zeppelin
                junk2
从IDE或分解的maven shaded.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并运行 maven shaded.jar在${project.dir}目标下,我看到以下异常:

> 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运行


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

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


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

导入java.io.File;
导入java.io.IOException;
导入java.util.*;
导入java.util.zip.ZipEntry;
导入java.util.zip.ZipException;
导入java.util.zip.ZipFile;
导入org.slf4j.Logger;
导入org.slf4j.LoggerFactory;
/**
*列出类路径中某个条目的子文件夹的条目,该子文件夹可能由文件系统文件夹和.jar组成。
*/
公共类ClassPathResourceFolderLister{
私有静态最终记录器Logger=LoggerFactory.getLogger(ClassPathResourceFolderLister.class);
/**
*对于类路径中的每个条目,验证(a)“文件夹”是否存在,以及(b)“文件夹”是否包含子内容,如果
*这些条件保持不变,返回子条目(无论是文件还是文件夹)。如果(a)或(b)对于
*如果要创建特定的类路径条目,请转到下一个条目,然后重试。
*
*@param folder类路径条目中要匹配的文件夹
*
*@返回第一个匹配类路径项的子文件夹项,并保证不重复
*/
公共静态集合GetFolderList(最终字符串文件夹){
最后一个字符串classPath=System.getProperty(“java.class.path”,”);
最后一个字符串[]classPathElements=classPath.split(System.getProperty(“path.separator”);
List classPathElementsList=新的ArrayList(Arrays.asList(classPathElements));
返回getFolderListingForFirstMatchInClassPath(文件夹,classPathElementsList);
}
私有静态集合
getFolderListingForFirstMatchInClassPath(最终字符串文件夹,列表类路径元素列表){
if(LOGGER.isDebugEnabled()){
debug(“getFolderListing for“+文件夹+”和类路径元素“+类路径元素列表”);
}
Collection retval=new HashSet();
String cleaned folder=stripTrailingAndLeadingSlashes(文件夹);
for(最后一个字符串元素:classPathElementsList){
System.out.println(“类路径元素:“+元素”);
retval=getFolderList(元素,cleanedFolder);
如果(retval.size()>0){
if(LOGGER.isDebugEnabled()){
debug(“在类路径列表中找到匹配的文件夹。返回:”+retval);
}
返回返回;
}
}
返回返回;
}
私有静态字符串stringtrailingandleadingslashes(最终字符串文件夹){
字符串=文件夹;
如果(stripped.equals(“/”){//处理退化情况:
返回“”;
}else{//处理以“/”开头或结尾的字符串的大小写,确信至少有两个字符
if(带(“/”)的条带.endsWith){
stripped=stripped.substring(0,stripped.length()-1);
}
if(带(“/”)的剥离开始){
stripped=stripped.substring(1,stripped.length());
}
if(带(“/”)的剥离.开始|带(“/”)的剥离.结束){
抛出新的IllegalArgumentException(“文件夹规范中连续斜杠太多:“+stripped”);
}
}
返回剥离;
}
私有静态集合GetFolderList(最终字符串元素,最终字符串folderName){
最终文件=新文件(元素);
if(file.isDirectory()){
返回getFolderContentsListingFromSubfolder(文件,folderName);
}否则{
返回getResourcesFromJarFile(文件,文件夹名);
}
}
私有静态集合getResourcesFromJarFile(最终文件文件,最终字符串folderName){
最终字符串leadingPathFziPentry=folderName+“/”;
final HashSet retval=新HashSet();
ZipFile zf=null;
试一试{
zf=新ZipFile(文件);
最终枚举e=zf.entries();
而(e.hasMoreElements()){
最终ZipEntry ze=(ZipEntry)e.nextElement();
最终字符串fileName=ze.getName();
if(LOGGER.isTraceEnabled()){
trace(“zip条目文件名:”+fileName);
}
if(fileName.startsWith(leadingPathFziPentry)){
最后一个字符串justLeafPartOfEntry=fileName.replaceFirst(导致pathofzipentry,“”);
最后一个字符串initSegmentOfPath=justLeafPartOfEntry.replaceFirst(“/.*”,“”);
if(initSegmentOfPath.length()>0){
trace(initSegmentOfPath);
retval.add(initSegmentOfPath);
}
}
}
}捕获(例外e){
抛出新的RuntimeException(“getResourcesFromJarFile失败。file=“+file+”文件夹=“+folderName,e”);
> 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 !
<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>
    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/*");
      }
    }
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;
  }
}