jarアーカイブにあるファイルを開こうとしています。jarファイルを読む
私はこのMavenプラグインを使用し:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>com.fatec.migration.script.utils.Script</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>assemble-all</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
私が構築し、その後、私のアーカイブをコピーします。
cp /home/stephane/dev/java/projects/fatec/Fiabilisation_Controles_XP_AS/target/fatec-script-jar-with-dependencies.jar .
今、私はそれを実行してみてくださいすることができます
java -jar fatec-script-jar-with-dependencies.jar 1 2017-02-01 2017-02-02 all
ああ、それができませんファイルを見つける:
The file secure/extrapack-renew.sql could not be opened.
java.lang.NullPointerException
at com.fatec.migration.script.utils.AbstractScript.loadSqlStatement(AbstractScript.java:75)
しかし、ファイルが存在しない、私はアーカイブにそれを見ることができます。
protected String loadSqlStatement(String scriptPath, String filename) {
String filepath = buildSqlFilePath(scriptPath, filename);
try {
return new String(Files.readAllBytes(Paths.get(getClass().getResource(filepath).toURI())));
} catch (Exception e) {
System.err.println("The file " + filepath + " could not be opened.");
e.printStackTrace();
}
return null;
}
private String buildSqlFilePath(String scriptPath, String filename) {
return scriptPath + "/" + filename;
}
は、scriptPathは、「安全な」で、ファイル名:ここでは
$ jar -tvf target/fatec-script-jar-with-dependencies.jar | grep "secure/extrapack-renew.sql"
1844 Fri Feb 10 17:43:46 CET 2017 secure/extrapack-renew.sql
は、私は、ファイルを開いてみてください方法です"extrapack-renew.sql"です。
私には何が欠けていますか?
SQLファイルはjarファイル内にあります。移動や変更は想定されていません。ハードコーディングされているとしましょう。たぶん、私は外部ファイルでそれを持つ必要はありません、リソースファイルではるかに少ない。 – Stephane
実際には、jarがパッケージ化された後にファイルを変更する必要がない場合、jarからファイルを移動する義務はありません。むしろ展開の選択肢です。 – davidxxx