如何在$ project--0.0.1-SNAPSHOT.jar中打包config.yml和资源 [英] How to package config.yml and resources in $project--0.0.1-SNAPSHOT.jar

查看:47
本文介绍了如何在$ project--0.0.1-SNAPSHOT.jar中打包config.yml和资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个拖放向导项目,该项目具有以下目录结构:

I've a drop wizard project which has the following directory structure:

basedir
    pom.xml
    config.yml
    src
     main
      resources
       myresource.xml
The build portion of 
<build>
    <finalName>project-${version}</finalName>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>2.1</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <transformers>
                            <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                <mainClass>project-package.App</mainClass>
                            </transformer>
                            <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer">
                            </transformer>
                        </transformers>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

当我从Eclipse运行代码时,我使用如下命令:
$ mypackage.App服务器config.yml
它工作正常,我可以在资源目录下读取文件.当我使用mvn进行构建时,在创建的快照jar中,它不包含config.yml,即使"basedir"中的snapshot.jar中包含"myresource.xml"的路径也丢失了.因此,当我从jar文件运行它时,我的代码没有将其作为资源.我应该如何将pom.xml更改为包含config.yml并将资源目录打包为可以作为资源访问的方式.

When I run my code from Eclipse, I use command as follows:
$mypackage.App server config.yml
It works fine and I can read the file under resources directory. When I use mvn to build, in the snapshot jar created, it does not include config.yml and the path to "myresource.xml" is missing even though it is included in the snapshot.jar in the basedir. So, when I run it from my jar file, my code does not find it as resources. How should I change the pom.xml to include config.yml and package the resource directory such a way that it is accessible as resource.

推荐答案

真的很奇怪

this.getClass().getResource("myresource.xml")

在IDE中工作,但在jar中运行时则无效.

does work in IDE but NOT when you run it from a jar.

this.getClass().getResourceAsStream("myresource.xml") 

在IDE内部和jar中均可工作.

works both inside IDE and from jar.

这可以解决资源问题.但是,我需要一个有关如何打包config.yml文件的答案.除了jar文件外,我是否应该另外提供?

That resolves the resources issue. However, I need an answer for how to package config.yml file. Should I supply separately in addition to the jar file?

这篇关于如何在$ project--0.0.1-SNAPSHOT.jar中打包config.yml和资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