문서의 이전 판입니다!
task hello << { println "hello" } // 괄호하고 이름 task(hello) << { println "hello" } task(copy, type: Copy) { from(file('srcDir')) into(buildDir) } // 이름을 문자열로 task('hello') << { println "hello" } // tasks 에 추가 tasks.add(name: 'hello') << { println "hello" }
gradle tasks
시에 정보를 표시해 준다.build
등이 있다.dist { description = '태스크 설명' group = '태스크의 그룹' } // 혹은 dist.description = '태스크 설명' dist.group = '태스크의 그룹'
task something { ext.prop1 = 'xxx' ext.prop2 = 'yyy' } // 외부에서 something.prop1 으로 접근 가능
task hello
가 있을 때,hello.name
project.hello.name
tasks.hello.name
tasks['hello'].name
// 단일 선언 task myCopy(type: Copy) // 태스크의 메소드 호출 등으로 설정하기 1 Copy myCopy = task(myCopy, type: Copy) myCopy.from 'resources' myCopy.into 'target' myCopy.include('**/*.txt', '**/*.xml', '**/*.properties') // 설정 2 task(myCopy, type: Copy) .from('resources') .into('target') .include('**/*.txt', '**/*.xml', '**/*.properties') // 설정 3 task myCopy(type: Copy) myCopy { from 'resources' into 'target' include('**/*.txt', '**/*.xml', '**/*.properties') } // 설정 4, configure() 메소드 task myCopy(type: Copy) myCopy.configure { from('source') into('target') include('**/*.txt', '**/*.xml', '**/*.properties') } // 설정 5. 선언시 task copy(type: Copy) { from 'resources' into 'target' include('**/*.txt', '**/*.xml', '**/*.properties') }
description
프라퍼티를 설정하면 gradle tasks
에서 볼 수 있게 된다.onlyIf
// hello task에 대해 hello.onlyIf { !project.hasProperty('skipHello') } // 실행시 skipHello 프라퍼티 지정 gradle hello -PskipHello
task.enabled=true|false
이 값이 true여야만 해당 태스크가 실행된다.inputs
와 TaskOutputs outputs
프라퍼티가 있다. 이 값을 설정해주면 자동으로 UP-TO-DATE인지 검사하여 실행 여부를 결정한다.task transform { ext.srcFile = file('mountains.xml') ext.destDir = new File(buildDir, 'generated') inputs.file srcFile outputs.dir destDir doLast { println "Transforming source file." destDir.mkdirs() // outputs.dir 영역에 파일을 생성하는 코드.. } }
inputs/outputs를 사용하여 VCS에서 받은 멀티 프로젝트의 프로젝트별 갱신 여부를 검사할 수 있다.
task checkUpToDate { description = '프로젝트 최신 갱신 여부 검사' def checkFile = file(new File(tmpDir, "gradle_${project.name}_check_up_to_date").absoluteFile) FileTree projectFileTree = fileTree(dir: project.projectDir) projectFileTree.exclude "${builDir}/**/*" inputs.files projectFileTree outputs.file checkFile doLast { println "[${project.name}] needs refresh." if (checkFile.exists()) { checkFile.delete() } checkFile.createNewFile() } }
dependsOn [a, b]
형태로는 실행 순서를 지정할 수 없다. dependsOn 은 의존 대상을 명시할 뿐 의존 대상의 실행순서는 명시하지 않는다.이 때, 다음과 같은 방식으로 순서를 명확히 하는 것도 가능하다.
task somethingLastTask << { tasks.somethingFirstTask.execute() tasks.somethingSecondTask.execute() .... // do somthing last }
// 보통은 task1 -> task2 순서로 실행하지만 특정 상황에서는 이를 무시한다. task2.shouldRunAfter task1 // 무조건 task1 -> task2 순서를 지킨다. task2.shouldRunAfter task1
tasks.withType(TaskType) { … }
을 사용하여 특정 태스크 타입에 대한 공통 설정을 수행할 수 있다.// 태스크 선언부에서.. outputs.upToDateWhen { false }