提问人:mjs 提问时间:10/5/2012 最后编辑:informatik01mjs 更新时间:1/13/2022 访问量:207634
Intellij IDEA Java 类在保存时不自动编译
Intellij IDEA Java classes not auto compiling on save
问:
昨天我从 Eclipse 切换到 IntelliJ IDEA。
我也在 WebSphere Server 7 中使用 JRebel。
现在一切似乎都正常工作,除了当我修改 Java 文件并点击保存时,IntelliJ 不会重新编译该文件,以便 JRebel 获取它。
Eclipse 的“自动构建”功能解决了这个问题。
在 IntelliJ IDEA 中,我必须按 ++ 重新编译相关类,以便 JRebel 拿起它。如果对两个文件进行更改,我必须对每个文件和其中一个文件执行此操作,并且由于 IntelliJ 使用保存所有机制,因此很难知道手动重新编译什么,我对此也不感兴趣。CTRLSHIFT9
难道没有办法让 IntelliJ 自己做到这一点吗?
答:
更新
对于 IntelliJ IDEA 12+ 版本,如果我们使用外部编译器选项,我们可以自动构建编辑后的源代码。唯一需要做的就是选中位于“编译器”设置下的“自动构建项目”选项:
此外,如果您想在应用程序运行时进行热部署,或者如果您正在使用 Spring Boot devtools,您也应该启用 from 注册表。这将自动编译您的更改。compiler.automake.allow.when.app.running
对于高于 2021.2 的版本,我们需要选中“即使开发应用程序当前正在运行,也允许自动启动”选项:
对于早于 2021.2 的版本:
使用++(或Mac上的++)类型打开注册表窗口后,找到并启用,请参阅此处:CtrlShiftA⌘ShiftARegistry
compiler.automake.allow.when.app.running
对于早于 12 的版本,您可以使用 *EclipseMode* 插件让 IDEA 自动编译保存的文件。
有关更多提示,请参阅“从 Eclipse 迁移到 IntelliJ IDEA”指南。
评论
警告
Eclipse Mode 插件已过时,与最近的 IDEA 12+ 版本不兼容。如果安装它,IDE 将在每次文件更改时挂起,并且响应速度非常慢。
IntelliJ IDEA 不使用自动构建,而是动态检测错误,而不是通过编译器。与 Eclipse 模式类似,将在 IDEA 12 中提供:
用途 |,它调用增量 make 过程,该过程将仅编译已更改和依赖的文件(速度非常快)。Build
Make
还有一个常见问题解答条目可能会有所帮助。
自动制作功能的更新:
当运行/调试配置正在运行时,没有效果。磁盘上的类仅在 | 上更改。这是核心设计决策,因为在我们看来,磁盘上的类更改应该始终在用户的控制之下。自动制作不是 Eclipse 功能的模仿者,它的工作方式不同,它的主要目的是节省等待类在真正需要时准备就绪的时间(在运行应用程序或测试之前)。自动生成不会替换您仍然需要触发的显式编译,如本问题中描述的情况。如果您正在寻找不同的行为,上面常见问题解答中链接的 EclipseMode 插件将是一个更好的选择。Make project automatically
Build
Make
评论
您可以一步到位地进行 keymap 保存和编译。转到键盘映射设置并搜索 。ctrl+s
Compile
评论
我最终录制了一个宏,以便一步保存和编译,并对其进行键盘映射。Ctrl+s
评论
我有同样的问题。 我使用的是“省电模式”,它可以防止增量编译并显示编译错误。
评论
我设法使用宏解决了这个问题。
我开始录制一个宏:
- 点击编辑 - 宏 - 开始宏录制
- 单击文件 - 全部保存
- 单击“生成”-“生成项目”
- 点击编辑 - 宏 - 停止宏录制
将其命名为有用的名称,例如“SaveAndMake”。
现在只需删除“保存所有键绑定”,并将相同的键绑定添加到宏中!
所以现在,每次我保存时,它都会保存并进行肮脏的编译,jRebel 现在可以正确检测所有更改。
评论
实际上没有区别,因为两者都需要 1 次点击:
- Eclipse:手动保存,自动编译。
- IntelliJ:自动保存,手动编译。
最简单的解决方案就是习惯它。因为当你把白天的大部分时间都花在IDE上时,最好在一个IDE中养成快速的习惯,而不是在几个IDE中养成缓慢的习惯。
评论
使用 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 - 从编译器启用自动制作
- 按:+ +(适用于 MacctrlshiftA⌘ + shift + A)
- 类型:
make project automatically
- 打:Enter
- 启用功能
Make Project automatically
2 - 在应用程序运行时启用自动制作
- 按:+ +(适用于 MacctrlshiftA⌘ + shift + A)
- 类型:
Registry
- 找到密钥并启用它,或单击它旁边的复选框
compiler.automake.allow.when.app.running
注意:立即重新启动应用程序:)
注意:这还应该允许使用 spring boot devtools 进行实时重新加载。
评论
请仔细按照以下步骤启用它。
1) 使用 SB V1.3 创建 Spring Boot 项目,并将“Devtools”(1*) 添加到依赖项中
2) 调用 Help->Find 操作...并键入“注册表”,在对话框中搜索“automake”并启用条目“compiler.automake.allow.when.app.running”,关闭对话框
3)在Settings->Build, Execution, Deployment->Compiler中启用后台编译“自动制作项目”
4)打开Spring Boot运行配置,如果一切都配置正确,您应该会收到警告消息
5)运行您的应用程序,即时更改您的课程
请将您的经验和问题作为对本期的评论报告。
评论
在我的 maven 项目中,唯一受此影响的方法是将“测试编译”目标添加到我的单元测试的运行配置中。令人难以置信的笨拙解决方案,但它有效。
评论
没有足够的点来评论现有的答案,但与上面的一些人类似,我最终只是添加了一个宏和键盘映射来组织导入/格式/保存所有/快速重载(F9)/同步。
添加同步是因为它似乎是我还可以查看由外部构建工具/观察者(即 webpack)修改的资源更新的唯一方法。
该过程比eclipse慢 - 并且对于外部文件刷新,通常必须多次运行该命令 - 但假设我可以忍受它。
Intellij 在其他模块中遇到编译问题时会悄悄失败,然后不执行自动构建。所以检查你的窗户Problems
我收到错误:某些 jar 不在类路径中。所以我只是删除损坏的 jar 和 perrform 下面的步骤
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
编辑运行/调试配置,以便在启动之前选择“生成”选项
选择“生成后”选项
上述解决方案在开发我的 JBehave 测试套件时对我有用
评论
由于我的项目结构中有一个不必要的模块,它对我不起作用。我想,测试是使用另一个模块执行的。
我不知道它是如何变成这样的,但删除它解决了这个问题。
确保您的“运行/调试”设置使用您正在通过自动保存构建的模块。
例如:请参阅模块
您可以在项目结构 - 模块中更改模块
我有同样的问题。我认为检查您的类是否可以编译是合适的。单击“重新编译”(默认为 Ctrl+Shift+F9)。如果它不起作用,那么您必须调查它无法编译的原因。
就我而言,代码没有自动编译,因为编译存在隐藏的错误(它们没有显示在任何地方的日志中,并且 maven clean-install 正在工作)。根本原因是项目结构 -> 模块配置不正确,因此 Intellij Idea 无法根据此配置构建它。
我遇到了同样的问题,IntelliJ 中也有一个问题文件图标,所以我删除了文件夹并重新导入项目解决了我的问题。.idea
对于使用新版本但找不到的人Intellij
compiler.automake.allow.when.app.running
https://youtrack.jetbrains.com/issue/IDEA-274903
基本上,该选项现在已移至(选中“允许自动制作”选项)Settings -> Advanced Settings -> Compiler
评论
对于 Intellij 2021 或更高版本,你将找不到注册表项
compiler.automake.allow.when.app.running
它已移至高级设置,如以下屏幕截图所示
评论
Ctrl
F9