Gradle + Nexus 管理Spring Boot的Jar

前言

Spring Boot 使你能輕松地創(chuàng)建獨立的、生產(chǎn)級的、基于 Spring 且能直接運行的應用程序。
Gradle 是一種構(gòu)建工具,和已有的ant、maven相比Gradle更簡潔高效,勝任復雜的構(gòu)建任務,社區(qū)活躍,技術(shù)成熟。

Spring Boot的gradle插件

Spring Boot Gradle插件在Gradle中提供Spring Boot支持,你可以用它來做打包(生成可執(zhí)行jar或war),運行Spring Boot應用程序,并提供的依賴關(guān)系管理spring-boot-dependencies
Spring Boot的Gradle插件需要Gradle 4.0或更高版本。

dependencies {
         classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }

引用第三方jar

Nexus作為Gradle的私服,請參考http://www.itdecent.cn/p/f50fd2531db1

打包

gradle 默認有bootJar任務
需要jar包時執(zhí)行bootJar命令即可。

版本發(fā)布

image.png
buildscript {
    ext {
        springBootVersion = '2.0.2.RELEASE'
        //發(fā)布到倉庫用戶名
        publishUserName = "admin"
        //發(fā)布到倉庫地址
        publishUserPassword = "admin123"
        //倉庫地址
        snapshotURL="http://ip:port/repository/snapshot/"
        releaseURL="http://ip:port/repository/release/"
        builtBy = "gradle 4.5"
        baseName = 'cs'
        version = '0.0.1-SNAPSHOT'
        snapshotVersion = '0.0.1-SNAPSHOT'
        releaseVersion = '0.0.1'
    }
    repositories {
        maven { url "http://ip:port/repository/jcenter/"}
        maven { url "http://ip:port/repository/maven-public/"}
        maven { url "http://ip:port/repository/spring-public/"}
        maven { url "http://ip:port/repository/maven-central/"}
    }
    dependencies {
         classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'maven-publish'
apply plugin: 'maven'

sourceCompatibility = 1.8

task step1_clean_build(type: Delete){
    def build = file('build')
    delete build
}

bootJar {
    baseName = baseName
    version = snapshotVersion
}

task sourceJar(type: Jar) {
   classifier "sources"
   from sourceSets.main.allJava
}

task brelease(type: Copy) {
   def sourceFile = baseName + '-' + snapshotVersion + '.jar'
   def targetFile = baseName + '.jar'
   println sourceFile
   from 'build/libs/'  
   include sourceFile
   into 'build/libs/'  
   rename (sourceFile,targetFile)  
}


dependencies {
    compile('org.springframework.boot:spring-boot-starter-data-jpa')
    compile('org.springframework.boot:spring-boot-starter-web')
    compile("org.springframework.boot:spring-boot-devtools")
    compile('org.hibernate:hibernate-search-orm:5.9.1.Final')
    compile('org.hibernate:hibernate-search-elasticsearch:5.9.1.Final')
    compile('org.apache.ant:ant:1.10.3')
    compile('io.springfox:springfox-swagger2:2.8.0')
    compile('io.springfox:springfox-swagger-ui:2.8.0')
    compile('org.apache.commons:commons-lang3:3.4')
    compile('commons-io:commons-io:2.4')   
    compile('commons-beanutils:commons-beanutils:1.9.3')
    runtime("org.mariadb.jdbc:mariadb-java-client")    
    testCompile('org.springframework.boot:spring-boot-starter-test')
    testCompile('org.springframework.restdocs:spring-restdocs-mockmvc')
    testCompile('junit:junit:4.12')
}

publishing {
    publications {
        maven(MavenPublication) {
            groupId 'chances_micro_service'
            artifactId baseName
            version snapshotVersion
            from components.java
            def sourcesFile = file('build/libs/'+baseName+'-sources.jar')
            artifacts = [sourcesFile, sourceJar]
        }
    }
    repositories {
        maven {
            if(version.endsWith('-SNAPSHOT')) {
                url = snapshotURL
            }else {
                url = releaseURL
            }
            credentials {
                username publishUserName
                password publishUserPassword
            }
        }
    }
}

artifacts {
    archives file('build/libs/'+ baseName + '.jar')
}

uploadArchives { 
    repositories { 
        mavenDeployer { 
            repository(url: releaseURL) {
               authentication(userName: publishUserName, password: publishUserPassword)
            }
            pom.version = releaseVersion
            pom.artifactId = baseName
            pom.groupId = 'chances_micro_service'
        }
    }  
} 

task devUpload(dependsOn: ['bootJar','publishMavenPublicationToMavenRepository']){}    

task releaseUpload(dependsOn:['brelease','uploadArchives']){}

上傳開發(fā)版本運行 devUpload 命令,上傳發(fā)布版本運行releaseUpload命令。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容