Now that we know how to install gradle into your system and configure it’s eclipse plugin, it’s time to look into the gradle script. Since most of us working in java technology are already familiar with maven, we will discuss the same elements in both maven and gradle build scripts so that new users will grasp Gradle build script knowledge easily.
While discussing the gradle tutorial points, I will refer the build.gradle file from my previous post. Below is the build.gradle file from our earlier gradle example project.
Below is the explanation of different elements one by one.
以下是对不同元素的说明。
Gradle教程– Java版本 (Gradle Tutorial – java version)
If we observe at line 4, it is referring to sourceCompatibility with value as 1.5. That means it is referring Java 1.5. By default, Eclipse Gradle plugin refers to Java 1.5 using “sourceCompatibility” element as shown below:
Now I am using Java 1.7, so above gradle script will produce following warning message.
现在我正在使用Java 1.7,因此上述gradle脚本将产生以下警告消息。
:compileTestJavawarning: [options] bootstrap class path not set in conjunction with -source 1.5
1 warning
So we need to update this to 1.7 as shown below.
因此,我们需要将其更新为1.7,如下所示。
sourceCompatibility = 1.7
If we run same gradle build command in Eclipse IDE, we won’t see these warnings because we are using same java version in both IDE and Gradle’s build script file.
We need to use the following syntax to apply required plugins in Gradle build script file.
我们需要使用以下语法在Gradle构建脚本文件中应用所需的插件。
apply plugin:
For example, if we are going to develop Java application in Eclipse IDE using Gradle, then we need to apply two plugins: “eclipse and java” as shown below
例如,如果我们要使用Gradle在Eclipse IDE中开发Java应用程序,那么我们需要应用两个插件:“ eclipse and java”,如下所示
apply plugin: 'java'
apply plugin: 'eclipse'
The following Gradle build script definitions are;
After developing any project or to test/deploy project in different environments (Dev, QA, PROD etc), we need to package it into our required format like Jar file, War file or EAR file.
This Gradle element is similar to the following Maven element
此Gradle元素类似于以下Maven元素
jar
To create WAR (WebApplication Archive) file, we need to use below gradle syntax.
要创建WAR(Web应用程序存档)文件,我们需要使用以下gradle语法。
war {
}
This Gradle element is similar to the following Maven element
此Gradle元素类似于以下Maven元素
war
To create EAR(Enterprise Application Archive) file, we need to use this syntax:
要创建EAR(企业应用程序归档)文件,我们需要使用以下语法:
ear {
}
This Gradle element is similar to the following Maven element
此Gradle元素类似于以下Maven元素
ear
If we miss packaging or assemble element definition, both maven and gradle uses default value: “jar” package plugin.
如果我们错过打包或组装元素的定义,那么maven和gradle都使用默认值:“ jar”包插件。
As “jar” is default assemble value, we don’t need to apply jar plugin in gradle’s build script file. However, to create WAR or EAR we need to apply the respective plugins as shown below:
如何提供我们所需的项目名称和版本 (How to provide our required Project name and Version)
As we discussed , we can use “version” element of build.gradle file to define Jar/WAR/EAR file’s version. But it’s recommended to use this syntax to define our jar file name and version.
正如我们所讨论的,我们可以使用build.gradle文件的“ version”元素来定义Jar / WAR / EAR文件的版本。 但是建议使用此语法定义我们的jar文件名和版本。
jar {
baseName = 'MyJavaGradleProject'
version = '2.0'
}
Here baseName element defines jar file name and version element defines version number.
在这里, baseName元素定义了jar文件名, version元素定义了版本号。
Now our newly created jar file name is MyJavaGradleProject-2.0.jar.
现在,我们新创建的jar文件名为MyJavaGradleProject-2.0.jar 。
To create war file, we need to use this element definition:
要创建战争文件,我们需要使用以下元素定义:
war{
baseName = 'MyJavaGradleProject'
version = '2.0'
}
It will create a WAR file with this name : MyJavaGradleProject-2.0.war. We can deploy this war file into any Web or Application Server like Tomcat, Weblogic etc. or we can run this using “java -jar” command.
NOTE:- If we define both version elements like below, what will happen?
注意:-如果我们同时定义以下两个版本元素,将会发生什么?
version = '1.0'
jar {
baseName = 'MyJavaGradleProject'
version = '2.0'
}
gradle command use “version” element from “jar{ }” element so that our Jar file name will be MyJavaGradleProject-2.0.jar because inner “version” element has higher priority than outer element. Inner “version” element overrides outer “version” element value.
gradle命令使用“ jar {}”元素中的“ version”元素,因此我们的Jar文件名为MyJavaGradleProject-2.0.jar,因为内部“ version”元素的优先级高于外部元素。 内部“版本”元素将覆盖外部“版本”元素的值。
Gradle依赖 (Gradle dependencies)
We need to use “dependencies” element in build.gradle file to define our project dependencies. When we define dependencies, Gradle will check those jar files from MavenRepository and download them into local and add to our project build path.
That means, without specifying group, name and version of each dependency jar file, we can define them using this format.
这意味着,无需指定每个依赖项jar文件的组,名称和版本,我们就可以使用这种格式来定义它们。
But we should take care of defining them in same order: group, name then version. Instead of comma(,), We need to use colon(:) operator to separate them.
We use the following Gradle build script element to define our required repository to connect and download our project dependencies to the Local Repository.
If we observe both Gradle build.gradle file and maven pom.xml file, we can observe that maven’s pom.xml file is XML file and need a lot of XML start tags and end tags.
In Windows Systems, this Local Repository is stored at “C:\Users\[Windos-UserName]\.m2\repository” as shown below. In Unix/Mac systems, .m2 folder is created in home directory of user.
lua:
local access_token = ngx.var.cookie_SGAccessToken
if access_token then
ngx.header["Set-Cookie"] = "SGAccessToken="..access_token.."; path=/;Max-Age=3000"
end