java - 如何使用 vertx:package 将资源文件添加到 fat jar
问题描述
我正在尝试为我的项目构建一个胖 jar 文件。它构建得很好,但是我在资源文件夹中的配置文件找不到。
这是我的 pom.xml
<properties>
<maven.compiler.target>1.8</maven.compiler.target>
<vertx.verticle>com.vertx.MainVerticle</vertx.verticle>
<maven.compiler.source>1.8</maven.compiler.source>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<vertx.version>3.4.0</vertx.version>
<fabric8-vertx-maven-plugin.version>1.0.5</fabric8-vertx-maven-plugin.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-dependencies</artifactId>
<version>${vertx.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-core</artifactId>
</dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-web</artifactId>
</dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-mysql-postgresql-client</artifactId>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.0</version>
</dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-web-client</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>io.fabric8</groupId>
<artifactId>vertx-maven-plugin</artifactId>
<version>${fabric8-vertx-maven-plugin.version}</version>
<configuration>
<redeploy>true</redeploy>
</configuration>
</plugin>
</plugins>
</build>
下面是我的资源文件夹结构
缺少的文件是 config.json。它包含我的班级使用的值。但是,当我运行 jar 文件时,我得到了错误
java.nio.file.NoSuchFileException
这是加载文件的方式
public static JsonObject config() {
try {
if (jsonObj != null)
return jsonObj;
ClassLoader classloader = Thread.currentThread().getContextClassLoader();
String jsonStr = new String(Files.readAllBytes(Paths.get(classloader.getResource("config.json").getPath())));
return new JsonObject(jsonStr);
} catch (IOException e) {
e.printStackTrace();
return new JsonObject();
}
}
解决方案
推荐阅读
- c# - Entity Framework Core:在没有外部实体的情况下坚持一对多
- javascript - 尝试将数组项传递给 html 表单中的 getElementById() 选择选项
- python - Python csv.DictReader 内存不足
- excel - 打开多个 Excel 实例时如何通过 Excel 宏将数据从 Excel 导出到 Access
- wso2-am - API Manager 2.6.0 无法启动——找不到 carbon.xml 文件,但安装成功
- function - JS - 定义显示煎茶窗口功能
- c - 在 C 中通过数组名称和指针访问二维数组元素
- php - Wordpress 更新 5.0.2 删除了 WPBakery 插件模板
- mysql - SQL 在服务器上与 phpmyadmin 一起工作,但在 XAMPP 中不工作
- django - Django 表单 - 有没有办法动态编辑 FilePathField?