【Java】gradle簡單理解

簡介(百度百科)

定義:Gradle是一個基於Apache Ant和Apache Maven概念的項目自動化構建開源工具。它使用一種基於Groovy的特定領域語言(DSL)來聲明項目設置,目前也增長了基於Kotlin語言的kotlin-based DSL,拋棄了基於XML的各類繁瑣配置。
簡介:Gradle是一個基於JVM的構建工具,是一款通用靈活的構建工具,支持maven, Ivy倉庫,支持傳遞性依賴管理,而不須要遠程倉庫或者是pom.xml和ivy.xml配置文件,基於Groovy,build腳本使用Groovy編寫html

平常使用:java

  • gradle對多工程的構建支持很出色,工程依賴是gradle的第一公民。
  • gradle支持局部構建。 支持多方式依賴管理:包括從maven遠程倉庫、nexus私服、ivy倉庫以及本地文件系統的jars或者dirs
  • gradle是第一個構建集成工具,與ant、maven、ivy有良好的相容相關性。
  • 輕鬆遷移:gradle適用於任何結構的工程,你能夠在同一個開發平臺平行構建原工程和gradle工程。一般要求寫相關測試,以保證開發的插件的類似性,這種遷移能夠減小破壞性,儘量的可靠。這也是重構的最佳實踐。
  • gradle的總體設計是以做爲一種語言爲導向的,而非成爲一個嚴格死板的框架。
  • 免費開源

因此:
gradle就是又能幹maven的活,又能幹ant的活,用groove語言寫腳本,表達能力還更強。mysql

語句詳解

最外層的build.gradle
一、buildscript裏是gradle腳本的執行所需依賴,分別是對應的maven庫和插件web

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裏是項目自己須要的依賴,好比我如今要依賴我本身maven庫的xx庫,那麼我應該將maven {url ‘http://maven.repos.xxx.com/ne...‘}寫在這裏,而不是buildscript中,否則找不到。spring

//這個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'    // 版本號
}

三、排除傳遞依賴。排除傳遞依賴有多種緣由,遠程倉庫中不存在,運行時不須要,或者版本衝突。排除傳遞依賴的方式有兩種:sql

  • 直接在configuration中排除
  • 在具體的某個dependency中排除express

    subprojects {apache

    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'
}

二、common-service-api/build.gradlecentos

// 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'
    }
}

三、common-service-provider/build.gradleapi

  • compile 放在這個configuration下的依賴是在編譯代碼時所使用的,但它做爲一個分組,包含代碼和編譯所需的依賴。
  • runtime 代碼在運行時須要的依賴,默認,也會包含compile中的依賴。
  • testCompile 編譯測試代碼時所須要的依賴,默認,被編譯的代碼和代碼須要的編譯依賴也屬於該分組。
  • 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'
       )
相關文章
相關標籤/搜索