首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Spring Boot应用程序中使用JavaCompiler应用程序接口-无法正确设置编译类路径

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

Stack Overflow用户
提问于 2017-10-24 02:57:02
回答 1查看 544关注 0票数 6

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

其中一个应用程序模块使用Java来生成JavaCompiler代码并在运行时编译它。

生成的代码需要驻留在web应用程序类路径中的依赖项,因此我们有大致以下代码:

代码语言:javascript
复制
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();
代码语言:javascript
复制

然而,这并不适用于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>标记,但这似乎没有什么帮助,因为我无法获得扩展目录以便将其添加到类路径中。

EN

回答 1

Stack Overflow用户

发布于 2017-11-17 15:36:32

面临着类似的问题。

看起来像是spring boot的一个限制。

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

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46896593

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档