博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Gradle Eclipse插件教程
阅读量:2533 次
发布时间:2019-05-11

本文共 8719 字,大约阅读时间需要 29 分钟。

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 Eclipse插件。 在我以前的文章中,我们讨论了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.5version = '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命令。

翻译自:

转载地址:http://raqzd.baihongyu.com/

你可能感兴趣的文章
阶段3 2.Spring_04.Spring的常用注解_3 用于创建的Component注解
查看>>
阶段3 2.Spring_04.Spring的常用注解_2 常用IOC注解按照作用分类
查看>>
阶段3 2.Spring_09.JdbcTemplate的基本使用_5 JdbcTemplate在spring的ioc中使用
查看>>
阶段3 3.SpringMVC·_07.SSM整合案例_02.ssm整合之搭建环境
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第1节零基础快速入门SpringBoot2.0_3、快速创建SpringBoot应用之手工创建web应用...
查看>>
阶段3 3.SpringMVC·_07.SSM整合案例_04.ssm整合之编写SpringMVC框架
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第1节零基础快速入门SpringBoot2.0_5、SpringBoot2.x的依赖默认Maven版本...
查看>>
阶段3 3.SpringMVC·_07.SSM整合案例_08.ssm整合之Spring整合MyBatis框架
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第2节 SpringBoot接口Http协议开发实战_9、SpringBoot基础HTTP其他提交方法请求实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第2节 SpringBoot接口Http协议开发实战_12、SpringBoot2.x文件上传实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第4节 Springboot2.0单元测试进阶实战和自定义异常处理_19、SpringBoot个性化启动banner设置debug日志...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第4节 Springboot2.0单元测试进阶实战和自定义异常处理_20、SpringBoot2.x配置全局异常实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第5节 SpringBoot部署war项目到tomcat9和启动原理讲解_23、SpringBoot2.x启动原理概述...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第4节 Springboot2.0单元测试进阶实战和自定义异常处理_21、SpringBoot2.x配置全局异常返回自定义页面...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第8节 数据库操作之整合Mybaties和事务讲解_32..SpringBoot2.x持久化数据方式介绍...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第8节 数据库操作之整合Mybaties和事务讲解_34、SpringBoot整合Mybatis实操和打印SQL语句...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第8节 数据库操作之整合Mybaties和事务讲解_35、事务介绍和常见的隔离级别,传播行为...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第9节 SpringBoot2.x整合Redis实战_40、Redis工具类封装讲解和实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第9节 SpringBoot2.x整合Redis实战_37、分布式缓存Redis介绍...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第10节 SpringBoot整合定时任务和异步任务处理_42、SpringBoot常用定时任务配置实战...
查看>>