Gradle Eclipse插件教程

Today we will look into Gradle Eclipse plugin. In my previous post, we have discussed about Gradle Basics and Gradle installation. Before reading this post, Please refer my previous post about gradle tutorial.

今天,我们将研究Gradle Eclipse插件。 在我以前的文章中,我们讨论了Gradle基础知识和Gradle安装。 在阅读本文之前,请参考我以前关于gradle教程的文章

Gradle Eclipse插件 (Gradle Eclipse Plugin)

In this post, we will discuss about the following two topics.

在本文中,我们将讨论以下两个主题。

  1. How to setup Gradle Eclipse Plugin

    如何设置Gradle Eclipse插件
  2. How to Develop Java Simple Example with Gradle Eclipse Plugin

    如何使用Gradle Eclipse插件开发Java简单示例

Gradle uses it’s own DSL(Domain Specific Language) Groovy-based scripts to write build scripts. Unlike Ant and Maven, it does not use complex XML build scripts. Gradle is developed using Java and configuration elements are developed using Groovy. It uses Groovy to write build scripts.

Gradle使用它自己的基于Groovy的DSL(域特定语言)的脚本来编写构建脚本。 与Ant和Maven不同,它不使用复杂的XML构建脚本。 Gradle使用Java开发,而配置元素则使用Groovy开发。 它使用Groovy编写构建脚本。

Prerequisite: To understand Gradle DSL scripts, we should have some knowledge about Groovy basics. Please go through some Groovy tutorials before going through this post.

先决条件 :要了解Gradle DSL脚本,我们应该对Groovy基础有所了解。 在阅读本文之前,请阅读一些Groovy教程。

NOTE: Groovy is a Dynamic Programming Language and it’s syntax is similar to java programming language. It is very easy to learn for a Java Developer.

注意 :Groovy是一种动态编程语言,其语法类似于Java编程语言。 对于Java开发人员来说,这很容易学习。

Gradle构建脚本 (Gradle Build Scripts)

Now we will start writing simple Gradle build scripts. As we are already familiar with Ant and Maven build scripts, we know what is the starting point to start writing build scripts.

现在,我们将开始编写简单的Gradle构建脚本。 正如我们已经熟悉了Ant和Maven构建脚本一样,我们知道开始编写构建脚本的起点是什么。

First and foremost thing we should know is Gradle default build script file name. Like Ant’s default build script name is build.xml and Maven default build script name is pom.xml, Gradle default build script name is build.gradle".

我们首先应该知道的是Gradle默认构建脚本文件名。 就像Ant的默认构建脚本名称是build.xml和Maven默认构建脚本名称是pom.xml ,Gradle的默认构建脚本名称是build.gradle"

When we run “gradle” command, it searches for this default file available in the current working directory. If it finds, it executes that build script. Otherwise, displays some useful default help message.

当我们运行“ gradle”命令时,它将搜索当前工作目录中可用的默认文件。 如果找到,它将执行该构建脚本。 否则,显示一些有用的默认帮助消息。

We will use Eclipse Gradle Plugin to develop and test all our Gradle examples. Before working with simple examples, let’s first setup Gradle Plugin with Eclipse IDE.

我们将使用Eclipse Gradle插件来开发和测试所有Gradle示例。 在处理简单示例之前,让我们首先使用Eclipse IDE设置Gradle插件。

Gradle Eclipse插件安装 (Gradle Eclipse Plugin installation)

I’m using Eclipse 4.4 Luna IDE. You can use same steps for other Eclipse versions too.

我正在使用Eclipse 4.4 Luna IDE。 您也可以对其他Eclipse版本使用相同的步骤。

  1. Open “Eclipse Marketplace…” from “Help” menu. Type “gradle” in Search box as shown below

    从“帮助”菜单中打开“ Eclipse Marketplace…”。 在搜索框中输入“ gradle”,如下所示
  2. Click on “Install” button for “Gradle Integration for Eclipse(4.4) 3.6.4.RELEASE” option to install Gradle Eclipse Plugin

    单击“安装”按钮以使用“ Gradle Integration for Eclipse(4.4)3.6.4.RELEASE”选项,以安装Gradle Eclipse插件
  3. Accept license by clicking radio button and click on “Finish” button.

    单击单选按钮接受许可,然后单击“完成”按钮。

This step installs Eclipse Gradle plugin and restarts Eclipse IDE. Now we can start developing applications using Gradle Build tool.

