従来のWAR Spring Webアプリケーションから、現代のSpring Boot実行可能ファイルJarに移行する予定のアプリケーションに移行しました。SpringブートアプリケーションでJavaCompiler APIを使用する - コンパイルクラスパスを正しく設定できません
アプリモジュールの1つはJavaCompiler
APIを使用してJavaコードを生成し、実行時にコンパイルします。
生成されたコードは、Webアプリケーションのクラスパスに存在する依存関係を必要とし、私たちはおおよそ次のコードを持っていた:ブートでは動作しない、しかし
StandardJavaFileManager standardFileManager = compiler.getStandardFileManager(null, null, null);
List<String> optionList = new ArrayList<String>();
// set compiler's classpath to be same as the runtime's
String classpathString = System.getProperty("java.class.path");
optionList.addAll(Arrays.asList("-nowarn",
"-classpath",
classpathString,
"-g",
"-source",
javaVersion,
"-target",
javaVersion));
Collection classpathFiles = getClasspathJars(classpathString);
addPreviousCompiledClassesToClasspathFiles(rootClassPath, classpathFiles);
try {
File file = new File(getTargetRoot(tempClassPath));
file.mkdirs();
standardFileManager.setLocation(StandardLocation.CLASS_OUTPUT, Lists.newArrayList(file));
standardFileManager.setLocation(StandardLocation.CLASS_PATH, classpathFiles);
// adding current dir to the source path, to find the compiled DV
if (generateSeparateJarsForEachDecision.equals(NO_JAR)) {
standardFileManager.setLocation(StandardLocation.SOURCE_PATH, Lists.newArrayList(file));
}
} catch (IOException e) {
throw new RuntimeException(e);
}
List<CharSequenceJavaFileObject> compilationUnits = Lists
.newArrayList(new CharSequenceJavaFileObject( StringUtils.capitalize(filename),
toString(javaCode)));
JavaCompiler.CompilationTask task = compiler
.getTask(writer, standardFileManager, listener, optionList, null, compilationUnits);
status = task.call();
`` `
を。 クラスパスにはmy-app.jar
しか含まれていません。ネストされたjarファイルをタスクの-cp
に追加することはできません。働いていたこれらの{absolute-path}/my-app.jar!/BOOT-INF/lib/my-dep.jar
{absolute-path}/my-app.jar!/BOOT-INF/lib/*.jar
なし: 私は手動でもそうのような-cp
パラメータ彼らに追加しようとしました。 <requiresUnpack>
タグをビルドに使用することについても考えましたが、クラスパスに追加するために展開されたディレクトリを保持できなかったため、役に立たなかったようです。