build.gradle 10.2 KB
/*
 * Copyright 2016 LINE Corporation
 *
 * LINE Corporation licenses this file to you under the Apache License,
 * version 2.0 (the "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at:
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */

import com.github.spotbugs.snom.SpotBugsTask
import org.springframework.boot.gradle.plugin.SpringBootPlugin

// ./gradlew clean && ./gradlew uploadArchives -Prelease

buildscript {
    ext {
        kotlin_version = '1.3.72'
        spring_boot_version = '2.3.0.RELEASE'
    }

    repositories {
        mavenCentral()
        maven { url 'https://plugins.gradle.org/m2/' }
        maven { url 'https://repo.spring.io/plugins-release' }
    }
    dependencies {
        classpath 'com.github.ben-manes:gradle-versions-plugin:0.27.0'
        classpath 'com.puppycrawl.tools:checkstyle:8.32'
        classpath 'gradle.plugin.com.github.spotbugs.snom:spotbugs-gradle-plugin:4.2.0'
        classpath 'io.franzbecker:gradle-lombok:4.0.0'
        classpath 'io.spring.gradle:dependency-management-plugin:1.0.9.RELEASE'
        classpath "org.springframework.boot:spring-boot-gradle-plugin:$spring_boot_version"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

apply plugin: 'idea'
apply plugin: 'jacoco'
apply plugin: 'com.github.ben-manes.versions'

group = 'com.linecorp.bot'
version = '3.7.0'

//set build variables based on build type (release, continuous integration, development)
def isDevBuild
def isReleaseBuild
def sonatypeRepositoryUrl
if (hasProperty('release')) {
    isReleaseBuild = true
    sonatypeRepositoryUrl = 'https://oss.sonatype.org/service/local/staging/deploy/maven2/'
} else if (hasProperty('ci')) {
    version += '-SNAPSHOT'
    sonatypeRepositoryUrl = 'https://oss.sonatype.org/content/repositories/snapshots/'
} else {
    isDevBuild = true
    version += '-SNAPSHOT'
}

allprojects {
    repositories {
        mavenCentral();
    }
}

subprojects {
    apply plugin: 'com.github.spotbugs'
    apply plugin: 'java-library'
    apply plugin: 'checkstyle'
    apply plugin: 'io.franzbecker.gradle-lombok'
    apply plugin: 'io.spring.dependency-management'

    sourceCompatibility = 1.8
    targetCompatibility = 1.8

    group = rootProject.group
    version = rootProject.version

    ext['guava.version'] = '28.2-jre'
    ext['retrofit.version'] = '2.6.1'

    dependencyManagement {
        imports {
            mavenBom SpringBootPlugin.BOM_COORDINATES
        }

        dependencies {
            dependency 'com.google.guava:guava:' + ext['guava.version']
            dependency 'com.github.stefanbirkner:system-rules:1.19.0'
            dependencySet(group: 'com.squareup.retrofit2', version: ext['retrofit.version']) {
                entry 'converter-jackson'
                entry 'retrofit'
            }
            dependencySet(group: 'io.jsonwebtoken', version: '0.11.1') {
                entry 'jjwt-api'
                entry 'jjwt-impl'
                entry 'jjwt-jackson'
            }
        }
    }

    dependencies {
        annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor'
        // http://docs.spring.io/spring-boot/docs/current/reference/html/configuration-metadata.html#configuration-metadata-annotation-processor

        testImplementation 'com.google.guava:guava'
        testImplementation 'com.github.stefanbirkner:system-rules'
        testImplementation 'com.squareup.okhttp3:mockwebserver'
        testImplementation 'org.hibernate.validator:hibernate-validator'
        testImplementation 'org.springframework.boot:spring-boot-starter-test' // MockHttpServletRequest
        testImplementation 'org.springframework.boot:spring-boot-starter-logging'
    }

    compileJava.dependsOn(processResources)
    // http://docs.spring.io/spring-boot/docs/current/reference/html/configuration-metadata.html#configuration-metadata-annotation-processor

    def springManagedModules = [
        'org.springframework',
        'com.fasterxml.jackson',
        'javax.validation',
        'org.hibernate',
        'javax.servlet'
    ];
    dependencyUpdates.resolutionStrategy {
        componentSelection { rules ->
            rules.all { ComponentSelection selection ->
                boolean springManaged = springManagedModules.any { module ->
                    selection.candidate.group.startsWith(module)
                }
                if (springManaged) {
                    selection.reject('Spring Managed')
                }
                boolean rejected = ['alpha', 'beta', 'rc', 'cr', 'm'].any { qualifier ->
                    selection.candidate.version ==~ /(?i).*[.-]${qualifier}\d*[.\d-]*/
                }
                if (rejected) {
                    selection.reject('Release candidate')
                }
            }
        }
    }

    jar {
        manifest {
            attributes 'Implementation-Title': project.name,
                'Implementation-Version': archiveVersion
        }
    }

    lombok {
        version = '1.18.12'
    }

    if (!project.name.startsWith('sample-') && !project.name.startsWith('test-')) {
        task delombok(type: io.franzbecker.gradle.lombok.task.DelombokTask, dependsOn: compileJava) {
            ext.outputDir = file("$buildDir/delombok")
            outputs.dir(outputDir)
            sourceSets.main.java.srcDirs.each {
                inputs.dir(it)
                args(it, "-d", outputDir)
            }
        }

        javadoc {
            dependsOn delombok
            source = delombok.outputDir
            options.encoding = 'UTF-8'
            options.locale = 'en_US'
            options.addStringOption('Xdoclint:none', '-quiet')
            // To create javadoc for generated method&constructor, delombok & run javadoc on delombok.outputDir.
        }

        task javadocJar(type: Jar, dependsOn: 'javadoc') {
            archiveClassifier.set('javadoc')
            from javadoc.destinationDir
        }

        task sourcesJar(type: Jar, dependsOn: 'classes') {
            archiveClassifier.set('sources')
            from sourceSets.main.allSource
        }

        artifacts {
            archives javadocJar, sourcesJar
        }
    }

    checkstyle {
        configProperties = [projectDir: rootProject.projectDir]
        toolVersion = '8.23'
    }

    tasks.withType(SpotBugsTask) {
        reports {
            xml {
                enabled = false
            }
            html {
                enabled = false
            }
        }
        excludeFilter = file("$rootProject.projectDir/config/findbugs/excludeFilter.xml")
    }

    compileJava {
        options.compilerArgs << '-Xlint:all' << '-Xlint:deprecation' << '-Werror' << '-Xlint:-processing' << '-parameters'
        options.encoding = 'UTF-8'
    }
    compileTestJava {
        options.encoding = 'UTF-8'
    }

    test {
        useJUnitPlatform()
    }

    project.plugins.withType(SpringBootPlugin) {
        bootRun {
            systemProperties System.properties
        }
    }
}

task codeCoverageReport(type: JacocoReport) {
    onlyIf = { true }
    executionData fileTree(project.rootDir.absolutePath).include("**/build/jacoco/*.exec")

    [':line-bot-api-client', ':line-bot-model', ':line-bot-servlet', ':line-bot-spring-boot', ':line-bot-cli'].each {
        sourceSets(project(it).sourceSets.main)
    }

    reports {
        xml.enabled true
        xml.destination file('reports/jacoco/report.xml')
        html.enabled true
        csv.enabled false
    }
}

[':line-bot-api-client', ':line-bot-model', 'line-bot-parser', ':line-bot-servlet', ':line-bot-spring-boot', ':line-bot-cli'].each { projectName ->
    project(projectName) { project ->
        apply plugin: 'jacoco'
        apply plugin: 'signing'
        apply plugin: 'maven'

        signing {
            required { isReleaseBuild }
            sign configurations.archives
        }

        uploadArchives {
            repositories {
                if (isDevBuild) {
                    mavenLocal()
                } else {
                    mavenDeployer {
                        if (isReleaseBuild) {
                            beforeDeployment { MavenDeployment deployment -> signing.signPom(deployment) }
                        }

                        repository(url: sonatypeRepositoryUrl) {
                            authentication(userName: sonatypeUsername, password: sonatypePassword)
                        }

                        pom.project {
                            name project.name
                            packaging 'jar'
                            description project.description
                            url 'https://github.com/line/line-bot-sdk-java'

                            scm {
                                url 'scm:git@github.com:line/line-bot-sdk-java.git'
                                connection 'scm:git@github.com:line/line-bot-sdk-java.git'
                                developerConnection 'scm:git@github.com:line/line-bot-sdk-java.git'
                            }
                            licenses {
                                license {
                                    name 'Apache'
                                    url 'https://opensource.org/licenses/Apache-2.0'
                                }
                            }
                            developers {
                                developer {
                                    id 'tokuhirom'
                                    name 'Tokuhiro Matsuno'
                                    email 'tokuhirom@gmail.com'
                                }
                                developer {
                                    id 'kazuki-ma'
                                    name 'Kazuki MATSUDA'
                                    email 'matsuda.kazuki@gmail.com'
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}