본문 바로가기

Gradle

[gradle] Building and Testing with gradle 공부 요약 - 나머지 -

1. define variable

def helloName="need4spd"
int personAge = 35
task hello << {
println "Hello ${helloName}. ${personAge} years old."
}
//run
Jang-ui-MacBook-Pro:gradleTest need4spd$ gradle -q -b definevar.gradle hello
Hello need4spd. 35 years old.


2. using ant task
task hello << {
String greeting = "hello from ant"
ant.echo(message: greeting)
}
//run
Jang-ui-MacBook-Pro:gradleTest need4spd$ gradle -b helloant.gradle hello
:buildSrc:compileJava UP-TO-DATE
:buildSrc:compileGroovy UP-TO-DATE
:buildSrc:processResources UP-TO-DATE
:buildSrc:classes UP-TO-DATE
:buildSrc:jar UP-TO-DATE
:buildSrc:assemble UP-TO-DATE
:buildSrc:compileTestJava UP-TO-DATE
:buildSrc:compileTestGroovy UP-TO-DATE
:buildSrc:processTestResources UP-TO-DATE
:buildSrc:testClasses UP-TO-DATE
:buildSrc:test UP-TO-DATE
:buildSrc:check UP-TO-DATE
:buildSrc:build UP-TO-DATE
:buildSrc:compileJava UP-TO-DATE
:buildSrc:compileGroovy UP-TO-DATE
:buildSrc:processResources UP-TO-DATE
:buildSrc:classes UP-TO-DATE
:buildSrc:jar UP-TO-DATE
:buildSrc:assemble UP-TO-DATE
:buildSrc:compileTestJava UP-TO-DATE
:buildSrc:compileTestGroovy UP-TO-DATE
:buildSrc:processTestResources UP-TO-DATE
:buildSrc:testClasses UP-TO-DATE
:buildSrc:test UP-TO-DATE
:buildSrc:check UP-TO-DATE
:buildSrc:build UP-TO-DATE
:hello
[ant:echo] hello from ant
BUILD SUCCESSFUL
Total time: 3.493 secs
Jang-ui-MacBook-Pro:gradleTest need4spd$
view raw helloant.gradle hosted with ❤ by GitHub


3. zip with ant
task zipsourceInGradle << {
ant.zip(destfile: 'samples-from-gradle.zip') {
fileset(dir: 'samples') {
include(name: '**.txt')
}
}
}
view raw antzip.gradle hosted with ❤ by GitHub


4. task list when java build
from : http://www.gradleware.com/registered/books/building-and-testing/maven-and-gradle.html#_cue_graven
Example 4.2. The simplest Maven equivalent build.gradle file
apply plugin: 'java'
That one line Gradle build file in Example 4.2, “The simplest Maven equivalent build.gradle file”, when executed with a mere gradle build from the command line, performed the following actions:
Downloaded any declared dependencies (none) to ~/.gradle/cache
Compiled the code in src/main/java
Wrote the class files into build/classes/main
Attempted to compile and run any unit tests (none)
Wrote unit test results in XML format to build/test-results/
Wrote an HTML-formatted unit test report to build/reports/tests/
Generated a MANIFEST.MF in build/tmp/jar/MANIFEST.MF
Compressed the .class files along with the MANIFEST.MF into a JAR in build/libs/maven-gradle-comparison-simple.jar
The described actions taken by Gradle are made evident by examining the files in the structure of the output directory.
Example 4.3. Listing of the build subdirectory from the Example 4.2, “The simplest Maven equivalent build.gradle file” Gradle build project
build
├── classes
│ └── main
│ └── Main.class
├── dependency-cache
├── libs
│ └── maven-gradle-comparison-simple.jar
├── reports
│ └── tests
│ ├── css3-pie-1.0beta3.htc
│ ├── index.html
│ ├── report.js
│ └── style.css
├── test-results
└── tmp
└── jar
└── MANIFEST.MF
// with version
apply plugin: 'java'
version = '0.0.1-SNAPSHOT'
$ tree
├── classes
│ └── main
│ └── Main.class
├── dependency-cache
├── libs
│ └── maven-gradle-comparison-withattrs-0.0.1-SNAPSHOT.jar
├── reports
│ └── tests
│ ├── css3-pie-1.0beta3.htc
│ ├── index.html
│ ├── report.js
│ └── style.css
├── test-results
└── tmp
└── jar
└── MANIFEST.MF
view raw to do hosted with ❤ by GitHub


5. dependencies
apply plugin: 'java'
apply plugin: 'groovy'
group = 'com.gradleware.samples'
version = '0.0.1-SNAPSHOT'
repositories {
mavenCentral()
}
dependencies {
compile 'commons-beanutils:commons-beanutils:1.8.3'
//default 'junit:junit:4.8.2'
testCompile 'mule:mule-extras-groovy:1.1.1'
//testRuntime
//archives
//runtime
}
//run
Jang-ui-MacBook-Pro:gradleTest need4spd$ gradle -b libdependencies.gradle dependencies
:buildSrc:compileJava UP-TO-DATE
:buildSrc:compileGroovy UP-TO-DATE
:buildSrc:processResources UP-TO-DATE
:buildSrc:classes UP-TO-DATE
:buildSrc:jar UP-TO-DATE
:buildSrc:assemble UP-TO-DATE
:buildSrc:compileTestJava UP-TO-DATE
:buildSrc:compileTestGroovy UP-TO-DATE
:buildSrc:processTestResources UP-TO-DATE
:buildSrc:testClasses UP-TO-DATE
:buildSrc:test UP-TO-DATE
:buildSrc:check UP-TO-DATE
:buildSrc:build UP-TO-DATE
:buildSrc:compileJava UP-TO-DATE
:buildSrc:compileGroovy UP-TO-DATE
:buildSrc:processResources UP-TO-DATE
:buildSrc:classes UP-TO-DATE
:buildSrc:jar UP-TO-DATE
:buildSrc:assemble UP-TO-DATE
:buildSrc:compileTestJava UP-TO-DATE
:buildSrc:compileTestGroovy UP-TO-DATE
:buildSrc:processTestResources UP-TO-DATE
:buildSrc:testClasses UP-TO-DATE
:buildSrc:test UP-TO-DATE
:buildSrc:check UP-TO-DATE
:buildSrc:build UP-TO-DATE
:dependencies
------------------------------------------------------------
Root project
------------------------------------------------------------
archives - Configuration for archive artifacts.
No dependencies
compile - Classpath for compiling the main sources.
Download http://repo1.maven.org/maven2/commons-beanutils/commons-beanutils/1.8.3/commons-beanutils-1.8.3.pom
Download http://repo1.maven.org/maven2/org/apache/commons/commons-parent/14/commons-parent-14.pom
\--- commons-beanutils:commons-beanutils:1.8.3
\--- commons-logging:commons-logging:1.1.1
default - Configuration for default artifacts.
\--- commons-beanutils:commons-beanutils:1.8.3
\--- commons-logging:commons-logging:1.1.1
groovy - The Groovy libraries to be used for this Groovy project.
No dependencies
runtime - Classpath for running the compiled main classes.
\--- commons-beanutils:commons-beanutils:1.8.3
\--- commons-logging:commons-logging:1.1.1
testCompile - Classpath for compiling the test sources.
Download http://repo1.maven.org/maven2/mule/mule-extras-groovy/1.1.1/mule-extras-groovy-1.1.1.pom
+--- commons-beanutils:commons-beanutils:1.8.3
| \--- commons-logging:commons-logging:1.1.1
\--- mule:mule-extras-groovy:1.1.1
testRuntime - Classpath for running the compiled test classes.
+--- commons-beanutils:commons-beanutils:1.8.3
| \--- commons-logging:commons-logging:1.1.1
\--- mule:mule-extras-groovy:1.1.1
BUILD SUCCESSFUL
Total time: 10.469 secs


6. custom repositories
repositories {
add(new FileSystemResolver()) {
name = "repo"
addArtifactPattern("$rootDir/repo/[organization]/[module]-[revirsion].[ext]")
addIvyPattern("$rootDir/repo/[organization]/ivy-[module]-[revision].xml")
//addArtifactPattern("http://repo.gradleware.org/[organization]/[module]/[revision]/[artifact]-[revision]/[ext]")
checkmodified = true
}
}


7. multiple source directory
apply plugin: 'java'
sourceSets.main.java.srcDirs =
["src/main/java", "srcAdditional/main/java"]
// This add (while maintaining the default src/main/java)
// can also be accomplished with a call:
sourceSets.main.java.srcDirs 'srcAdditionalTwo/main/java'


8. default task
apply plugin: 'java'
//Alternate approach 1
//defaultTasks = ['clean', 'build']
//Alternate approach 2
//defaultTasks 'clean'
//Approach 3
defaultTasks 'clean', 'build'


9. publish
apply plugin: 'java'
apply plugin: 'maven'
group = 'com.gradleware.samples'
uploadArchives {
repositories.mavenDeployer {
repository(url: "file:///Users/mccm06/Documents/Temp/Scratch/mytemprepo/")
}
}