(グラドル初心者)
Java プログラムは、クラスローダーの getResourceAsStream 関数を使用して、クラスパスからプロパティ ファイルを読み取ります。プログラムはエラーなしで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 ビルド エントリ
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;
}