gradle docker插件bmuschko将build.gradle拆分为两个文件会出错

时间:2018-05-17 23:15:38

标签: docker spring-boot gradle gradle-plugin

我是使用gradle脚本的新手。我有一个build.gradle文件,我想分成两个文件。拆分较大的build.gradle文件后,我得到以下两个文件。

的build.gradle

buildscript {
    ext {
        springBootVersion = '1.5.12.RELEASE'
        gradleDockerVersion = '3.2.7'
    }
    repositories {
        jcenter()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
        classpath("com.bmuschko:gradle-docker-plugin:${gradleDockerVersion}")
    }
}

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

jar {
    baseName = 'gs-spring-boot'
    version =  '0.1.0'
}

repositories {
    mavenCentral()
}

sourceCompatibility = 1.8
targetCompatibility = 1.8
compileJava.options.encoding = 'UTF-8'

dependencies {
    compile("org.springframework.boot:spring-boot-starter-web")
    testCompile("junit:junit")
}
project.ext.imageName = 'myImage'
project.ext.tagName ='myTag'
project.ext.jarName = (jar.baseName + '-' + jar.version).toLowerCase()

apply from: 'dockerapp.gradle'

dockerapp.gradle

def gradleDockerVersion = '3.7.2'
buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath("com.bmuschko:gradle-docker-plugin:${gradleDockerVersion}")
    }
}

apply plugin: 'com.bmuschko.docker-remote-api'

import com.bmuschko.gradle.docker.tasks.image.DockerBuildImage
import com.bmuschko.gradle.docker.tasks.image.DockerRemoveImage
import com.bmuschko.gradle.docker.tasks.image.Dockerfile

def imageName = project.ext.imageName
def tagName = project.ext.tagName
def jarName = project.ext.jarName

task createAppDockerfile(type: Dockerfile) {
    // Don't create dockerfile if file already exists
    onlyIf { !project.file('Dockerfile').exists() }
    group 'Docker'
    description 'Generate docker file for the application'
    dependsOn bootRepackage
    destFile = project.file('Dockerfile')
    String dockerProjFolder = project.projectDir.name
    from 'openjdk:8-jre-slim'
    runCommand("mkdir -p /app/springboot/${dockerProjFolder} && mkdir -p /app/springboot/${dockerProjFolder}/conf")
    addFile("./build/libs/${jarName}.jar", "/app/springboot/${dockerProjFolder}/")
    environmentVariable('CATALINA_BASE', "/app/springboot/${dockerProjFolder}")
    environmentVariable('CATALINA_HOME', "/app/springboot/${dockerProjFolder}")
    workingDir("/app/springboot/${dockerProjFolder}")
    if (System.properties.containsKey('debug')) {
        entryPoint('java', '-Xdebug', '-Xrunjdwp:server=y,transport=dt_socket,address=5005,suspend=n', '-jar', "/app/springboot/${dockerProjFolder}/${jarName}.jar")
    } else {
        entryPoint('java', '-jar', "/app/springboot/${dockerProjFolder}/${jarName}.jar")
    }
}

task removeAppImage(type: DockerRemoveImage) {
    group 'Docker'
    description 'Remove the docker image using force'
    force = true
    targetImageId { imageName }
    onError { exception ->
        if (exception.message.contains('No such image')) {
            println 'Docker image not found for the current project.'
        }
    }
}

task createAppImage(type: DockerBuildImage) {
    group 'Docker'
    description 'Executes bootRepackage, generates a docker file and builds image from it'
    dependsOn(createAppDockerfile, removeAppImage)
    dockerFile = createAppDockerfile.destFile
    inputDir = dockerFile.parentFile
    if (tagName)
        tag = "${tagName}"
    else if (imageName)
        tag = "${imageName}"
    else
        tag = "${jarName}"
}

如果我尝试运行命令./gradlew createAppImage,我会收到如下错误:

Gradle error

dockerapp.gradle文件中的其他两个任务似乎没有问题。如果我将所有代码放在build.gradle文件中,它可以正常工作而不会出现任何错误。拆分文件和执行createAppImage而不会遇到错误的最佳方法是什么?

2 个答案:

答案 0 :(得分:1)

我能够在CDancy(插件的维护者)的帮助下解决这个问题,如下所示:

<强>的build.gradle

buildscript {
    ext {
        springBootVersion = '1.5.12.RELEASE'
        gradleDockerVersion = '3.2.7'
    }
    repositories {
        jcenter()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
        classpath("com.bmuschko:gradle-docker-plugin:${gradleDockerVersion}")
    }
}    

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

jar {
    baseName = 'gs-spring-boot'
    version =  '0.1.0'
}    

repositories {
    mavenCentral()
}    

sourceCompatibility = 1.8
targetCompatibility = 1.8
compileJava.options.encoding = 'UTF-8'    

