简体   繁体   English

Gradle和src / main / resources

[英]Gradle and src/main/resources

I have a gradle project which is working fine within eclipse. 我有一个gradle项目,在Eclipse中运行良好。 In the java code I have 在Java代码中

Properties p = MyClass.class.getClassLoader.getResourceAsStream("myProps.properties");

File f = new File(p.getProperty("myFile"));

My structure is: 我的结构是:

src/main/java - has java files
src/main/resources - has myProps.properties and myFile.txt

myProps.properties has: myProps.properties具有:

myFile=src/main/resources/myFile.txt

If I do a gradle build (using a default shadowJar task) my resources have now been moved from src/main/resources to the root of the jar file. 如果我执行gradle构建(使用默认的shadowJar任务),我的资源现在已从src / main / resources移到jar文件的根目录。

shadowJar {
   manifest{
     // The only thing I modify is the attributes
   }
}

When I execute the jar by running 当我通过运行执行罐子时
"java -jar myJar.jar" I get FileNotFound exception referring to the myFile.txt resource file. “ java -jar myJar.jar”我得到FileNotFound异常,它引用了myFile.txt资源文件。

What is the correct way to fix this issue so that the code works from both the Eclipse and executable jar environments? 解决此问题以使代码在Eclipse和可执行jar环境中均可工作的正确方法是什么?

I needed to read the file as a stream (the same way I read in the properties file). 我需要将文件读取为流(与读取属性文件的方式相同)。 I removed 'src/main/resources' from the path in the properties file and it worked 我从属性文件的路径中删除了“ src / main / resources”,它可以正常工作

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

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