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

时间:2018-12-18 06:48:45

标签: java maven jar maven-plugin

我已经完成研究,但是我真的无法使它起作用。 我在Maven中使用Spring Boot。 Thymeleaf和jquery是我的前端。

我的目录:

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

TemporaryController内,我正在执行此操作:

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

因此,我可以访问MOCK_DATA.json目录下的resources

但是现在,我想将其包装到罐子中。这对我来说有点麻烦。这是我的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>

得到的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

如您所见,正在发生一些冗余,当我尝试运行jar文件时,它给了我java.io.FileNotFoundException: ./src/main/resourfces/MOCK_DATA.json (No such file or directory)

2 个答案:

答案 0 :(得分:1)

您不能将内部jar资源视为常规文件,您需要让classloader为您执行此操作,并且该路径将相对于jar的顶层(因此,您只想让classloader加载“ MOCK_DATA。 json”(不带任何路径)或/ resources文件夹。这样做是这样的:

Classpath resource within jar

顺便说一句。 / src / main / resources文件夹在maven中是自动的,除非您需要配置过滤等,然后再使用默认值:)

答案 1 :(得分:1)

如果您尝试从.jar中读取内容,那么

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

起作用。相反,您需要Class.getResourceAsStream()或同等的货币。

这里是一个例子:

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

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

以下是一些其他详细信息:How getClassLoader().getResourceAsStream() works in java

相关问题