docker学习(三) gradle 使用docker插件自动构建springboot工程

gradle 使用docker插件自动构建springboot工程

使用gradle 构建springboot工程直接打成docker镜像,例子在github例子源码

1 IDEA gradle构建一个Springboot多模块项目,过程略,可参见源码。

2 编写build.gradle文件

buildscript {
    repositories {
        maven { url "https://repo.spring.io/libs-milestone/" }
        jcenter()
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
        classpath "se.transmode.gradle:gradle-docker:${transmodeGradleDockerVersion}"
    }
}
allprojects {
    apply plugin: 'java'
    apply plugin: 'org.springframework.boot'
    apply plugin: 'idea'
    apply plugin: 'io.spring.dependency-management'
    group = 'com.iscas.docker'
    version = '1.0-SNAPSHOT'
    sourceCompatibility = 1.8

    configurations {
        compile.exclude group: 'org.springframework.boot', module: 'spring-boot-starter-tomcat'
        compile.exclude group: 'org.apache.tomcat'
        compile.exclude group: 'org.apache.tomcat.embed'
    }

    repositories {
//        maven { url "http://maven.aliyun.com/nexus/content/groups/public/" }
//        maven { url "https://oss.sonatype.org/content/groups/public/" }
        maven { url "https://repo.spring.io/libs-milestone/" }
        jcenter()
        mavenCentral()
    }

    dependencies {
        testCompile "org.springframework.boot:spring-boot-starter-test"

        compile('org.springframework.boot:spring-boot-starter-web')
        compile('org.springframework.boot:spring-boot-starter-undertow')

    }

    dependencyManagement {
        imports {

            mavenBom "io.spring.platform:platform-bom:${platformVersion}"
        }
    }

    if (project.subprojects) {
//        bootRepackage {
//            enabled = false
//        }
    } else {
        apply plugin: 'docker'
        docker {
            maintainer = 'zqw0209 <zqw0209@sina.com>'
            baseImage = 'frolvlad/alpine-oraclejdk8:slim'
//            useApi = true
//            hostUrl = 'https://hub.docker.com/'
//            apiUsername = 'zqw0209'
//            apiPassword = '12345678a'
//            apiEmail = 'zqw0209@sina.com'
        }
        task dockerBuild(type: Docker, dependsOn: build) {
            applicationName = project.name
            tag = "zqw0209/${applicationName}"
            addFile {
                from configurations.archives.artifacts.files
                into '/data/workspace/' + project.name
            }
            workingDir '/data/workspace/' + project.name
            setEnvironment('HOST_MACHINE_IP', hostMachineIp)
            defaultCommand(['java', '-jar', '-Dspring.profiles.active=docker', "${project.name}-${project.version}.jar"])
        }
    }

}

分部分介绍各个配置

1)buildscript repositories 指定仓库位置,dependencies中指定springboot 版本,gradle-docker插件版本

buildscript {
    repositories {
        maven { url "https://repo.spring.io/libs-milestone/" }
        jcenter()
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
        classpath "se.transmode.gradle:gradle-docker:${transmodeGradleDockerVersion}"
    }
}

2)allprojects 中指定各个插件

 apply plugin: 'java'
    apply plugin: 'org.springframework.boot'
    apply plugin: 'idea'
    apply plugin: 'io.spring.dependency-management'
    group = 'com.iscas.docker'
    version = '1.0-SNAPSHOT'
    sourceCompatibility = 1.8

3) 指定springboot-web

 dependencies {
        testCompile "org.springframework.boot:spring-boot-starter-test"

        compile('org.springframework.boot:spring-boot-starter-web')
        compile('org.springframework.boot:spring-boot-starter-undertow')

    }
  1. 指定版本平衡的工具
 dependencyManagement {
        imports {

            mavenBom "io.spring.platform:platform-bom:${platformVersion}"
        }
    }

5) 指定在主模块进行生成docker镜像。dockerbaseImage指定依赖的镜像,task dockerBuild(type: Docker, dependsOn: build)tag指定打包的iamge镜像名称等,defaultCommand指定启动命令等。

if (project.subprojects) {
//        bootRepackage {
//            enabled = false
//        }
    } else {
        apply plugin: 'docker'
        docker {
            maintainer = 'zqw0209 <zqw0209@sina.com>'
            baseImage = 'frolvlad/alpine-oraclejdk8:slim'
//            useApi = true
//            hostUrl = 'https://hub.docker.com/'
//            apiUsername = 'zqw0209'
//            apiPassword = '12345678a'
//            apiEmail = 'zqw0209@sina.com'
        }
        task dockerBuild(type: Docker, dependsOn: build) {
            applicationName = project.name
            tag = "zqw0209/${applicationName}"
            addFile {
                from configurations.archives.artifacts.files
                into '/data/workspace/' + project.name
            }
            workingDir '/data/workspace/' + project.name
            setEnvironment('HOST_MACHINE_IP', hostMachineIp)
            defaultCommand(['java', '-jar', '-Dspring.profiles.active=docker', "${project.name}-${project.version}.jar"])
        }
    }

参考:https://blog.csdn.net/baidu_22254181/article/details/80789068

上一篇 docker术语
下一篇 配置阿里云镜像加速器

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值