Gradle + Nexus 管理Spring Boot的Jar

前言

Spring Boot 使你能轻松地创建独立的、生产级的、基于 Spring 且能直接运行的应用程序。
Gradle 是一种构建工具,和已有的ant、maven相比Gradle更简洁高效,胜任复杂的构建任务,社区活跃,技术成熟。

Spring Boot的gradle插件

Spring Boot Gradle插件在Gradle中提供Spring Boot支持,你可以用它来做打包(生成可执行jar或war),运行Spring Boot应用程序,并提供的依赖关系管理spring-boot-dependencies
Spring Boot的Gradle插件需要Gradle 4.0或更高版本。

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

引用第三方jar

Nexus作为Gradle的私服,请参考https://www.jianshu.com/p/f50fd2531db1

打包

gradle 默认有bootJar任务
需要jar包时执行bootJar命令即可。

版本发布

image.png
buildscript {
    ext {
        springBootVersion = '2.0.2.RELEASE'
        //发布到仓库用户名
        publishUserName = "admin"
        //发布到仓库地址
        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']){}

上传开发版本运行 devUpload 命令,上传发布版本运行releaseUpload命令。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容