我们有一个应用程序已经从传统的WAR Spring Web应用程序迁移到我们希望的现代Spring Boot可执行文件罐。
其中一个app模块使用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();
```
但是,这不适用于Boot。
类路径只包含my-app.jar
,我不能将任何嵌套的jar添加到任务的-cp
- 它不会是那些嵌套的jar。
我还尝试手动添加-cp
参数,如下所示:{absolute-path}/my-app.jar!/BOOT-INF/lib/my-dep.jar
{absolute-path}/my-app.jar!/BOOT-INF/lib/*.jar
这些都没有奏效。
考虑在构建时使用<requiresUnpack>
标记,但这似乎没有帮助,因为我无法获得扩展的dir以便将其添加到类路径中。
答案 0 :(得分:0)
面临类似的问题。
看起来像弹簧靴的限制。
因此创建了一个嵌入了Jersey和Tomcat的独立应用程序。
现在jar包含所有库,并且能够将其设置为Java Compiler的类路径