Jhipster罐中没有资源
我有一个带有版本的Jhipster Spring Boot项目:
- Java:1.8
- JHIPSTER:3.0.3
- Spring Boot:2.1.6.Release
它与War Package效果很好。我将战争更改为jar软件包,它可以在Intellij中运行,但是使用命令行java -jar myproject.jar.jar
资源文件在运行时找不到。
如何在Jhipster Jar文件中获取资源文件?
这是pom.xml的更改:
<packaging>jar</packaging>
...
<build>
<defaultGoal>spring-boot:run</defaultGoal>
<resources>
<resource>
<directory>${project.basedir}/src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>**/*.json</include>
<include>config/*.yml</include>
</includes>
</resource>
...
</resources>
...
</build>
在Java文件中,我使用Defaultresourceloader加载资源:
DefaultResourceLoader resourceLoader = new DefaultResourceLoader();
Resource resource = resourceLoader.getResource("classpath:config/");
try(
Reader reader = new InputStreamReader(resource.getInputStream(), StandardCharsets.UTF_8);
BufferedReader br = new BufferedReader(reader)) {
br.lines().forEach(fileName -> {
LOGGER.info("Update config: {}", fileName);
... 在命令行模式下,信息“ Update Config:XXX”永远不会记录。
资源文件在目录中: $ {project.basedir}/src/main/resources
我还尝试在POM中使用复制资源添加资源,但我无法获取资源文件。
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>${maven-resources-plugin.version}</version>
<executions>
<execution>
<id>default-resources</id>
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/classes</outputDirectory>
<resources>
<resource>
<directory>${project.basedir}/src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>config/*.yml</include>
<include>**/*.json</include>
</includes>
</resource>
</resources>
</execution>
</executions>
</plugin>
...
</plugins>
</pluginManagement>
更新 正如 @gaëlmarziou所提到的,我在.jar.original中找到了资源。因此,问题在我的Java代码中。 要在JAR中的目录中列出所有资源文件,我应该使用资源[]。这是校正的代码:
@Value("classpath:config/*")
private Resource[] configFiles;
...
Arrays.stream(configFiles).map(file -> file.getFilename()).forEach(System.out::println);
I have a Jhipster Spring Boot project with version:
- Java: 1.8
- Jhipster: 3.0.3
- Spring Boot: 2.1.6.RELEASE
It worked well with WAR package. I changed WAR to JAR package, it can run in IntelliJ, but with command line java -jar myproject.jar
the resources files can't be found at run time.
How to get resources files in Jhipster jar file?
Here are the changes in the pom.xml:
<packaging>jar</packaging>
...
<build>
<defaultGoal>spring-boot:run</defaultGoal>
<resources>
<resource>
<directory>${project.basedir}/src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>**/*.json</include>
<include>config/*.yml</include>
</includes>
</resource>
...
</resources>
...
</build>
In a Java file, I use DefaultResourceLoader to load resources:
DefaultResourceLoader resourceLoader = new DefaultResourceLoader();
Resource resource = resourceLoader.getResource("classpath:config/");
try(
Reader reader = new InputStreamReader(resource.getInputStream(), StandardCharsets.UTF_8);
BufferedReader br = new BufferedReader(reader)) {
br.lines().forEach(fileName -> {
LOGGER.info("Update config: {}", fileName);
...
On command line mode, the info "Update config: xxx" is never logged.
The resources files are in the directory:${project.basedir}/src/main/resources
I also tried to add resources with copy-resources in the pom, but I couldn't get resources files.
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>${maven-resources-plugin.version}</version>
<executions>
<execution>
<id>default-resources</id>
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/classes</outputDirectory>
<resources>
<resource>
<directory>${project.basedir}/src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>config/*.yml</include>
<include>**/*.json</include>
</includes>
</resource>
</resources>
</execution>
</executions>
</plugin>
...
</plugins>
</pluginManagement>
Update
As mentioned by @GaëlMarziou, I found resources in .jar.original. So the problem is in my Java code.
To list all resource files in a directory in the jar, I should use Resource[]. Here is the corrected code:
@Value("classpath:config/*")
private Resource[] configFiles;
...
Arrays.stream(configFiles).map(file -> file.getFilename()).forEach(System.out::println);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论