gradle教程_Gradle教程

gradle教程

Welcome to Gradle Tutorial. In my earlier posts, we looked into What is Gradle and Gradle Eclipse Plugin.

欢迎使用Gradle教程。 在我之前的文章中,我们研究了什么是Gradle和Gradle Eclipse插件 。

Gradle教程 (Gradle Tutorial)

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.

现在我们知道了如何将gradle安装到您的系统中并配置它的eclipse插件,现在该看一下gradle脚本了。 由于我们大多数从事Java技术工作的人员已经熟悉maven,因此我们将在maven和gradle构建脚本中讨论相同的元素,以便新用户可以轻松掌握Gradle构建脚本知识。

This post is very important for gradle beginners to understand gradle build script. Please go through each and every section clearly.

对于Gradle初学者来说,这篇文章对Gradle构建脚本的理解非常重要。 请清楚地浏览每个部分。

Gradle教程– build.gradle (Gradle Tutorial – build.gradle)

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.

在讨论gradle教程要点时,我将引用上一篇文章中的build.gradle文件。 以下是我们较早的gradle示例项目中的build.gradle文件。

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'
       }
    }
}

Below is the explanation of different elements one by one.

以下是对不同元素的说明。

  1. Gradle教程– Java版本 (Gradle Tutorial – java version)

  2. 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:

    如果我们在第4行观察到,它是指sourceCompatibility的值为1.5 。 这意味着它正在引用Java 1.5。 默认情况下,Eclipse Gradle插件使用“ sourceCompatibility”元素引用Java 1.5,如下所示:

    sourceCompatibility = 1.5

    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.

    如果我们在Eclipse IDE中运行相同的gradle build命令,则不会看到这些警告,因为我们在IDE和Gradle的构建脚本文件中都使用了相同的Java版本。

    In build.gradle file, we use “sourceCompatibility” element to define java version to be used by gradle build tool.

    在build.gradle文件中,我们使用“ sourceCompatibility”元素定义gradle构建工具要使用的java版本。

  3. 应用所需的插件 (Apply required plugins)

  4. 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;

    以下Gradle构建脚本定义为:

    apply plugin: 'java'
    apply plugin: 'eclipse'
    
    sourceCompatibility = 1.7

    similar to the following maven build script definition.

    与以下Maven构建脚本定义类似。

    
      
    	
        org.apache.maven.plugins
        maven-eclipse-plugin
        3.1
            
              1.7
              1.7
            
      
     
    

    Here we are defining “maven-eclipse-plugin” plugin to use Java 1.7 version.

    在这里,我们定义了“ maven-eclipse-plugin”插件以使用Java 1.7版本。

    NOTE: Like Gradle Eclipse Plugin, Maven Eclipse Plugin uses Java 1.5 version as default value.

    注意:与Gradle Eclipse插件一样,Maven Eclipse插件使用Java 1.5版本作为默认值。

  5. 打包项目文件 (Packaging Project files)

  6. 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.

    在开发完任何项目或在不同的环境(Dev,QA,PROD等)中测试/部署项目之后,我们需要将其打包为所需的格式,例如Jar文件 ,War文件或EAR文件。

    Then how to define this kind of packaging in build.gradle file so that “gradle build” command will create that file at the end of build execution.

    然后,如何在build.gradle文件中定义这种包装,以便“ gradle build”命令将在构建执行结束时创建该文件。

    In Gradle, we need to use “jar” element to inform “gradle” command to create JAR File as shown below:

    在Gradle中,我们需要使用“ jar”元素来通知“ gradle”命令来创建JAR文件,如下所示:

    jar {
    }

    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:

    由于“ jar”是默认的汇编值,因此我们无需在gradle的构建脚本文件中应用jar插件。 但是,要创建WAR或EAR,我们需要应用相应的插件,如下所示:

    To create WAR file:

    要创建WAR文件:

    apply plugin: "war"
    war {
    }

    To create EAR file:

    要创建EAR文件:

    apply plugin: "ear"
    ear {
    }
  7. Jar文件名和版本 (Jar file name and version)

  8. Jar Filename: By default, gradle uses project name as jar file name. In our example, our Project name is “JavaGradleSimpleExample”.

    Jar文件名 :默认情况下,gradle使用项目名称作为jar文件名。 在我们的示例中,我们的项目名称为“ JavaGradleSimpleExample”。

    Jar File Version: We need to define project jar file version using “version” element in build.gradle file as shown below.

    Jar文件版本 :我们需要使用build.gradle文件中的“ version”元素定义项目jar文件版本,如下所示。

    version = '1.0'

    This Gradle element is similar to the following Maven element.

    此Gradle元素类似于以下Maven元素。

    1.0

    Finally, our project jar file name = Project name + Version name. That’s why our jar file name is JavaGradleSimpleExample-1.0.jar.

    最后,我们的项目jar文件名=项目名称+版本名称。 这就是我们的jar文件名为JavaGradleSimpleExample-1.0.jar

  9. 如何提供我们所需的项目名称和版本 (How to provide our required Project name and Version)

  10. 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.

    它将创建一个名为以下名称的WAR文件:MyJavaGradleProject-2.0.war。 我们可以将此war文件部署到任何Web或Application Server(例如Tomcat,Weblogic等)中,也可以使用“ java -jar”命令运行它。

    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”元素的优先级高于外部元素。
    内部“版本”元素将覆盖外部“版本”元素的值。

  11. Gradle依赖 (Gradle dependencies)

  12. 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.

    我们需要在build.gradle文件中使用“ dependencies”元素来定义我们的项目依赖项。 当我们定义依赖关系时,Gradle将从MavenRepository中检查那些jar文件,并将它们下载到本地,然后添加到我们的项目构建路径中。

    We have defined our “JavaGradleSimpleExample” Project’s dependencies as shown below:

    我们定义了“ JavaGradleSimpleExample”项目的依赖项,如下所示:

    dependencies {
    compile group:'commons-collections',name:'commons-collections',version:'3.2'
    testCompile group: 'junit', name: 'junit', version: '4.+'
    }

    Here we use “compile” element to define project development dependencies and “testCompile” element to define JUnit/Testing Dependencies.

    在这里,我们使用“ compile”元素定义项目开发依赖关系,并使用“ testCompile”元素定义JUnit / Testing依赖关系。

    We can define above “dependencies” element even simpler as shown below:

    我们可以更简单地定义上面的“ dependencies”元素,如下所示:

    dependencies {
    compile("commons-collections:commons-collections:3.2")
    testCompile("junit:junit:4.+")
    }

    We can also use the following syntax to define “dependencies” element (without brackets):

    我们还可以使用以下语法来定义“ dependencies”元素(不带括号):

    dependencies {
    compile "commons-collections:commons-collections:3.2"
    testCompile "junit:junit:4.+"
    }

    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.

    但是我们应该注意以相同的顺序定义它们:组,名称然后是版本。 而不是逗号(,),我们需要使用冒号(:)运算符将它们分开。

    Like maven, in gradle build script also we define group, name and version of each dependency jar file.

    像maven一样,在gradle构建脚本中,我们还定义了每个依赖项jar文件的组,名称和版本。

    This gradle element definition;

    此gradle元素定义;

    dependencies {
    compile group:'commons-collections',name:'commons-collections',version:'3.2'
    }

    is similar to the following Maven definition

    与以下Maven定义相似

    
    	
    		commons-collections
    		commons-collections
    		3.2
    	
    

    That means Gradle’s “group” element is similar to Maven’s “groupId” element.

    这意味着Gradle的“ group”元素类似于Maven的“ groupId”元素。

    Gradle’s “name” element is similar to Maven’s “artifactId” element and Gradle’s “version” element is similar to Maven’s “version” element.

    Gradle的“ name”元素类似于Maven的“ artifactId”元素,而Gradle的“ version”元素类似于Maven的“ version”元素。

  13. Gradle资料库定义 (Gradle Repository Definitions)

  14. We use the following Gradle build script element to define our required repository to connect and download our project dependencies to the Local Repository.

    我们使用以下Gradle构建脚本元素来定义所需的存储库,以连接我们的项目依赖项并将其下载到本地存储库。

    repositories {
        mavenCentral()
    }

    Here we are informing to Gradle eclipse plugin that use maven repository to download required dependencies.

    在这里,我们通知Gradle eclipse插件,该插件使用maven存储库下载所需的依赖项。

