从 spring 引导 jar 中读取

Read from spring boot jar

我有以下 spring 引导 jar 结构

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

现在我想从同一个 jar 中的 Util class 读取我的 pom 文件。下面的代码总是 returns 为空。

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");

路径始终为空。

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

另外Path没用,因为你以后不能在JarFile外面读取它,这里有一个如何从当前Spring引导读取pom.xml的例子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();
        }
    }

}