What’s Gradle?

Gradle is build automation evolved. Gradle can automate the building, testing, publishing, deployment and more of software packages or other types of projects such as generated static websites, generated documentation or indeed anything else.

Gradle combines the power and flexibility of Ant with the dependency management and conventions of Maven into a more effective way to build. Powered by a Groovy DSL and packed with innovation, Gradle provides a declarative way to describe all kinds of builds through sensible defaults. Gradle is quickly becoming the build system of choice for many open source projects, leading edge enterprises and legacy automation challenges.

How to start with Gradle?

Download the latest Gradle release from http://www.gradle.org/downloads

Set up the environmental  var “GRADLE_HOME“.

Add GRADLE_HOME/bin to your PATH environment variable. Usually, this is sufficient to run Gradle.

To check if Gradle is properly installed just type ‘gradle -v’ in command line.

And make sure you have set up the ‘ANT_HOME’ and ‘JAVA_HOME’

Gradle Script Basics Overview

Everything in Gradle sits on top of two basic concepts: projects and tasks.

Every Gradle build is made up of one or more projects. A project represents some component of your software which can be built. What this means exactly depends on what it is that you are building. For example, a project might represent a library JAR or a web application. It might represent a distribution ZIP assembled from the JARs produced by other projects. A project does not necessarily represent a thing to be built. It might represent a thing to be done, such as deploying your application to staging or production environments. Don’t worry if this seems a little vague for now. Gradle’s build-by-convention support adds a more concrete definition for what a project is.

Example The first build script

build.gradle

task hello { doLast { println 'Hello world!' } }

In a command-line shell, enter into the containing directory and execute the build script by running gradle -q hello:

shortcut task definition

build.gradle

task hello << { println 'Hello world!' }

Using Groovy in Gradle’s tasks

task upper << {
      String someString = 'mY_nAmE'
      println "Original: " + someString
      println "Upper case: " + someString.toUpperCase() }

Use Groovy Iterator :

task count << {
     4.times {
        print "$it " } }

Output of gradle -q count

> gradle -q count
0 1 2 3

Task dependencies

task hello << {
        println 'Hello world!' }

task intro(dependsOn: hello) << {
        println "I'm Gradle" }

Output of gradle -q intro

> gradle -q intro
Hello world!
I'm Gradle

Accessing a task via API

task hello << {
     println 'Hello Earth' }
hello.doFirst {
     println 'Hello Venus' }
hello.doLast {
     println 'Hello Mars' }
hello << {
     println 'Hello Jupiter' }

Output of gradle -q hello

> gradle -q hello
Hello Venus
Hello Earth
Hello Mars
Hello Jupiter

How to build a JAVA Project

Using the Java plugin

apply plugin: ‘java’

This is all you need to define a Java project. This will apply the Java plugin to your project, which adds a number of tasks to your project.

Adding Maven repository

repositories {
    mavenCentral()
}

This means use maven central repository . And also you can add other external maven repository.

Adding dependencies

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

Customization of MANIFEST.MF

sourceCompatibility = 1.5
version = '1.0'
jar {
   manifest {
      attributes 'Implementation-Title': 'Gradle Quickstart', 'Implementation-Version': version
             }
    }

Publishing the JAR file

uploadArchives {
    repositories {
       flatDir {
         dirs 'repos' } } }

Creating an Eclipse project

apply plugin: 'eclipse'

Now execute gradle eclipse command to generate Eclipse project files.

Java example – complete build file

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

And now you can use  gradle build to build this project

Output of gradle build

> gradle build
:compileJava
:processResources
:classes
:jar
:assemble
:compileTestJava
:processTestResources
:testClasses
:test
:check
:build

BUILD SUCCESSFUL

Total time: 1 secs

Some other useful tasks are:

Multi-project Java build

hierarchical layout

  multiproject

  api/

services/webservice/

shared/

Here we have three projects. Project api produces a JAR file which is shipped to the client to provide them a Java client for your XML webservice. Project webservice is a webapp which returns XML. Project shared contains code used both by api and webservice.

To define a multi-project build, you need to create a settings file. The settings file lives in the root directory of the source tree, and specifies which projects to include in the build. It must be called settings.gradle. For this example, we are using a simple hierarchical layout. Here is the corresponding settings file:

include "shared", "api", "services:webservice"

Dependencies between projects

dependencies {
    compile project(':shared') }

This script will add currernt project denpendency with ‘shard’

Dependency Management

 

apply plugin: 'java'
repositories {
    mavenCentral() }
dependencies {
    compile group: 'org.hibernate', name: 'hibernate-core', version: '3.6.7.Final'
    testCompile group: 'junit', name: 'junit', version: '4.+' }
dependencies {
    compile 'org.hibernate:hibernate-core:3.6.7.Final' }

Usage of a remote Maven repository

repositories {
    maven {
     mavenCentral()
     url "http://repo.mycompany.com/maven2"
    } }</strong>

This script will find jars in the mavenCentral first and then find in the ‘http://repo.mycompany.com/maven2’

2 thoughts on “Gradle入门(DreamFroce精选版)”

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注