Java 将 Gradle 支持添加到 IntelliJ 项目的最佳方式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26745541/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 03:12:43  来源:igfitidea点击:

Best way to add Gradle support to IntelliJ Project

javagitintellij-ideagradle

提问by Gremash

I have looked around quite a bit and haven't found the best solution to convert an existing IntelliJ project to Gradle. I work in a team environment and we currently share the .ipr file as we have a few build configurations that we track. We will be getting rid of those in favor of Gradle eventually but I can't screw things up too much until the Gradle conversion is done.

我环顾四周,没有找到将现有 IntelliJ 项目转换为 Gradle 的最佳解决方案。我在团队环境中工作,我们目前共享 .ipr 文件,因为我们有一些我们跟踪的构建配置。我们最终会摆脱那些支持 Gradle 的人,但在 Gradle 转换完成之前,我不能把事情搞砸太多。

Also, our Java source files are located in the root of the src directory instead of src/main/java as is standard.

此外,我们的 Java 源文件位于 src 目录的根目录中,而不是标准的 src/main/java 中。

Is there a way I can add Gradle to my project that won't make me delete and recreate my IntelliJ project and won't screw everyone else up when they do a Git pull?

有没有一种方法可以将 Gradle 添加到我的项目中,而不会让我删除并重新创建我的 IntelliJ 项目,并且在他们执行 Git 拉取时不会把其他人搞砸?

采纳答案by SuperAndrew

Why don't you just add:

你为什么不添加:

build.gradle 

in your root project folder, and use plugin for example:

在您的根项目文件夹中,并使用插件,例如:

apply plugin: 'idea'
//and standard one
apply plugin: 'java'

and with this fire from command line:

并从命令行触发:

gradle cleanIdea 

and after that:

在那之后:

gradle idea

After that everything should work

之后一切都应该工作

回答by hippoLogic

I'm using Version 12 of IntelliJ.

我正在使用 IntelliJ 的第 12 版。

I solved a similar problem by creating an entirely new project and "Checking out from Version Control" Merging the two projects later was fairly easy.

我通过创建一个全新的项目和“从版本控制检出”解决了类似的问题,稍后合并两个项目相当容易。

回答by Guildenstern70

Another way, simpler.

另一种方式,更简单。

Add your

添加您的

build.gradle

file to the root of your project. Close the project. Manually remove *.iml file. Then choose "Import Project...", navigate to your project directory, select the build.gradlefile and click OK.

文件到项目的根目录。关闭项目。手动删除 *.iml 文件。然后选择“导入项目...”,导航到您的项目目录,选择build.gradle文件并单击“确定”。

回答by Yar

Just as a future reference, if you already have a Mavenproject all you need to do is doing a gradle initin your project directory which will generates build.gradleand other dependencies, then do a gradle buildin the same directory.

作为将来的参考,如果您已经有一个Maven项目,您需要做的就是gradle init在您的项目目录中执行 a将生成build.gradle和其他依赖项,然后gradle build在同一目录中执行 a 。

回答by Michal Kordas

There is no need to remove any .imlfiles. Follow this:

无需删除任何.iml文件。按照这个:

  • close the project
  • File->Open...and choose your newly created build.gradle
  • IntelliJ will ask you whether you want:
    • Open Existing Project
    • Delete Existing Project and Import
  • Choose the second option and you are done
  • 关闭项目
  • File->Open...并选择您新创建的 build.gradle
  • IntelliJ 会问你是否想要:
    • Open Existing Project
    • Delete Existing Project and Import
  • 选择第二个选项,你就完成了

回答by Raniz

In IntelliJ 2017.2.4 I just closed the project and reopened it and I got a dialog asking me if I wanted to link with build.gradlewhich opened up the import dialog for Gradle projects.

在 IntelliJ 2017.2.4 中,我刚刚关闭了项目并重新打开它,我收到一个对话框,询问我是否想与build.gradle链接,后者打开了 Gradle 项目的导入对话框。

No need to delete any files or add the ideaplugin to build.gradle.

无需删除任何文件或将idea插件添加到build.gradle

回答by Gayan Weerakutti

  1. Add build.gradlein your project's root directory.

  2. Then just File-> Invalidate Caches / Restart

  1. 添加build.gradle到项目的根目录中。

  2. 然后只是File- >Invalidate Caches / Restart

Here is a basic build.gradle for Java projects:

这是 Java 项目的基本 build.gradle:

plugins {
    id 'java'
}

sourceCompatibility = '1.8'
targetCompatibility = '1.8'
version = '1.2.1'