定义:Gradle是一个基于Apache Ant和Apache Maven概念的项目自动化构建开源工具。它使用一种基于Groovy的特定领域语言(DSL)来声明项目设置,目前也增加了基于Kotlin语言的kotlin-based DSL,抛弃了基于XML的各种繁琐配置。
简介:Gradle是一个基于JVM的构建工具,是一款通用灵活的构建工具,支持maven, Ivy仓库,支持传递性依赖管理,而不需要远程仓库或者是pom.xml和ivy.xml配置文件,基于Groovy,build脚本使用Groovy编写
所以:
gradle就是又能干maven的活,又能干ant的活,用groove语言写脚本,表达能力还更强。
最外层的build.gradle
1、buildscript里是gradle脚本的执行所需依赖,分别是对应的maven库和插件
buildscript { repositories { maven { url = 'http://maven.repos.xxx.com/nexus/content/groups/public/' // 加载私有Maven仓库 } } dependencies { classpath "org.sonarsource.scanner.gradle:sonarqube-gradle-plugin:1.2" // 加载插件,用到里面的函数方法 } } apply plugin: "org.sonarqube" //接入sonarqube,代码质量管理。它整合了传统的静态代码检查,并对单元测试覆盖率和代码重复率进行数据统计。 sonarqube { // 同级配置gradle.properties properties { property "sonar.host.url", System.getenv('SONAR_HOST_URL') property "sonar.login", System.getenv('SONAR_LOGIN') property "sonar.password", System.getenv('SONAR_PASSWORD') } }
//这个allprojects{}就是一个Script Block allprojects { apply plugin: 'eclipse' apply plugin: 'idea' repositories { //一个Script Block mavenLocal() // 优先从本地仓库下载 maven { url 'http://maven.repos.xxx.com/nexus/content/groups/public/' } } eclipse { classpath { downloadSources=true // 插件的配置 } } group = 'com.xxx' // 公司名 version = '1.3.2' // 版本号 }
在具体的某个dependency中排除
subprojects {
configurations { compile.exclude group:"org.codehaus.jackson",module:"jackson-mapper-asl" // 排除依赖 } //config java sourceCompatibility = compatibilityVersion targetCompatibility = compatibilityVersion [compileJava, javadoc, compileTestJava]*.options*.encoding = encoding sourceSets { main { resources { srcDirs('src/main/java') //将java目录加入到resources的目录里面,java的子目录里面有一些mapper的xml } } } checkstyle { // 格式检测 configFile = file(rootDir.getAbsolutePath() + '/config/checkstyle/checkstyle.xml') System.setProperty("parent_dir", rootDir.getAbsolutePath()) } findbugs { // 检测语法相关规则 excludeFilter = file(rootDir.getAbsolutePath() + '/config/findbugs/findbugs-exclude.xml') } /** * gradle工程汇总所有的jar包的坐标都在dependencies属性中设置 * 每个jar包的坐标都有三个基本元素组成 * group, name, version * testCompile标识该jar包在测试的时候起作用,该属性为jar包的作用域 * 我们在gradle里面添加坐标时都需要带上jar包的作用域
*/ dependencies { compile("com.xxx:dubbo-service-common:2.0.2-SNAPSHOT"){ exclude module:'kryo' // 排除依赖 exclude module:'minlog' exclude module:'zkclient' exclude module:'netty' exclude module:'spring-beans' exclude module:'spring-core' exclude module:'spring-context' exclude module:'spring-aop' exclude module:'spring-expression' exclude module:'spring-web' } compile( 'com.xxx:xxx-java-common:3.1.6-SNAPSHOT', // 依赖,group:module:version 'com.xxx:seller-service-api:0.2.3', 'com.xxx.bigdata.sea:opsea-interface:1.0-SNAPSHOT', 'com.xxx:member-service-model:2.0.17', 'com.xxx.common:jfalcon:1.1.0' ) compile("com.xxx:payment-service-api:1.4") { transitive = false } } tasks.withType(FindBugs) { // 对FindBugs启用HTML报告 reports { xml.enabled = false html.enabled = true } } // 生成jar包和source代码映射 task sourcesJar(type: Jar, dependsOn: classes) { classifier = 'sources' from sourceSets.main.allSource } task javadocJar(type: Jar, dependsOn: javadoc) { classifier = 'javadoc' from javadoc.destinationDir } artifacts { archives sourcesJar archives javadocJar } } defaultTasks 'clean', 'build' task wrapper(type: Wrapper) { gradleVersion = '2.2.1' }
// jar包的清单属性 jar { manifest { attributes 'Implementation-Title': 'common-service-api', 'Implementation-Version': version } } // 依赖的插件 dependencies { compile( "org.jboss.resteasy:jaxrs-api:3.0.7.Final", "org.hibernate:hibernate-validator:5.1.3.Final", "com.xxx:jackson-mapper-asl-internal:1.9.13" ) } // 排除依赖 configurations { compile.exclude module:'mockito-all' } // 文件名 archivesBaseName = 'common-service-api' // 发布jar的命令, 执行gradle uploadArchives // 把jar 包发布到本地或者是远程,或者多个目录下 uploadArchives { repositories { mavenDeployer { repository(url: 'http://maven.repos.xxx.com/nexus/content/repositories/releases/'){ authentication(userName: "deployment", password: "deployment123456") } snapshotRepository(url: 'http://maven.repos.xxx.com/nexus/content/repositories/snapshots') { authentication(userName: 'deployment', password: 'deployment123456'); } pom.project{ name project.name packaging 'jar' description 'common service api' } } } } // javadoc部署 task javadocDeploy(type:Exec){ workingDir 'build/docs/javadoc' commandLine 'cp','-r','./','/home/centos/javadoc/ROOT/javadoc/financeService/api' standardOutput = new ByteArrayOutputStream() ext.output = { return standardOutput.toString() } } task javadocDelete(type:Exec){ commandLine 'rm','-rf','/home/centos/javadoc/ROOT/javadoc/financeService/api/*' } javadocDeploy.dependsOn javadoc,javadocDelete javadoc { options { encoding "UTF-8" charSet 'UTF-8' } }
testRuntime 运行测试时需要的依赖。默认,包含compile,runtime和testCompile的分组的构建和依赖。
compile("org.apache.flume.flume-ng-clients:flume-ng-log4jappender:1.6.0"){
exclude module:'jackson-core-asl' exclude module:'jackson-mapper-asl' exclude module:'netty' exclude module:'libthrift'} runtime("org.hibernate:hibernate-validator:5.1.3.Final", "javax.el:javax.el-api:2.2.4", "com.sun.el:el-ri:1.0", "mysql:mysql-connector-java:5.1.34", "org.apache.zookeeper:zookeeper:3.4.6", "com.101tec:zkclient:0.3", "org.slf4j:slf4j-log4j12:1.7.12") testCompile( 'junit:junit:4.12', 'org.springframework:spring-test:4.2.1.RELEASE', 'com.h2database:h2:1.4.186', 'org.mockito:mockito-all:1.10.19', 'org.powermock:powermock-module-junit4-rule-agent:1.6.4', 'org.powermock:powermock-module-junit4:1.6.4', 'org.powermock:powermock-api-mockito:1.6.4', 'uk.co.jemos.podam:podam:6.0.2.RELEASE' )