Skip to content

Instantly share code, notes, and snippets.

@lifuzu
lifuzu / build.gradle
Last active December 31, 2015 02:39
Build phases gradle test
println 'This is executed during the configuration phase.'
task configurated {
println 'This is also executed during the configuration phase.'
}
task test << {
println 'This is executed during the execution phase.'
}
@lifuzu
lifuzu / build.gradle
Created December 12, 2013 05:23
Parse XML file in gradle
//Declaring the inputs and outputs of a task
//build.gradle
task transform {
ext.srcFile = file('mountains.xml')
ext.destDir = new File(buildDir, 'generated')
inputs.file srcFile
outputs.dir destDir
doLast {
println "Transforming source file."
@lifuzu
lifuzu / build.gradle
Created December 12, 2013 05:37
extract the class file to create a jar package
//1. Use the zipTree directly in the jar task rather than creating an intermediate zip.
//2. Use eachFile to change the relative path of each file to strip off the first folder.
jar {
from zipTree('runtime.jar') {
include 'classes/**/*.class'
eachFile { it.relativePath = it.relativePath.segments[1..-1] }
}
}
//Jar tasks have all of the CopySpec methods, which provide the eachFile abilities.
@lifuzu
lifuzu / build.gradle
Created December 12, 2013 05:52
Configure the snapshot and the release repository for Gradle
//configure the snapshot and the release repository in the 'Upload' task (e.g. the 'uploadArchives') task:
uploadArchives {
repositories {
mavenDeployer {
repository(url: 'http://myCompanyRepo.com:8081/releases') {
authentication(userName: 'admin', password: 'password');
}
snapshotRepository(url: 'http://myCompanyRepo.com:8081/snapshots') {
@lifuzu
lifuzu / build.gradle
Created December 12, 2013 05:52
Get the latest version for dependency
//get the latest version like
compile "junit:junit:+"
//or better specify at least the major version like
compile "junit:junit:4.+"
/*
* The plugins used for this build.
*/
/*
* Provides the compile, test, jar, etc tasks
*/
apply plugin: 'java'
/*
* Generates the Eclipse project files.
configurations {
apt
}
dependencies {
compile 'com.squareup.dagger:dagger:1.1.0'
apt 'com.squareup.dagger:dagger-compiler:1.1.0'
}
android.applicationVariants.all { variant ->
@lifuzu
lifuzu / version.gradle
Created December 14, 2013 01:49
Get the most recent tag from the current branch, which made by 'git tag -a master_1.2.3.123'
/*
* Gets the version name from the latest Git tag
*/
def getVersionName = { ->
def stdout = new ByteArrayOutputStream()
exec {
commandLine 'git', 'describe', '--tags'
standardOutput = stdout
}
@lifuzu
lifuzu / build.gradle
Created December 15, 2013 04:08
A copy task mapping source directory structure onto a new destination structure
//A copy task mapping source directory structure onto a new destination structure
task complexCopy(type: Copy) {
from('src/main/templates') {
include '**/*.gtpl'
into 'templates'
}
from('i18n')
from('config') {
exclude 'Development*.groovy'
into 'config'
@lifuzu
lifuzu / build.gradle
Created December 15, 2013 04:14
Renaming files
//Renaming files using regular expressions
task rename(type: Copy) {
from 'source'
into 'dest'
rename(/file-template-(\d+)/, 'production-file-$1.txt')
}
//Renaming files programmatically
task rename(type: Copy) {
from 'source'
into 'dest'