首页 文章

Intellij IDEA Java类不会在保存时自动编译

提问于
浏览
144

昨天我从Eclipse切换到Intellij .

我也在使用jRebel和Websphere Server 7 .

现在一切似乎都工作得很好,除了 when I modify 一个Java文件,并且 hit save ,Intellij does not 重新编译该文件,以便jRebel获取它 .

日食“ Build Automatically ”功能解决了这个问题 .

在Intellij中,我必须按 CTRL + SHIFT + 9 重新编译jRebel的相关类才能获取它 . 如果在 two files 之间进行了更改,那么我有 to do this on each and one of ,因为Intellij使用了save all机制,所以很难知道手动重新编译的内容,我也不感兴趣 .

有没有办法让Intellij到 do this on its own

15 回答

  • 64

    我收到错误:一些 jar 不在classpath.So我只是删除损坏的jar并在步骤下执行

    1.Project >  Setting>Build,Execution,Deployment>Compiler>check build project automatically
    2.CTRL+SHIFT+A find/search **registry** --Check for below param
    compiler.automake.allow.when.app.running
    compiler.automake.trigger.delay=500---According to ur requirement
    3.Add devtool in pom.xml
             <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-devtools</artifactId>
                <optional>true</optional>
            </dependency>
    4.Build ,If found any probelm while building ,saying some jar in not in class path.Just delete the corrupted jar
    and re-build the project angain after sync with maven lib
    
  • 70

    当Intellij遇到其他模块中的编译问题时,它会安静地失败,然后不执行自动构建 . 所以检查你的 Problems 窗口

  • 2

    编辑运行/调试配置,以便在启动之前选择“构建”选项

    选择“构建”选项后

    在我的JBehave测试套件上工作时,上述解决方案对我有用

  • 192

    没有足够的点来评论现有的答案,但与上面的一些人类似,我最后只是添加一个宏和键盘映射到 Organize Imports / Format / SaveAll / FastReload(F9) / Synchronize .

    添加同步,因为它似乎是我还能看到外部构建工具/观察者(即webpack)修改的资源更新的唯一方法 .

    这个过程比eclipse慢 - 并且对于外部文件刷新经常需要多次运行命令 - 但是假设我可以忍受它 .

  • 14

    请遵循以下两个步骤:

    1 - 从编译器启用Automake

    • 按:ctrl shift A(对于Mac⌘转换A)

    • 类型: make project automatically

    • 点击:输入

    • 启用 Make Project automatically 功能

    2 - 在应用程序运行时启用Automake

    • 按:ctrl shift A(对于Mac⌘转换A)

    • 类型: Registry

    • 找到它旁边的密钥 compiler.automake.allow.when.app.runningenable itclick the checkbox

    注意:立即重启您的应用程序:)

    注意:这也应该允许使用spring boot devtools进行实时重新加载 .

  • 5

    使用Reformat and Compile插件(受Alexandre DuBreuil的Save Actions插件启发):

    https://plugins.jetbrains.com/plugin/8231?pr=idea_ce

    目前我只提供一个jar文件,但这是代码中最重要的部分:

    private final static Set<Document> documentsToProcess = new HashSet<Document>();
    private static VirtualFile[] fileToCompile = VirtualFile.EMPTY_ARRAY;
    
    // The plugin extends FileDocumentManagerAdapter.
    // beforeDocumentSaving calls reformatAndCompile
    private static void reformatAndCompile(
            @NotNull final Project project,
            @NotNull final Document document,
            @NotNull final PsiFile psiFile) {
        documentsToProcess.add(document);
        if (storage.isEnabled(Action.compileFile) && isDocumentActive(project, document)) {
            fileToCompile = isFileCompilable(project, psiFile.getVirtualFile());
        }
        ApplicationManager.getApplication().invokeLater(new Runnable() {
            @Override
            public void run() {
                if (documentsToProcess.contains(document)) {
                    documentsToProcess.remove(document);
                    if (storage.isEnabled(Action.optimizeImports)
                            || storage.isEnabled(Action.reformatCode)) {
                        CommandProcessor.getInstance().runUndoTransparentAction(new Runnable() {
                            @Override
                            public void run() {
                                if (storage.isEnabled(Action.optimizeImports)) {
                                    new OptimizeImportsProcessor(project, psiFile)
                                        .run();
                                }
                                if (storage.isEnabled(Action.reformatCode)) {
                                    new ReformatCodeProcessor(
                                            project,
                                            psiFile,
                                            null,
                                            ChangeListManager
                                                .getInstance(project)
                                                .getChange(psiFile.getVirtualFile()) != null)
                                                    .run();
                                }
                                ApplicationManager.getApplication().runWriteAction(new Runnable() {
                                    @Override
                                    public void run() {
                                        CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(psiFile);
                                    }
                                });
                            }
                        });
                    }
                }
    
                if (fileToCompile.length > 0) {
                    if (documentsToProcess.isEmpty()) {
                        compileFile(project, fileToCompile);
                        fileToCompile = VirtualFile.EMPTY_ARRAY;
                    }
                } else if (storage.isEnabled(Action.makeProject)) {
                    if (documentsToProcess.isEmpty()) {
                        makeProject(project);
                    }
                } else {
                    saveFile(project, document, psiFile.getVirtualFile());
                }
            }
        }, project.getDisposed());
    }
    
    private static void makeProject(@NotNull final Project project) {
        ApplicationManager.getApplication().invokeLater(new Runnable() {
            @Override
            public void run() {
                CompilerManager.getInstance(project).make(null);
            }
        }, project.getDisposed());
    }
    
    private static void compileFile(
            @NotNull final Project project,
            @NotNull final VirtualFile[] files) {
        ApplicationManager.getApplication().invokeLater(new Runnable() {
            @Override
            public void run() {
                CompilerManager.getInstance(project).compile(files, null);
            }
        }, project.getDisposed());
    }
    
    private static void saveFile(
            @NotNull final Project project,
            @NotNull final Document document,
            @NotNull final VirtualFile file) {
        ApplicationManager.getApplication().invokeLater(new Runnable() {
            @Override
            public void run() {
                final FileDocumentManager fileDocumentManager = FileDocumentManager.getInstance();
                if (fileDocumentManager.isFileModified(file)) {
                    fileDocumentManager.saveDocument(document);
                }
            }
        }, project.getDisposed());
    }
    
  • -1

    在受我影响的maven项目中,唯一对我有用的是为我的单元测试的运行配置添加“测试 - 编译”目标 . 令人难以置信的笨拙解决方案,但它的工作原理 .

  • 9

    Please follow these steps carefully to enable it.

    1)使用SB V1.3创建Spring Boot项目,并将“Devtools”(1 *)添加到依赖项

    2)调用Help-> Find Action ...并键入"Registry",在对话框中搜索"automake"并启用条目“compiler.automake.allow.when.app.running”,关闭对话框

    3)在设置 - >构建,执行,部署 - >编译器“自动生成项目”中启用后台编译

    4)打开Spring Boot运行配置,如果一切配置正确,你应该收到警告信息

    5)运行您的应用程序,即时更改您的课程

    请将您的经历和问题报告为此问题的评论 .

    click here for more info

  • -1

    我遇到过同样的问题 . 我使用的是“省电模式”,它可以防止逐步编译并显示编译错误 .

  • 0

    UPDATED
    对于IDEA 12版本,如果我们使用外部编译器选项,我们可以自动构建已编辑的源 . 唯一需要的是检查位于编译器设置下的选项"Build project automatically" . 此外,如果您希望热部署,在应用程序运行时或者如果您使用的是Spring boot devtools,则还应从注册表启用 compiler.automake.allow.when.app.running . 这将自动编译您的更改 .

    在注册表窗口打开后,使用ctrl shift A(或⌘转移A在mac上)键入 Registry ,找到并启用 compiler.automake.allow.when.app.running ,请参见此处:


    对于12以上的版本,您可以使用EclipseMode插件使IDEA自动编译保存的文件 . 有关从Eclipse迁移时的更多提示,请参阅:https://www.jetbrains.com/help/idea/2016.3/eclipse.html .

  • 0

    我设法使用宏来解决这个问题 .

    我开始录制一个宏:

    • 单击编辑 - 宏 - 开始宏录制

    • 单击文件 - 全部保存

    • 单击“构建 - 制作项目”

    • 单击编辑 - 宏 - 停止宏录制

    将它命名为“SaveAndMake” .

    现在只需删除Save all keybinding,并将相同的键绑定添加到宏中!

    所以现在,每次我保存,它都会保存并进行脏编译,jRebel现在可以正确检测所有更改 .

  • 0

    您可以键盘映射 ctrl+s 以一步保存和编译 . 转到键盘设置并搜索 Compile .

  • 1

    警告

    Eclipse Mode 插件已过时,与最近的IDEA 12版本不兼容 . 如果安装它,IDE将挂起每个文件改变并且反应极其缓慢 .


    IntelliJ IDEA不使用自动构建,它可以动态检测错误,而不是通过编译器 . 类似于Eclipse模式将在IDEA 12中提供:

    Make project automatically

    使用 Build | Make ,它调用增量make进程,该进程只编译已更改的和依赖文件(速度非常快) .

    还有一个可能有帮助的FAQ entry .

    Update on the automatic make feature :运行/ debug配置运行时, Make project automatically 无效 . 磁盘上的类只会在 Build |上更改 Make . 它's the core design decision as in our opinion class changes on disk should be always under user'的控制权 . 自动make不是Eclipse功能的模板,它的工作方式不同,它可以替换你仍然需要触发的显式编译,就像在这个问题中描述的情况一样 . 如果您正在寻找不同的行为,那么在上面的FAQ中链接的EclipseMode插件将是更好的选择 .

  • 36

    我最后录制了 Macro 以一步保存和编译,并将键盘映射 Ctrl+s .

  • 15

    实际上有 no difference ,因为两者都需要1次点击:

    • Eclipse:手动保存,自动编译 .

    • IntelliJ:自动保存,手动编译 .

    Simplest solution 只是习惯了它 . 因为当你将大部分时间花在你的IDE上时,最好在一个人中拥有快速习惯而不是在其中一些习惯中放慢习惯 .

相关问题