此步骤将安装Eclipse Gradle插件并重新启动Eclipse IDE。 现在我们可以开始使用Gradle Build工具开发应用程序了。

Gradle Eclipse插件示例 (Gradle Eclipse Plugin Example)

Now we are going to develop a simple java example with Eclipse Gradle Plugin. Please use the following steps to develop and test this application.

现在,我们将使用Eclipse Gradle Plugin开发一个简单的Java示例。 请使用以下步骤来开发和测试此应用程序。

  1. Click on “File >> New >> Other” to open “New” Wizard window to create new Java Gradle Project.

    单击“文件>>新建>>其他”以打开“新建”向导窗口,以创建新的Java Gradle Project。
  2. Select “Gradle Project” option under “Gradle” Category as shown below:

    在“成绩”类别下选择“成绩项目”选项,如下所示:

    Click on “Next” Button to see “New Gradle Project” Window as shown below

    单击“下一步”按钮以查看“新Gradle项目”窗口,如下所示

  3. In “New Gradle Project” Window, we need to provide two details

    在“ New Gradle Project”窗口中,我们需要提供两个详细信息
    1. Please provide our project name: “JavaGradleSimpleExample”

      请提供我们的项目名称:“ JavaGradleSimpleExample”
    2. Select “Java Quickstart” option from “Sample project” Dropdown box

      从“示例项目”下拉框中选择“ Java快速入门”选项

    Now click on “Finish” Button to create new Java Gradle project.

    现在单击“完成”按钮以创建新的Java Gradle项目。

  4. Now our Java Gradle project structure looks like below image.

    现在,我们的Java Gradle项目结构如下图所示。
  5. If you observe this project structure, Gradle Project is same as Maven Project structure. Yes, Gradle uses Maven Project structure but instead of pom.xml file, we have build.gradle file.

    如果您观察到此项目结构,则Gradle Project与Maven Project结构相同。 是的,Gradle使用Maven Project结构,但我们使用build.gradle文件代替pom.xml文件。

  6. Our project build.gradle file contains below content.

    我们的项目build.gradle文件包含以下内容。
  7. apply plugin: 'java'
    apply plugin: 'eclipse'
    
    sourceCompatibility = 1.5
    version = '1.0'
    jar {
        manifest {
            attributes 'Implementation-Title': 'Gradle Quickstart',
                       'Implementation-Version': version
        }
    }
    
    repositories {
        mavenCentral()
    }
    
    dependencies {
    compile group:'commons-collections',name:'commons-collections',version:'3.2'
    testCompile group: 'junit', name: 'junit', version: '4.+'
    }
    
    test {
        systemProperties 'property': 'value'
    }
    
    uploadArchives {
        repositories {
           flatDir {
               dirs 'repos'
           }
        }
    }

    If you don’t understand this file content at this stage, don’t worry. We will discuss this file content in detail in coming posts and also provide you one post about Gradle and Maven build scripts differences in detail.

    如果您在此阶段不了解此文件的内容,请不要担心。 我们将在以后的文章中详细讨论此文件的内容,并为您提供有关Gradle和Maven构建脚本差异的详细信息。

  8. This default Java Gradle project, creates one Java file: Person.java and one JUnit test class PersonTest.java as shown below.

    这个默认的Java Gradle项目创建一个Java文件: Person.java和一个JUnit测试类PersonTest.java ,如下所示。
  9. Person.java

    Person.java

    package org.gradle;
    
    import org.apache.commons.collections.list.GrowthList;
    
    public class Person {
        private final String name;
    
        public Person(String name) {
            this.name = name;
            new GrowthList();
        }
    
        public String getName() {
            return name;
        }
    }

    PersonTest.java

    PersonTest.java

    package org.gradle;
    
    import org.junit.Test;
    import static org.junit.Assert.*;
    
    public class PersonTest {
        @Test
        public void canConstructAPersonWithAName() {
            Person person = new Person("Larry");
            assertEquals("Larry", person.getName());
        }
    }

    It does NOT have much logic. It just created a Person POJO class and one JUnit to test it.

    它没有太多逻辑。 它只是创建了一个Person POJO类和一个JUnit对其进行测试。

  10. It’s time to run our application. Please use the following steps to build and test our Gradle build script.

    现在该运行我们的应用程序了。 请使用以下步骤来构建和测试我们的Gradle构建脚本。
    1. Right click on our “JavaGradleSimpleExample” Project’s build.gradle file and select “Run As” >> “Gradle build” option as shown below:

      右键单击我们的“ JavaGradleSimpleExample”项目的build.gradle文件,然后选择“运行方式” >>“ Gradle构建”选项,如下所示:
    2. It opens “Edit Configuration” Wizard window as shown below

      它将打开“编辑配置”向导窗口,如下所示
    3. Here observe “Type tasks in the editor below. Use + to activate content assistant.” text.

      在这里观察“在下面的编辑器中键入任务。 使用+激活内容助手。” 文本。

    4. We need to type our required Gradle commands. When we keep control point in that Editor, it will display all available Gradle commands as shown below

      我们需要输入所需的Gradle命令。 当我们将控制点保留在该编辑器中时,它将显示所有可用的Gradle命令,如下所示
    5. Now type “build” Gradle command in that Text editor as shown below.

      现在,在该文本编辑器中键入“ build” Gradle命令,如下所示。
    6. Click on “Apply” button to apply our changes. Then click on “Run” button to start our Gradle build commnad “gradle build”

      单击“应用”按钮以应用我们的更改。 然后点击“运行”按钮开始我们的Gradle构建命令“ gradle build”
    7. Observe the Eclipse IDE Console log.

      查看Eclipse IDE控制台日志。

    If you observe the console output, it shows “BUILD SUCCESSFUL” message. That means our Gradle build command has executed successfully.

    如果您观察到控制台输出,则显示“ BUILD SUCCESSFUL”消息。 这意味着我们的Gradle构建命令已成功执行。

