首页 > 解决方案 > 如何使用 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();
    }
}

标签: javavert.x

解决方案


推荐阅读