[英]Intellij IDEA Java classes not auto compiling on save
昨天我从 Eclipse 切换到 IntelliJ IDEA。
我也将 JRebel 与 WebSphere Server 7 一起使用。
现在一切似乎都运行良好,除了当我修改Java 文件并点击 save时,IntelliJ不会重新编译该文件,以便让 JRebel 拾取它。
Eclipse“自动构建”特性解决了这个问题。
在 IntelliJ IDEA 中,我必须按CTRL + SHIFT + 9重新编译相关类,以便 JRebel 将其拾取。 如果对两个文件进行了更改,我必须对每个文件都执行此操作,并且由于 IntelliJ 使用全部保存机制,因此很难知道要手动重新编译什么,而我也不是很感兴趣。
有没有办法让 IntelliJ 自己做到这一点?
对于 IntelliJ IDEA 12+ 版本,如果我们使用外部编译器选项,我们可以自动构建编辑过的源代码。 唯一需要的是检查选项“自动构建项目”,位于“编译器”设置下:
此外,如果您想在应用程序运行时进行热部署,或者如果您正在使用 spring boot devtools,您也应该从注册表启用compiler.automake.allow.when.app.running
。 这将自动编译您的更改。
打开注册表窗口后,使用Ctrl + Shift + A (或 Mac 上的⌘ + Shift + A )键入Registry
,找到并启用compiler.automake.allow.when.app.running
,请参见此处:
有关更多提示,请参阅“从 Eclipse 迁移到 IntelliJ IDEA”指南。
请按照以下两个步骤操作:
make project automatically
Make Project automatically
功能Registry
compiler.automake.allow.when.app.running
并启用它或单击它旁边的复选框注意:现在重新启动您的应用程序 :)
注意:这也应该允许使用 spring boot devtools 实时重新加载。
Eclipse Mode插件已过时,并且与最近的 IDEA 12+ 版本不兼容。 如果您安装它,IDE 将在每次文件更改时挂起,并且响应速度极慢。
IntelliJ IDEA 不使用自动构建,它会即时检测错误,而不是通过编译器。 类似于 Eclipse 模式将在IDEA 12 中可用:
使用Build
| Make
,它调用增量 make 过程,该过程将仅编译更改的和依赖的文件(速度非常快)。
还有一个常见问题条目可能会有所帮助。
自动生成功能更新:运行/调试配置运行时, Make project automatically
无效。 磁盘上的类只会在Build
时更改 | Make
. 这是核心设计决策,因为我们认为磁盘上的类更改应该始终在用户的控制之下。 自动生成不是 Eclipse 功能的复制品,它的工作方式不同,它的主要目的是节省等待类在真正需要时准备就绪的时间(在运行应用程序或测试之前)。 自动 make 不会替换您仍然需要触发的显式编译,就像在这个问题中描述的情况一样。 如果您正在寻找不同的行为,上面常见问题解答中链接的 EclipseMode 插件将是更好的选择。
您可以通过键盘映射ctrl+s
来保存和编译一步。 转到键盘映射设置并搜索Compile
。
实际上没有区别,因为两者都需要单击 1 次:
最简单的解决方案就是习惯它。 因为当您在 IDE 中度过大部分时间时,最好在其中一个习惯上养成快速习惯,而不是在其中几种习惯上养成慢习惯。
我最终录制了一个宏以在一个步骤中保存和编译,并将Ctrl+s
键映射到它。
我设法使用宏解决了这个问题。
我开始录制一个宏:
将它命名为一些有用的东西,例如“SaveAndMake”。
现在只需删除保存所有键绑定,并将相同的键绑定添加到您的宏中!
所以现在,每次我保存时,它都会保存并进行脏编译,jRebel 现在可以正确检测所有更改。
请仔细按照以下步骤启用它。
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) 运行您的应用程序,即时更改您的课程
请报告您的经验和问题作为对此问题的评论。
Intellij在其他模块遇到编译问题时悄悄失败,然后不执行自动构建。 所以检查你的Problems
窗口
我遇到了同样的问题,并且在 intellij 中还有一个问题文件图标,所以我删除了.idea
文件夹并重新导入项目解决了我的问题。
对于使用新Intellij
版本但找不到compiler.automake.allow.when.app.running
的人
https://youtrack.jetbrains.com/issue/IDEA-274903
基本上该选项现在已移至Settings -> Advanced Settings -> Compiler
(检查允许自动生成选项)
我遇到过同样的问题。 我正在使用“省电模式”,它可以防止增量编译并显示编译错误。
使用 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());
}
我遇到过同样的问题。 我认为检查您的类是否可以编译是合适的。 单击重新编译(默认情况下为 Ctrl+Shift+F9)。 如果它不工作,那么你必须调查它为什么不编译。
在我的情况下,代码没有自动编译,因为编译时存在隐藏的错误(它们没有显示在日志中的任何地方,并且 maven clean-install 正在工作)。 根本原因是项目结构 -> 模块配置不正确,因此 Intellij Idea 无法根据此配置构建它。
对于 Intellij 2021 或更高版本,您将找不到注册表项
compiler.automake.allow.when.app.running
它已移至高级设置,如下面的屏幕截图所示
来源: https ://youtrack.jetbrains.com/issue/IDEA-274903
我收到错误:某些 jar 不在类路径中。所以我只是删除损坏的 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
没有足够的点数来评论现有答案,但与上面的某些人类似,我最终只是添加了一个宏和键映射来组织导入/格式/保存所有/FastReload(F9)/同步。
添加了同步,因为它似乎是我还可以看到由外部构建工具/观察者(即 webpack)修改的资源更新的唯一方法。
该过程比 eclipse 慢 - 对于外部文件刷新,通常必须多次运行该命令 - 但假设我可以忍受它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.