从弹簧靴罐中读取

时间:2018-10-17 09:54:57

标签: maven file spring-boot executable-jar

我有以下的Spring Boot Jar结构

- bootstrap.yml
- org
- META-INF
  -- MANIFEST.MF
  -- Maven
     -- org.account.core
      -- account-service
       -- pom.properties
       -- pom.xml

现在,我想从同一jar中的Util类读取我的pom文件。下面的代码总是返回空。

public static String findFilePathInsideJar(String matchingFile) throws IOException {
        String path = null;
        File jarFile = new File(FileUtils.class.getProtectionDomain().getCodeSource().getLocation().getPath());
        if (jarFile.isFile()) {
            JarFile jar = new JarFile(jarFile);
            Enumeration<JarEntry> entries = jar.entries();
            while (entries.hasMoreElements()) {
                String name = entries.nextElement().getName();
                if (name.endsWith(matchingFile)) {
                    path = name;
                    break;
                }
            }
            jar.close();
        }
        return path;
 }

然后我像下面这样调用实用程序

String path = findFilePathInsideJar("pom.xml");

路径始终为空。

1 个答案:

答案 0 :(得分:0)

Path始终为null,因为jarFile.isFile() == false:)

Path也是无用的,因为以后您无法在JarFile之外阅读它,这是一个如何从当前Spring Boot pom.xml jar中读取uber的示例:

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) throws IOException {
        SpringApplication.run(DemoApplication.class, args);

        String currentJarPath = deduceCurrentJarPath();
        Optional<InputStream> pom = findFileInJar(currentJarPath, "pom.xml");

        // read pom.xml and print in console
        if (pom.isPresent()) {
            InputStream inputStream = pom.get();
            byte[] pomBytes = new byte[1024 * 1024];
            inputStream.read(pomBytes);
            inputStream.close();
            System.out.println(new String(pomBytes));
        }

    }

    private static String deduceCurrentJarPath() {
        String path = DemoApplication.class.getProtectionDomain().getCodeSource().getLocation().getPath();
        int endPathIndex = path.indexOf(".jar") + ".jar".length();
        int startPathIndex = "file:".length();
        return path.substring(startPathIndex, endPathIndex);
    }

    public static Optional<InputStream> findFileInJar(String jarPath, String fileName) {
        try (JarFile jarFile = new JarFile(new File(jarPath))) {
            Enumeration<JarEntry> jarEntries = jarFile.entries();
            while (jarEntries.hasMoreElements()) {
                JarEntry jarEntry = jarEntries.nextElement();
                String name = jarEntry.getName();
                if (name.endsWith(fileName)) {
                    InputStream inputStream = jarFile.getInputStream(jarEntry);
                    byte[] bytes = new byte[inputStream.available()];
                    inputStream.read(bytes);
                    return Optional.of(new ByteArrayInputStream(bytes));
                }
            }
            return Optional.empty();
        } catch (IOException e) {
            e.printStackTrace();
            return Optional.empty();
        }
    }

}
相关问题