1. copy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
task copyFiles(type: Copy) { | |
from 'resources' | |
into 'target' | |
include '**/*.xml', '**/*.txt', '**/*.properties' | |
} |
2. jar
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
apply plugin: 'java' | |
task customJar(type: Jar) { | |
manifest { | |
attributes firstKey: 'firstValue', secondKey: 'secondValue' | |
} | |
archiveName = 'hello.jar' | |
destinationDir = file("${buildDir}/jars") | |
from sourceSets.main.classes | |
} |
3. javaexec
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
apply plugin: 'java' | |
repositories { | |
mavenCentral() | |
} | |
dependencies { | |
runtime 'commons-codec:commons-codec:1.5' | |
} | |
task encode(type: JavaExec, dependsOn: classes) { | |
main = 'org.gradle.example.commandline.MetaphoneEncoder' | |
args = "The rain in Spain falls mainly in the plain".split().toList() | |
classpath sourceSets.main.classesDir | |
classpath configurations.runtime | |
} |
4. Custom task type - in Build Files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
task createDatabase(type: MySqlTask) { | |
sql = 'CREATE DATABASE IF NOT EXISTS example' | |
} | |
task createUser(type: MySqlTask, dependsOn: createDatabase) { | |
sql = "GRANT ALL PRIVILEGES ON example.* TO exampleuser@localhost IDENTIFIED BY 'passw0rd'" | |
} | |
task createTable(type: MySqlTask, dependsOn: createUser) { | |
username = 'exampleuser' | |
password = 'passw0rd' | |
database = 'example' | |
sql = 'CREATE TABLE IF NOT EXISTS users (id BIGINT PRIMARY KEY, username VARCHAR(100))' | |
} | |
class MySqlTask extends DefaultTask { | |
def hostname = 'localhost' | |
def port = 3306 | |
def sql | |
def database | |
def username = 'root' | |
def password = 'password' | |
@TaskAction | |
def runQuery() { | |
def cmd | |
if(database) { | |
cmd = "mysql -u ${username} -p${password} -h ${hostname} -P ${port} ${database} -e " | |
} | |
else { | |
cmd = "mysql -u ${username} -p${password} -h ${hostname} -P ${port} -e " | |
} | |
project.exec { | |
commandLine = cmd.split().toList() + sql | |
} | |
} | |
} |
5. Custom task type - source tree
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
task createDatabase(type: MySqlTask) { | |
sql = 'CREATE DATABASE IF NOT EXISTS example' | |
} | |
task createUser(type: MySqlTask, dependsOn: createDatabase) { | |
sql = "GRANT ALL PRIVILEGES ON example.* TO exampleuser@localhost IDENTIFIED BY 'passw0rd'" | |
} | |
task createTable(type: MySqlTask, dependsOn: createUser) { | |
username = 'exampleuser' | |
password = 'passw0rd' | |
database = 'example' | |
sql = 'CREATE TABLE IF NOT EXISTS users (id BIGINT PRIMARY KEY, username VARCHAR(100))' | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.gradle.api.DefaultTask | |
import org.gradle.api.tasks.TaskAction | |
class MySqlTask extends DefaultTask { | |
def hostname = 'localhost' | |
def port = 3306 | |
def sql | |
def database | |
def username = 'root' | |
def password = 'password' | |
@TaskAction | |
def runQuery() { | |
def cmd | |
if(database) { | |
cmd = "mysql -u ${username} -p${password} -h ${hostname} -P ${port} ${database} -e " | |
} | |
else { | |
cmd = "mysql -u ${username} -p${password} -h ${hostname} -P ${port} -e " | |
} | |
project.exec { | |
commandLine = cmd.split().toList() + sql | |
} | |
} | |
} |
된다.. 일단은 .gradle 디렉토리를 삭제해보라는 stackoverflow 답변에 따라 해보니 실행은 되며, 추가로 알게된 것은 만약 MySqlTask가 package를 가지고 있을때는, import를 해주는 구문이 들어가야 한다고 한다.
http://stackoverflow.com/questions/16209354/gradle-could-not-find-property-in-buildsrc