将Gradle转换为Maven (Convert Gradle to Maven)

Finally, if we convert our Gradle’s build.gradle build file into Maven’s pom.xml file, it will looks like the following:

最后,如果我们将Gradle的build.gradle构建文件转换为Maven的pom.xml文件,它将类似于以下内容:



  4.0.0
  com.journaldev
  JavaGradleSimpleExample
  1.0
  jar

    
      
           commons-collections
           commons-collections
           3.2
      
      
           junit
           junit
           4.1
      
    

    
       
           
               org.apache.maven.plugins
               maven-eclipse-plugin
               3.1
               
                 1.7
                 1.7
               
           
     

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.

如果同时观察Gradle build.gradle文件和maven pom.xml文件,则可以观察到maven的pom.xml文件是XML文件,并且需要很多XML起始标记和结束标记。

Where as Gradle’s build.gradle file is plan text and simple file. This is one of the greatest advantages of gradle over maven.

其中Gradle的build.gradle文件是计划文本和简单文件。 这是gradle相对于maven的最大优势之一。

Maven / Gradle本地存储库 (Maven/Gradle Local Repository)

When we execute Maven/Gradle commands, they will interact with online maven repository and download required jars into Local repository.

当我们执行Maven / Gradle命令时,它们将与在线Maven存储库进行交互并将所需的jar下载到本地存储库中。

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.

在Windows系统中,此本地存储库存储在“ C:\ Users \ [Windos-UserName] \。m2 \ repository”中 ,如下所示。 在Unix / Mac系统中,.m2文件夹在用户的主目录中创建。

That’s all about gradle tutorial. I have covered most of the gradle build script elements that you will use.

这就是有关gradle教程的全部内容。 我已经介绍了您将使用的大多数gradle构建脚本元素。

Reference: Official User Guide

参考: 官方用户指南

翻译自: https://www.journaldev.com/8179/gradle-tutorial

gradle教程

你可能感兴趣的:(java,maven,gradle,jar,linux)