When we run the gradle build command, it does the following things:

当我们运行gradle build命令时,它将执行以下操作:

  1. It compiles both java files

    它编译两个java文件
  2. It generate jar file with name “JavaGradleSimpleExample-1.0.jar” at ${PROJECT_ROOT_DIR}\build\libs that is “JavaGradleSimpleExample\build\libs” as shown below:

    它在$ {PROJECT_ROOT_DIR} \ build \ libs处生成名称为“ JavaGradleSimpleExample-1.0.jar”的jar文件,即“ JavaGradleSimpleExample \ build \ libs”,如下所示:
  3. It executes JUnit file

    它执行JUnit文件

If something goes wrong in any one of these steps, we will see “BUILD FAILED” error message.

如果这些步骤中的任何一个出现问题,我们将看到“ BUILD FAILED”错误消息。

That’s it for Gradle Eclipse plugin example. We will explore build.gradle file content and Gradle commands in coming posts.

Gradle Eclipse插件示例就是这样。 我们将在以后的文章中探讨build.gradle文件的内容和Gradle命令。

翻译自: https://www.journaldev.com/8118/gradle-eclipse-plugin-tutorial

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Eclipse是一个功能强大的开发工具,可以支持多种开发语言,如Java、C++等。而Gradle是一个流行的构建工具,用于自动构建、测试和部署代码。 当我们在Eclipse中使用Gradle时,有时可能会遇到下载失败的问题。导致下载失败的原因可能有以下几种: 1. 网络连接问题:下载过程中可能出现网络连接不稳定或者断开的情况,导致下载失败。这时我们可以尝试重新开始下载,或者检查网络连接是否正常。 2. 服务器问题:Gradle的下载可能受到服务器的限制,如果服务器出现故障或者过载,可能会导致下载失败。这时我们可以尝试重新下载,或者等待服务器恢复正常。 3. Gradle版本问题:Eclipse中的Gradle插件可能不兼容当前安装的Gradle版本,导致下载失败。我们可以尝试升级或降级Gradle的版本,或者更新Eclipse中的Gradle插件。 4. 配置问题:可能是因为我们的Gradle配置文件中指定的下载源无效或不可用,导致下载失败。这时我们可以尝试修改配置文件,指定有效的下载源。 解决下载失败问题的方法包括: 1. 检查网络连接并重新开始下载。 2. 尝试使用其他下载源或者更新Gradle版本。 3. 更新Eclipse中的Gradle插件。 4. 检查Gradle配置文件中的下载源是否有效并进行修改。 总之,对于Eclipse中的Gradle下载失败问题,我们需要仔细排查可能的原因,并采取相应的解决方法。通过确保网络连接稳定,更新Gradle版本或插件,以及修复配置文件中的问题,我们应该能够成功下载并使用Gradle

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值