Gradle quick start

What is Gradle

To me, i use it to replace ‘make’ and ‘autotools’.

Download/Install

Download and Install/Java Quick Start.
Then configure it in .bashrc:

export GRADLE_OPTS="-DsocksProxyHost=127.0.0.1 -DsocksProxyPort=9999"
#export GRADLE_OPTS=-Dorg.gradle.daemon=true
export GRADLE_HOME=/home/y/.programs/gradle-2.5
export PATH=$GRADLE_HOME/bin:$PATH

gradle directory structure

default

By default Gradle uses standard Maven project structure.
Reference:
https://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html
https://docs.gradle.org/current/userguide/java_plugin.html

src/main/java --> java source code
src/main/resources --> other files needed, like logback.xml and log4j.properties
src/test/java --> unit tests

After “gradle build”, a directory “build” will appear, jar file is in build/libs.

get resource file in code:

File f1 = new File(getClass().getClassLoader().getResource("binary_tree_1.ser").getFile());

改变默认src/res位置

如果是android project,sourceSets需要放到android object里面

sourceSets {
    main {
        java {
            srcDir 'src'
        }  
        res {
            srcDir 'res'
        }  
        resources {
            srcDir 'src'
        }
        assets {
            srcDir 'assets'
        }
        manifest {
            srcFile 'AndroidManifest.xml'
        }
    }//main
}//sourceSets

Gradle support in vim/IDE

gradle support in VIM:(it needs pathogen.vim) by vim-gradle

gradle is also supported in Intellij.

文档

google提供的配置android的gradle
https://developer.android.com/tools/building/configuring-gradle.html

常用配置

配置静态变量

buildTypes {
    debug {
        buildConfig "public final static boolean HAS_PROTOTYPE_FEATURE = true;"
    }
    release {
        buildConfig "public final static boolean HAS_PROTOTYPE_FEATURE = false;"
    }
}

配置依赖项目

dependencies {
    compile project(':library')
}

配置依赖jar目录

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
}

配置依赖jar文件

dependencies {
    compile files (
        'libs/gcm.jar',
        '...more-jar-files...'
    )
}

配置gradle run

可执行任务gradle run相当于java -jar xxx

task pack_clear(type: JavaExec, dependsOn:[':pack_ramdisk_and_gz', 'build']) {
    description '运行指定main函数的java'
    classpath = sourceSets.main.runtimeClasspath
    main = "cfig.bootimg.repack_with_cmd"
    args rootProject.outClearIMg, rootProject.rootWorkDir, rootProject.mkbootimgBin
}

对于gradle, 如果mai在src/main/groovy/cfig/exec.groovy, 则main = “cfig.exec”

指定gradle版本

buildscript {
    repositories {
          mavenCentral()
    }
    dependencies {
          classpath 'com.android.tools.build:gradle:1.3.1'
    }
}

两步编译jar

load进plugin

apply plugin: 'java'

指定main class

jar {
    manifest {
        attributes 'Main-Class': 'org.cfig.gradle.Hi'
    }
}

或者定制更多:

jar() {
    from {configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }}
    archivesBaseName = "Hi"
    manifest {
        attributes 'Implementation-Title': 'Good Day',
                   'Implementation-Version': '1.0',
                   'Main-Class': 'cfig.Hi',
                   'BUILD_TIME': '2015.11.11'
    }
}

或者创建类型为Jar的task

task mkbootimg(type: Jar, dependsOn:['build']) {
    from files(sourceSets.main.output.classesDir)
    from configurations.runtime.asFileTree.files.collect { zipTree(it) }
    baseName = 'mkbootimg'
    manifest {
        attributes 'Main-Class': 'cfig.bootimg.mkbootimg'
    }
}

using gradlew

read this first.
add the following block to the bottom of your build.gradle

task wrapper(type: Wrapper) {
    gradleVersion = '4.0'
}

Run the following command to download and initialize the wrapper scripts:

gradle wrapper

enable stdout in test

test {
    testLogging {
        showStandardStreams = true
    }
}

forece a test task even everything is UP-TO-DATE

gradle test --rerun-tasks

init gradle project

gradle init --type basic
gradle init --type groovy-library
gradle init --type java-library