(new to gradle)
Java Program使用classloader getResourceAsStream函数从classpath读取属性文件。程序在eclipse中运行没有错误。执行distZip gradle任务生成的批处理文件会导致以下错误
Exception in thread "main" java.io.FileNotFoundException: property file 'prop.properties' not found in the classpath
at com.foo.bar.fb.utility.PropResourceCache.getProperties(PropResourceCache.java:26)
at com.foo.bar.fb.offline.RunOfflineProcess.main(RunOfflineProcess.java:40) Press any key to continue . . .
我认为这是因为清单文件中没有包含这些属性。有没有办法在程序的src / main / resources / Properties目录中包含所有属性文件?什么是最佳做法?
gradle build entry
jar.doFirst{
manifest {
attributes(
"Manifest-Version" : "1.0",
"Built-By" : System.getProperty('user.name'),
"Built-Date" : new Date(),
"Main-Class" : mainClassName,
"Class-Path" : configurations.runtime.collect{ "lib/"+it.getName() }.join(' ')
)
}
}
访问属性文件
public Properties getProperties(String propFileName) throws IOException {
String name = File.pathSeparator+"Properties"+File.separator+propFileName;
Properties props = new Properties();
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(name);
if (inputStream == null) {
throw new FileNotFoundException("property file '" + name
+ "' not found in the classpath");
}
props.load(inputStream);
return props;
}
答案 0 :(得分:2)
src/main/resources
(不是src/main/resource
)下的所有内容都会自动包含在Jar中。您可以通过解压缩Jar来验证这一点。