grade用在android环境被大家所熟知,其实grade是一种流行的编译框架,也可以用在编译java环境上。
java工程的编译史是,JAVA工程一开始是用javac,但是构建一个项目需要做到:
完成这一系列步骤的是项目构建工具,先后有了ANT, Maven,Gradle
buildscript {
repositories {
mavenCentral()
}
dependencies {
}
}
buildscript中定义了脚本需要使用的资源。包括依赖项、第三方插件、maven仓库地址等。gradle在执行脚本时,第一执行buildscript代码块中的内容,然后执行剩余的build脚本。
“buildscript {}”块指定第三方库作为Gradle插件的话,指定插件使用“apply plugin”。比如接着在build.gradle继续写入。
apply plugin: "java"
apply plugin: "eclipse"
apply plugin: "idea"
apply plugin: "application"
// use jacocoTestReport task to create coverage report
apply plugin: "jacoco"
接着使用sourceCompatibility 和targetCompatibility定义jdk版本。分别是制定编译java文件字节码和java虚拟机兼容的版本号。
sourceCompatibility = 1.8
targetCompatibility = 1.8
接下来定义些常量。常量定义了常用组件的版本号,在后续脚本直接引用此常亮,后续有升级依赖版本号更方便。
def log4jVersion = "2.11.2"
def scalaVersion = "2.11"
def sparkVersion = "2.2.0"
def clouderaVersion = "cdh6.0.0"
//def kafkaVersion = "0.9.0-kafka-2.0.2"
def kafkaVersion = "0.8.2.1"
def jacksonVersion = "2.9.9"
使用repository定义依赖仓库:
repositories {
mavenCentral()
// spark
maven {
url "https://repository.cloudera.com/artifactory/cloudera-repos/"
}
}
mainClassName="com.xxx.xxx"
configurations.all {
resolutionStrategy {
force "io.netty:netty-all:4.1.6.Final"
force "io.netty:netty-tcnative-boringssl-static:1.1.33.Fork23"
}
exclude group: 'org.slf4j', module: 'slf4j-log4j12'
}
buildscript代码块中的repositories和dependencies的使用方式与直接在build.gradle文件中一样。不同之处是在buildscript代码块中你可以对dependencies使用classpath声明。compile是build使用的依赖,而testCompile是编译测试用例所需要的依赖。
dependencies {
compile "com.googlecode.json-simple:json-simple:1.1"
compile("io.lettuce:lettuce-core:5.0.3.RELEASE") {
exclude group: 'org.apache.logging.log4j', module: 'log4j-slf4j-impl'
exclude group: "io.netty"
}
testCompile "info.solidsoft.mockito:mockito-java8:1.0.0-beta"
compile fileTree(dir: 'libs', include: ['*.jar'])
}
test {
systemProperty "io.lettuce.core.epoll", "false"
testLogging {
exceptionFormat "full"
events "passed", "skipped", "failed"
quiet {
events "failed"
exceptionFormat "full"
}
}
reports {
junitXml.enabled = true
html.enabled = true
}
if (Files.exists(Paths.get("reports"))) {
File out = File.createTempFile("testlog-", ".txt", new File("reports"))
beforeTest { descriptor ->
out.append("Running test: " + descriptor + "\n");
}
// listen to standard out and standard error of the test JVM(s)
onOutput { descriptor, event ->
out.append(event.message.replaceAll(/\n$/, "") + "\n")
}
}
}
开始构建会进入到此项脚本
gradle.taskGraph.whenReady {
}
接着可以定义各种task。
task spark(type: Jar) {
from sourceSets.main.output
baseName = 'spark'
}
compileJava.doLast {
tasks.instrument.execute()
}
当前最新的是这个wget https://downloads.gradle-dn.com/distributions/gradle-5.6.2-bin.zip。这个链接不能保证后面也有效。
让gradle去解析build.gradle文件或者执行./gradlew tasks列出所有可执行的task。
执行相应的tasks任务,比如./gradlew build,./gradlew test等等。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。