![](/img/trans.png)
[英]Jar should not point at files within the project directory? Eclipse( Maven In Build)
[英]Access files within resource directory in maven generated jar
我已經完成了研究,但是我真的無法使它起作用。 我在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>>() {});
這樣,我就可以訪問resources
目錄下的MOCK_DATA.json
。
但是現在,我想將其包裝到罐子中。 這對我來說有點麻煩。 這是我的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)
您不能將內部jar資源視為常規文件,您需要讓classloader為您執行此操作,並且該路徑將相對於jar的頂層(因此,您只想讓classloader加載“ MOCK_DATA.json”任何路徑)或/ resources文件夾。 這是怎么做的:
順便說一句。 / src / main / resources文件夾在maven中是自動的,除非您需要配置過濾等,否則默認為:)
如果您嘗試讀取.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.getClass().getResourceAsStream(MY-RESOURCE);
BufferedReader br = new BufferedReader(new InputStreamReader(is));
...
is.close();
} catch(Exception e) {
System.out.println(e);
}
以下是一些其他詳細信息: getClassLoader()。getResourceAsStream()如何在Java中工作
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.