我们有一个应用程序,它已经从传统的WAR Spring web应用程序迁移到我们希望成为现代Spring Boot可执行Jar的应用程序。
其中一个应用程序模块使用Java来生成JavaCompiler代码并在运行时编译它。
生成的代码需要驻留在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,并且我不能将任何嵌套的jars添加到任务的-cp中-它不会添加那些嵌套的jars。我还尝试像这样手动将添加到-cp参数中:{absolute-path}/my-app.jar!/BOOT-INF/lib/my-dep.jar {absolute-path}/my-app.jar!/BOOT-INF/lib/*.jar
这些都不管用。我也考虑过在构建时使用<requiresUnpack>标记,但这似乎没有什么帮助,因为我无法获得扩展目录以便将其添加到类路径中。
发布于 2017-11-17 15:36:32
面临着类似的问题。
看起来像是spring boot的一个限制。
因此创建了一个嵌入了Jersey和Tomcat的独立应用程序。
现在,jar包含了所有的库,并且能够将其设置为Java编译器的类路径
https://stackoverflow.com/questions/46896593
复制相似问题