本文共 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安装。 在阅读本文之前,请参考我以前关于 。
In this post, we will discuss about the following two topics.
在本文中,我们将讨论以下两个主题。
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开发人员来说,这很容易学习。
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插件。
I’m using Eclipse 4.4 Luna IDE. You can use same steps for other Eclipse versions too.
我正在使用Eclipse 4.4 Luna IDE。 您也可以对其他Eclipse版本使用相同的步骤。
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工具开发应用程序了。
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示例。 请使用以下步骤来开发和测试此应用程序。
Select “Gradle Project” option under “Gradle” Category as shown below:
在“成绩”类别下选择“成绩项目”选项,如下所示:
Click on “Next” Button to see “New Gradle Project” Window as shown below
单击“下一步”按钮以查看“新Gradle项目”窗口,如下所示
Now click on “Finish” Button to create new Java Gradle project.
现在单击“完成”按钮以创建新的Java Gradle项目。
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文件。
build.gradle
file contains below content. 我们的项目build.gradle
文件包含以下内容。 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构建脚本差异的详细信息。
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对其进行测试。
Here observe “Type tasks in the editor below. Use + to activate content assistant.” text.
在这里观察“在下面的编辑器中键入任务。 使用+激活内容助手。” 文本。
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命令时,它将执行以下操作:
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/