且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

IntelliJ(2020.2)-如何为基于Gradle的项目禁用“自动构建项目"?

更新时间:2021-07-05 01:00:11

IntelliJ的Gradle支持中有两件事有时会混淆:syncbuild.您的图片演示了sync过程(请注意工具窗口上的标题).单词build在这里有点误导.

There are two different things in IntelliJ's Gradle support that sometimes confused: sync and build. Your pictures demonstrate sync process (note caption on the toolwindow). Word build is kind of misleading here.

什么是sync?在gradle中,我们使用Groovy定义构建过程. Groovy是一种命令式编程语言,因此,如果不实际执行脚本,就很难预测生成的依赖关系图.在sync阶段,想法执行渐变构建configuration phase构建依赖关系图),并从Gradle守护程序获取已配置的对象.这些信息用于在IDE中设置项目:模块,库,依赖项,哪些源是测试的,哪些是产品等.

What is sync? In gradle we use Groovy to define the build procedure. Groovy is an imperative programming language, so it's hard to predict resulting dependencies graph without actually executing the script. During the sync Idea executes configuration phase of gradle build (one that builds dependency graph), and obtains configured objects from the Gradle daemon. This information is used to setup project in the IDE: modules, libraries, dependencies, which sources are test, which are prod, etc.

sync中未进行实际构建.您可以通过将语法错误添加到任何源文件来说服自己,并观察同步成功.但是,如果您调用它,构建将失败.

Actual build is not happening during sync. You can convince yourself by adding syntax error to any source file, and observe that the sync succeeds. But build will fail if you invoke it.

回答原始问题:您不能禁用自动构建,因为未启用.

In answer to the original question: you can't disable automatic build, because it is not enabled.

是否可以在Gradle项目中禁用同步?简短答案-不.如果您需要一个代码浏览器(不需要了解源代码中的所有交叉引用),则IDEA可能不是***选择.

Is it possible to disable sync in Gradle project? Short answer - no. If you need a code browser, which is not required to understand all the cross-references in the source code, IDEA is not the best choice probably.

TL; DR;

没有sync,IDE不知道哪些文件是源文件,哪些不是. IDEA无法打开文件夹.它只能打开项目.好东西是模块可以包含文件夹.因此,您可以执行以下操作:File | New | Project.选择Empty projectNext,选择一些随机文件夹外部要打开的源文件夹Finish.

Without sync IDE does not know which files are sources, and which are not. IDEA cannot open folders. It only can open projects. Good thing is that module can contain folder. So you can do the following: File | New | Project. Select Empty project, Next, select some random folder outside the source folder you want to open, Finish.

然后添加新模块:

在左侧面板中选择Java,其他所有内容均保持默认设置,NextFinish.然后在新模块中删除现有内容根,并添加包含源的文件夹作为新内容根

Select Java in the left panel, everything else keep default, Next, Finish. Then in new module remove existing content root, and add folder with sources as new content root

产生的项目几乎没有用.大量红色代码(至少是外部库中未解析的符号),没有检查,没有导航,没有任何意义.但这确实在某些罕见情况下可能有用.

Resulting project is mostly useless. Tons of red code (at least, unresolved symbols from external libraries), no inspections, no navigation, no sense. But it might be useful in some rare situations indeed.