简体   繁体   English

相对于最终Jar的Maven Shade外部属性文件

[英]Maven Shade External Properties file relative to final Jar

Hi all!! 大家好!!

Sorry if this is a dumb question, but I'm new to Maven and have reached an impasse! 抱歉,这是一个愚蠢的问题,但是我是Maven的新手,已经陷入僵局!

  1. I have a project which uses MyTest.properties, MoreMyTest.properties. 我有一个使用MyTest.properties,MoreMyTest.properties的项目。
  2. I use the Maven Shade plugin to build this project into a .jar file, which is working fine! 我使用Maven Shade插件将此项目构建到.jar文件中,效果很好! Unfortunately, the shade plugin is packaging MyTest.properties inside my jar file. 不幸的是,shadow插件将MyTest.properties打包在我的jar文件中。

Then, I get this error when I try to execute the jar. 然后,当我尝试执行jar时出现此错误。

java.io.FileNotFoundException: Unable to locate: properties/MyTest.properties at file:\\C:\\Dev\\test.jar!\\properties\\MyTest.properties at ... java.io.FileNotFoundException:无法找到:文件:\\ C:\\ Dev \\ test.jar!\\ properties \\ MyTest.properties中的properties / MyTest.properties ...

I'm wanting to access my properties both inside my IDE, and by running the jar from the command line when I deploy. 我想访问我的IDE中的属性,以及通过在部署时从命令行运行jar的方式。 I was thinking of having my properties files in a relative folder ../lib/ above the location of my jar file. 我当时正在考虑将我的属性文件保存在jar文件位置上方的相对文件夹../lib/中。

Inside my program I want to access the properties like so: 在我的程序中,我想像这样访问属性:

File testProperties = new File(
  ClassLoader.getSystemResource("properties/MyTest.properties").getFile()
);

I tried adding this to my POM.xml 我尝试将其添加到我的POM.xml

<transformer
    implementation="org.apache.maven.plugins.shade.resource.IncludeResourceTransformer">
  <resource>*.properties</resource>
  <file>../*properties</file>
</transformer>

(Sorry the formatting got messed on the above code snippet!) (很抱歉,上述代码段的格式混乱!)

But, it's not working. 但是,它不起作用。 I'd really appreciate any help in this. 我真的很感谢任何帮助。 I've not posted all code as the code is really big, but I hope you can get an idea of what I'm trying to achieve. 由于代码很大,因此我并未发布所有代码,但是希望您能了解我要实现的目标。

Many thanks, Paul 非常感谢,保罗

Ok, I have a solution. 好的,我有解决方案。 Always the way, just after posting! 发布后就一直如此! But thanks for your reply. 但是,谢谢您的答复。

  <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>../lib</outputDirectory>
                        <resources>
                            <resource>
                                <directory>src/main/resources</directory>
                            </resource>
                        </resources>
                    </configuration>
                </execution>
            </executions>
        </plugin>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM