如何访问jar中的资源,它可以存在于多个jar中 [英] How to access resources in jar where it can be present in multiple jar

查看:131
本文介绍了如何访问jar中的资源,它可以存在于多个jar中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个项目,我可以针对许多XSD生成大量代码。为了使事物分离,每组XSD在项目中捆绑在一起。我有多个项目会在资源中看到XSD并生成针对它们的代码。

I have a project where I generate lots of code against many XSD. To keep things separate each set of XSD are bundled together within a project. I have multiple project that will see XSD in resources and generate code against them.

我的问题是当我尝试访问存储在jar文件中的XSD时我不能获取代码从一个特定的jar访问XSD。相反,它将访问与标准匹配的第一个XSD而不管jar。

My problem is when I try to access the XSD that are stored in the jar files I cannot get the code to access the XSD from a perticular jar. Instead it will access the first XSD that matches the criterion regardless of the jar.

这是我用来列出资源的代码,所有的jar都具有相同的结构含义XSD始终存储在jar文件根目录的xsd文件夹中。下面的代码列出了文件夹中的XSD。

Here is the code I use to list the ressources, All the jars have the same structure meaning the XSDs are always stored in the xsd folder at the root of the jar file. The code below lists the XSD in the folder.

URL dirURL = clazz.getClassLoader().getResource(path);
  System.out.println(dirURL.toURI());
  if (dirURL != null && dirURL.getProtocol().equals("file")) {
   /* A file path: easy enough */
   System.out.println(dirURL.toURI());
  return new File(dirURL.toURI()).list();
  }

  if (dirURL == null) {
   /* 
    * In case of a jar file, we can't actually find a directory.
    * Have to assume the same jar as clazz.
    */
   String me = clazz.getName().replace(".", "/") + ".class";
   dirURL = clazz.getClassLoader().getResource(me);
   System.out.println(dirURL.toURI());
   System.out.println(me);
  }

  if (dirURL.getProtocol().equals("jar")) {
   /* A JAR path */
   String jarPath = dirURL.getPath().substring(5, dirURL.getPath().indexOf("!")); //strip out only the JAR file
   System.out.println(jarPath);
   JarFile jar = new JarFile(URLDecoder.decode(jarPath, "UTF-8"));
   Enumeration<JarEntry> entries = jar.entries(); //gives ALL entries in jar
   Set<String> result = new HashSet<String>(); //avoid duplicates in case it is a subdirectory
   String name = null;
   while (entries.hasMoreElements()) {
    name = entries.nextElement().getName();
    if (name.startsWith(path)) { //filter according to the path
     String entry = name.substring(path.length());
     int checkSubdir = entry.indexOf("/");
     if (checkSubdir >= 0) {
      // if it is a subdirectory, we just return the directory name
      entry = entry.substring(0, checkSubdir);
     }
     result.add(entry);
    }
   }
   return result.toArray(new String[result.size()]);


推荐答案

我通常会规定添加资源目录使用其下的JAR所特有的资源进入每个JAR。例如(在Maven结构中)

I usually make it a rule to add a resource directory into each JAR with resources that are unique to that JAR held under it. For example (in the Maven structure)

module1/src/main/resources/module1/example.xsd
module2/src/main/resources/module2/example.xsd

然后使用

The XSDs are then referenced using

InputStream module1XSD= SomeClass.class.getResourceAsStream("/module1/example.xsd");
InputStream module2XSD= SomeClass.class.getResourceAsStream("/module2/example.xsd");

只要module1和module2的JAR放在包含SomeClass的应用程序的类路径上。

so long as the JARs for module1 and module2 have been placed on the classpath of the application containing SomeClass.

Spring上下文会将这些引用为

Spring contexts would reference these as

classpath:module1/example.xsd,
classpath:module2/example.xsd

这意味着你会必须能够在您生成的JAR中移动XSD的位置。甚至可以通过构建脚本重新生成它们。

This does mean that you'll have to be able to move the location of XSDs in the JARs that you generate. Maybe even regenerating them through a build script.

这篇关于如何访问jar中的资源,它可以存在于多个jar中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