dependencies {
    compile("org.springframework.boot:spring-boot-starter-web")
    testCompile("junit:junit")
}
project.ext.imageName = 'myimage'
project.ext.tagName ='mytag'
project.ext.jarName = (jar.baseName + '-' + jar.version).toLowerCase()    

apply from: 'docker.gradle'

docker.gradle

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.bmuschko:gradle-docker-plugin:3.2.7'
    }
}
repositories {
    jcenter()
}
// use fully qualified class name
apply plugin: com.bmuschko.gradle.docker.DockerRemoteApiPlugin    

// import task classes
import com.bmuschko.gradle.docker.tasks.image.*    

def imageName = project.ext.imageName
def tagName = project.ext.tagName
def jarName = project.ext.jarName    

task createAppDockerfile(type: Dockerfile) {
    // Don't create dockerfile if file already exists
    onlyIf { !project.file('Dockerfile').exists() }
    group 'Docker'
    description 'Generate docker file for the application'
    dependsOn bootRepackage
    destFile = project.file('Dockerfile')
    String dockerProjFolder = project.projectDir.name
    from 'openjdk:8-jre-slim'
    runCommand("mkdir -p /app/springboot/${dockerProjFolder} && mkdir -p /app/springboot/${dockerProjFolder}/conf")
    addFile("./build/libs/${jarName}.jar", "/app/springboot/${dockerProjFolder}/")
    environmentVariable('CATALINA_BASE', "/app/springboot/${dockerProjFolder}")
    environmentVariable('CATALINA_HOME', "/app/springboot/${dockerProjFolder}")
    workingDir("/app/springboot/${dockerProjFolder}")
    if (System.properties.containsKey('debug')) {
        entryPoint('java', '-Xdebug', '-Xrunjdwp:server=y,transport=dt_socket,address=5005,suspend=n', '-jar', "/app/springboot/${dockerProjFolder}/${jarName}.jar")
    } else {
        entryPoint('java', '-jar', "/app/springboot/${dockerProjFolder}/${jarName}.jar")
    }
}    

task removeAppImage(type: DockerRemoveImage) {
    group 'Docker'
    description 'Remove the docker image using force'
    force = true
    targetImageId { imageName }
    onError { exception ->
        if (exception.message.contains('No such image')) {
            println 'Docker image not found for the current project.'
        } else {
            print exception
        }
    }
}    

task createAppImage(type: DockerBuildImage) {
    group 'Docker'
    description 'Executes bootRepackage, generates a docker file and builds image from it'
    dependsOn(createAppDockerfile, removeAppImage)
    dockerFile = createAppDockerfile.destFile
    inputDir = dockerFile.parentFile
    if (tagName)
        tag = "${tagName}"
    else if (imageName)
        tag = "${imageName}"
    else
        tag = "${jarName}"
}

更改基本上在我的docker.gradle文件中,我必须添加指向jcenter()的存储库部分并使用完全限定的插件类名。

答案 1 :(得分:0)

我不知道您为什么要在Gradle中使用Docker插件?在使用维护最强的Mushko's Gradle Docker Plugin玩了一段时间之后,我不明白为什么还要在不同的语法上同时在docker锅炉模板上构建 plugins模板

在Gradle中进行容器化对于程序员来说很舒服,并且 DevOps会说他们不能像这样使用Docker ,如果需要支持,应该走自己的路。我在下面找到了一个简短,纯Docker风格且功能齐全的解决方案

在项目结构中找到普通的Dockerfile和docker.sh Shell脚本:

enter image description here

在您的build.gradle文件中:

    buildscript {
        repositories {
            gradlePluginPortal()
        }
    }

    plugins {
        id 'java'
        id 'application'
    }

    // Entrypoint:

    jar {
        manifest {
            attributes 'Main-Class': 'com.example.Test'
        }
    }

    mainClassName = 'com.example.Test'

    // Run in DOCKER-container:

    task runInDockerContainer {
        doLast {
            exec {
                workingDir '.' // Relative to project's root folder.
                commandLine 'sh', './build/resources/main/docker.sh'
            }
    //        exec {
    //            workingDir "."
    //            commandLine 'sh', './build/resources/main/other.sh'
    //        }
        }
    }

在遵循常规Docker文档的情况下,在干净的shell脚本中执行任何您想要的操作:

    # Everything is run relative to project's root folder:

    # Put everything together into build/docker folder for DOCKER-build:
    if [ -d "build/docker" ]; then rm -rf build/docker; fi;
    mkdir build/docker;
    cp build/libs/test.jar build/docker/test.jar;
    cp build/resources/main/Dockerfile build/docker/Dockerfile;

    # Build image from files in build/docker:
    cd build/docker || exit;
    docker build . -t test;

    # Run container based on image:
    echo $PWD
    docker run test;