简体   繁体   English

访问Maven生成的jar中资源目录中的文件

[英]Access files within resource directory in maven generated jar

I've done my research, but I just really can't get it to work. 我已经完成了研究,但是我真的无法使它起作用。 I'm using Spring Boot with Maven. 我在Maven中使用Spring Boot。 Thymeleaf and jquery for my frontend. Thymeleaf和jquery作为我的前端。

My directory: 我的目录:

project-system/
├── mvnw
├── mvnw.cmd
├── pom.xml
├── src
│   ├── main
│   │   ├── java
│   │   │   └── rigor
│   │   │       └── io
│   │   │           └── projectsystem
│   │   │               ├── rush
│   │   │               │   ├── TemporaryController.java
│   │   │               └── ProjectSystemApplication.java
│   │   └── resources
│   │       ├── application.properties
│   │       ├── MOCK_DATA.json
│   │       ├── static
│   │       └── templates
│   │           ├── index.html
│   │           └── records.html

Inside TemporaryController , I'm doing this operation: TemporaryController内部,我正在执行以下操作:

list = new ObjectMapper().readValue(new File("./src/main/resources/MOCK_DATA.json"), new TypeReference<List<POJO>>() {});

So with this, I'm able to access the MOCK_DATA.json under the resources directory. 这样,我就可以访问resources目录下的MOCK_DATA.json

But now, I want to package it into a jar. 但是现在,我想将其包装到罐子中。 This is proving a bit troublesome for me. 这对我来说有点麻烦。 Here's my build in my pom file: 这是我的pom文件中的构建:

<build>
    <plugins>
        <plugin>
            <artifactId>maven-resources-plugin</artifactId>
            <version>2.6</version>
            <executions>
                <execution>
                    <id>copy-resources</id>
                    <phase>validate</phase>
                    <goals>
                        <goal>copy-resources</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${basedir}/target/classes/src/main</outputDirectory>
                        <includeEmptyDirs>true</includeEmptyDirs>
                        <resources>
                            <resource>
                                <directory>${basedir}/src/main/</directory>
                                <filtering>false</filtering>
                            </resource>
                        </resources>
                    </configuration>
                </execution>
            </executions>
        </plugin>

        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <classpathPrefix>lib/</classpathPrefix>
                        <mainClass>rigor.io.projectsystem.ProjectSystemApplication</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
    </plugins>
</build>

And the resulting target directory this gives me is: 得到的target目录为:

├── target
│   ├── classes
│   │   ├── application.properties
│   │   ├── MOCK_DATA.json
│   │   ├── rigor
│   │   │   └── io
│   │   │       └── projectsystem
│   │   │           ├── rush
│   │   │           │   ├── TemporaryController.class
│   │   │           └── ProjectSystemApplication.class
│   │   ├── src
│   │   │   └── main
│   │   │       ├── java
│   │   │       │   └── rigor
│   │   │       │       └── io
│   │   │       │           └── projectsystem
│   │   │       │               ├── rush
│   │   │       │               │   ├── TemporaryController.java
│   │   │       │               └── ProjectSystemApplication.java
│   │   │       └── resources
│   │   │           ├── application.properties
│   │   │           ├── MOCK_DATA.json
│   │   │           ├── static
│   │   │           └── templates
│   │   │               ├── index.html
│   │   │               └── records.html
│   │   └── templates
│   │       ├── index.html
│   │       └── records.html
│   ├── generated-sources
│   │   └── annotations
│   ├── generated-test-sources
│   │   └── test-annotations
│   ├── maven-archiver
│   │   └── pom.properties
│   ├── maven-status
│   │   └── maven-compiler-plugin
│   │       ├── compile
│   │       │   └── default-compile
│   │       │       ├── createdFiles.lst
│   │       │       └── inputFiles.lst
│   │       └── testCompile
│   │           └── default-testCompile
│   │               ├── createdFiles.lst
│   │               └── inputFiles.lst
│   ├── surefire-reports
│   │   ├── rigor.io.projectsystem.ProjectSystemApplicationTests.txt
│   │   └── TEST-rigor.io.projectsystem.ProjectSystemApplicationTests.xml
│   ├── test-classes
│   │   └── rigor
│   │       └── io
│   │           └── projectsystem
│   │               ├── ProjectSystemApplicationTests$1.class
│   │               └── ProjectSystemApplicationTests.class
│   ├── project-system-0.0.1-SNAPSHOT.jar
│   └── project-system-0.0.1-SNAPSHOT.jar.original

As you can see, there are some redundancies that are happening, and when I try to run the jar file, it gives me a java.io.FileNotFoundException: ./src/main/resourfces/MOCK_DATA.json (No such file or directory) 如您所见,正在发生一些冗余,当我尝试运行jar文件时,它给了我一个java.io.FileNotFoundException: ./src/main/resourfces/MOCK_DATA.json (No such file or directory)

You cannot treat internal jar resource as a regular file, you need to ask classloader to do it for you, and the path would be relative to top level of the jar (so you want just to ask classloader to load "MOCK_DATA.json" without any paths), or to /resources folder. 您不能将内部jar资源视为常规文件,您需要让classloader为您执行此操作,并且该路径将相对于jar的顶层(因此,您只想让classloader加载“ MOCK_DATA.json”任何路径)或/ resources文件夹。 This is how to do it: 这是怎么做的:

Classpath resource within jar jar中的类路径资源

BTW. 顺便说一句。 /src/main/resources folder is automatic in maven unless you need to configure filtering etc other then defaults :) / src / main / resources文件夹在maven中是自动的,除非您需要配置过滤等,否则默认为:)

If you're trying to read from a .jar, then 如果您尝试读取.jar,则

new File("./src/main/resources/MOCK_DATA.json"

will NOT work. 无法正常工作。 Instead, you need Class.getResourceAsStream() or equivalent. 相反,您需要Class.getResourceAsStream()或同等功能。

Here is an example: 这是一个例子:

https://www.tutorialspoint.com/java/lang/class_getresourceasstream.htm https://www.tutorialspoint.com/java/lang/class_getresourceasstream.htm

  ...
  try {
     // input stream
     InputStream is = this.getClass().getResourceAsStream(MY-RESOURCE);
     BufferedReader br = new BufferedReader(new InputStreamReader(is));
    ...
     is.close();
  } catch(Exception e) {
     System.out.println(e);
  }

Here are some additional details: How getClassLoader().getResourceAsStream() works in java 以下是一些其他详细信息: getClassLoader()。getResourceAsStream()如何在Java中工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM