常用gradle命令

sl521100發表於2015-09-12
1.build.gradle

ext {
profile = "dev"
tag='web'
if (project.hasProperty('pro')) {

temp = project.property('pro')
profile= temp.split('_')[0]
tag= temp.split('_')[1]
}
tag=tag+".gradle"

proConfig= loadGroovyConfig()


println profile+":"+ tag
}


apply from :tag

def loadGroovyConfig() {
def configFile = file('config.groovy')
new ConfigSlurper(profile).parse(configFile.toURL()).toProperties()
}


//gradle publishToMavenLocal -Ppro=test_external

2.web.gradle


apply plugin: 'java'
apply plugin: 'maven'
apply plugin: 'war'
apply plugin: 'jetty'
apply plugin: 'idea'



group = 'com.infosky.icustoms'
version = '1.0'

description = """"""

sourceCompatibility = 1.6
targetCompatibility = 1.6

[compileJava, javadoc, compileTestJava]*.options*.encoding = 'UTF-8'

tasks.withType(JavaCompile) {
options.encoding = "UTF-8"
}

configurations {
mycompile
myprovided
myonlycompile
}
sourceSets{
main{
if(project.profile=='dev') {
compileClasspath += configurations.mycompile
compileClasspath += configurations.myprovided
compileClasspath += configurations.myonlycompile

runtimeClasspath += configurations.mycompile
runtimeClasspath += configurations.myprovided
}else{
compileClasspath += configurations.mycompile
compileClasspath += configurations.myprovided
compileClasspath += configurations.myonlycompile

runtimeClasspath += configurations.mycompile
}
}
}

repositories {
mavenLocal()
maven { url "http://172.21.129.57:8081/nexus/content/groups/public/" }
maven { url "http://repo.maven.apache.org/maven2" }
}
dependencies {

mycompile group: 'com.infosky.icustoms', name: 'icustoms-sso-client', version:'1.0', transitive: false

myonlycompile group: 'junit', name: 'junit', version:'3.8.1'


myprovided group: 'ccsplib.infosky', name: 'antlr-2.7.7', version:'1.1.1'

}

def loadGroovyConfig() {
def configFile = file('config.groovy')
new ConfigSlurper(profile).parse(configFile.toURL()).toProperties()
}
processResources {
from ('src/main/java') {
include '**/*.sql'
include '**/*.xml'
include '**/*.ftl'
include '**/*.properties'
}

println "begin replace profile!"
from(sourceSets.main.resources.srcDirs) {
filter(org.apache.tools.ant.filters.ReplaceTokens,
tokens: project.proConfig
)
}

println ant.getProperties()
// ant.copy(file: 'src/command.bar', tofile: 'dist/command.bat', encoding: 'sjis') {
// filterchain {
// replaceTokens {
// token(key: 'jarPath', value=jar.archivePath.name)
// }
// }
// }

println "end replace profile!"

}


idea {
module {
scopes.PROVIDED.plus += [configurations.myprovided,configurations.mycompile,configurations.myonlycompile]
}
}

3.client.gradle
ext {
libName = 'icustoms-sso-client'
}
apply plugin: 'java'
apply plugin: 'maven'
apply plugin: 'maven-publish'


group = 'com.infosky.icustoms'
version = '1.0'

description = """sso"""

sourceCompatibility = 1.6
targetCompatibility = 1.6

tasks.withType(JavaCompile) {
options.encoding = "UTF-8"
}


configurations {
mycompile
myprovided
}
sourceSets{
main{
resources {
// srcDirs=[ 'SourceCode/resources']
exclude '**'
}
java{
include 'sso/common/**/*.*'
include 'sso/client/**/*.*'
include 'sso/constants/*.*'


include 'sso/ticket/**/*.*'
}

if(project.profile=='dev') {
compileClasspath += configurations.mycompile
compileClasspath += configurations.myprovided

runtimeClasspath += configurations.mycompile
runtimeClasspath += configurations.myprovided
}else{
compileClasspath += configurations.mycompile
compileClasspath += configurations.myprovided


runtimeClasspath += configurations.mycompile
}
}
}

repositories {
mavenLocal()
maven { url "http://172.21.129.57:8081/nexus/content/groups/public/" }
maven { url "http://repo.maven.apache.org/maven2" }
}
dependencies {
mycompile group: 'ccsplib.infosky', name: 'framework-core-bin', version:'1.1.1'

}


processResources {
from ('src/main/java') {
// include '**/*.sql'
// include '**/*.xml'
// include '**/*.ftl'
// include '**/*.properties'
// include '**/*.vm'
exclude '**/*.java'
}

println "begin replace profile!"
from(sourceSets.main.resources.srcDirs) {
filter(org.apache.tools.ant.filters.ReplaceTokens,
tokens: project.proConfig
)
}
println "end replace profile!"

}

jar {
baseName libName
from sourceSets.main.output

include 'sso/common/**/*.*'
include 'sso/client/**/*.*'
include 'sso/constants/*.*'
include 'sso/config/*.*'
include 'sso/utils/*.*'
include 'sso/holder/*.*'
include 'sso/user/**/*.*'
include 'sso/bean/**/*.*'
}



task sourceJar(type: Jar) {
from sourceSets.main.allJava

include 'sso/common/**/*.*'
include 'sso/client/**/*.*'
include 'sso/constants/*.*'
include 'sso/config/*.*'
include 'sso/utils/*.*'
include 'sso/holder/*.*'
include 'sso/user/**/*.*'
include 'sso/bean/**/*.*'
}

publishing {
publications {
mavenJava(MavenPublication) {
groupId group
artifactId libName
version version

from components.java

artifact sourceJar {
classifier "sources"
}
}
}
}