在 Spring Boot 应用程序中使用 JavaCompiler API - 无法正确设置编译类路径



我们有一个应用程序,它已经从传统的WAR Spring Web应用程序迁移到了我们希望的现代Spring Boot可执行文件Jar。

其中一个应用模块使用 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> 标签,但这似乎没有帮助,因为我无法掌握扩展的目录以将其添加到类路径中。

面临类似的问题。

看起来像弹簧靴的限制。

因此创建了一个嵌入了Jersey和Tomcat的独立应用程序。

现在jar包含所有库,并能够将其设置为Java编译器的类路径

最新更新