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 <[email protected]>'
            baseImage = 'frolvlad/alpine-oraclejdk8:slim'
//            useApi = true
//            hostUrl = 'https://hub.docker.com/'
//            apiUsername = 'zqw0209'
//            apiPassword = '12345678a'
//            apiEmail = '[email protected]'
        }
        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')

    }

4) 指定版本平衡的工具

 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 <[email protected]>'
            baseImage = 'frolvlad/alpine-oraclejdk8:slim'
//            useApi = true
//            hostUrl = 'https://hub.docker.com/'
//            apiUsername = 'zqw0209'
//            apiPassword = '12345678a'
//            apiEmail = '[email protected]'
        }
        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"])
        }
    }

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

猜你喜欢

转载自blog.csdn.net/u011943534/article/details/81264204