[Java] simple understanding of gradle

Introduction (Baidu Encyclopedia)

Definition: Gradle is an open source tool for project automation building based on Apache Ant and Apache Maven concepts. It uses a Groovy-based Domain Specific Language (DSL) to declare project settings. At present, it also adds kotlin-based DSL based on Kotlin language, which abandons various tedious configurations based on XML.
Introduction: Gradle is a JVM-based building tool, a general and flexible building tool, supporting maven, Ivy warehouse, supporting transitive dependency management, without the need for remote warehouse or pom.xml and ivy.xml configuration files, based on Groovy, build scripts are written using Groovy.

Daily use:

  • Gradle has excellent support for multi-project construction, and engineering dependence is the first citizen of gradle.
  • gradle supports partial builds. Support multiple dependency management: jars or dirs from maven remote warehouse, nexus private service, ivy warehouse and local file system
  • gradle is the first building integration tool, which has good compatibility and correlation with ant, maven and ivy.
  • Easy migration: Gradle works for any structure, and you can build both original and gradle projects in parallel on the same development platform. It is often required to write tests to ensure the similarity of the plug-ins being developed. This migration can reduce disruption and be as reliable as possible. This is also the best practice of refactoring.
  • gradle's overall design is oriented as a language, not as a rigid framework.
  • Free Open Source

So:
gradle is able to do maven's work and ant's work, writing scripts in groove language, and expressing ability is stronger.

Statement Explanation

Outermost build.gradle
1. Builscript is the required dependency for the execution of gradle script, which is maven library and plug-in respectively.

buildscript {
    repositories {
        maven { 
            url = 'http://maven.repos.xxx.com/nexus/content/groups/public/'//load private Maven repository 
        }
    }
    dependencies {
        classpath "org.sonarsource.scanner.gradle:sonarqube-gradle-plugin:1.2" // Loading plug-ins, using the function method inside
    }
}


apply plugin: "org.sonarqube" //Access sonarqube, code quality management. It integrates traditional static code checking, and makes data statistics on unit test coverage and code repetition rate.
sonarqube { // Configuration of gradle.properties at the same level
    properties {
        property "sonar.host.url", System.getenv('SONAR_HOST_URL')
        property "sonar.login", System.getenv('SONAR_LOGIN')
        property "sonar.password", System.getenv('SONAR_PASSWORD')
    }
}

2. All projects are the dependencies needed by the project itself. For example, if I want to rely on my own maven library's xx library now, I should put maven {url}‘ http://maven.repos.xxx.com/ne... '} Write it here, not in buildscript, or you won't find it.

//This allprojects {} is a Script Block  
allprojects {
    apply plugin: 'eclipse'
    apply plugin: 'idea'
    repositories { //A Script Block 
        mavenLocal()  // Priority to download from local warehouse
        maven {
            url  'http://maven.repos.xxx.com/nexus/content/groups/public/'
        }
    }
    eclipse {
        classpath {
            downloadSources=true // Configuration of plug-ins
        }
    }
    group = 'com.xxx'    // Company name
    version = '1.3.2'    // version number
}

3. Exclude transfer dependency. There are many reasons for excluding delivery dependencies, such as no remote warehouse, no need at runtime, or version conflicts. There are two ways to exclude transitive dependencies:

  • Exclude directly in configuration
  • Exclude in a specific dependency

    subprojects {

       configurations { 
           compile.exclude group:"org.codehaus.jackson",module:"jackson-mapper-asl" // Exclusion of dependency
       }
       //config java
       sourceCompatibility = compatibilityVersion
       targetCompatibility = compatibilityVersion
       [compileJava, javadoc, compileTestJava]*.options*.encoding = encoding
       sourceSets {
           main {
               resources {
                   srcDirs('src/main/java')      //Add the java directory to the resources directory. There are some mapper xml in the java subdirectory
               }
           }
       }
       checkstyle { // Format Detection
           configFile = file(rootDir.getAbsolutePath() + '/config/checkstyle/checkstyle.xml')
           System.setProperty("parent_dir", rootDir.getAbsolutePath())
       }
       findbugs { // Detection of grammar-related rules
           excludeFilter = file(rootDir.getAbsolutePath() + '/config/findbugs/findbugs-exclude.xml')
       }
    
       /**
        * gradle Project summary jar The coordinates of the package are all in dependencies Settings in properties
        * each jar The coordinates of a package consist of three basic elements
        * group, name, version
        * testCompile Identify the jar Packages work during testing. This property is jar Scope of packages
        * We are gradle When you add coordinates to it, you need to take them with you. jar Scope of packages
     */
    dependencies {
        compile("com.xxx:dubbo-service-common:2.0.2-SNAPSHOT"){
            exclude module:'kryo' // Exclusion of dependency
            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', // Dependency, 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) { // Enabling HTML reporting for FindBugs
        reports {
            xml.enabled = false
            html.enabled = true
        }
    }
 
    // Generating jar package and source code mapping
    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'
}

2,common-service-api/build.gradle

// List properties of jar packages
jar {
    manifest {
        attributes 'Implementation-Title': 'common-service-api', 'Implementation-Version': version
    }
}
// Dependent plug-ins
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"
    )
}
// Exclusion of dependency
configurations {
    compile.exclude module:'mockito-all'
}
// file name
archivesBaseName = 'common-service-api'
// Publish jar commands to execute gradle upload Archives
// Publish jar packages locally or remotely, or in multiple directories
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 deployment
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'
    }
}

3,common-service-provider/build.gradle

  • Compoile dependencies placed under this configuration are used when compiling code, but as a grouping, it contains the dependencies needed for code and compilation.
  • Dependencies required by runtime code at run time, by default, also include dependencies in compile.
  • By default, the compiled code and the compiled dependencies required by the code also belong to this group.
  • The dependencies that testRuntime needs to run tests. By default, groups containing compile, runtime, and testCompile are built and dependent.

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

Keywords: Java Gradle Maven xml Spring

Added by unreal128 on Sun, 04 Aug 2019 13:22:37 +0300