build.gradle示例代码
buildscript {
ext {
springBootVersion = '1.5.4.RELEASE'
}
repositories {
mavenLocal()
maven { url = "http://maven.aliyun.com/nexus/content/groups/public" }
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
classpath 'se.transmode.gradle:gradle-docker:1.2'
}
}
apply plugin: 'java'
apply plugin: 'idea'
//apply plugin: 'war'
apply plugin: 'org.springframework.boot'
//docker所需插件
apply plugin: 'docker'
version = '1.0.0'
sourceCompatibility = 1.8
repositories {
mavenLocal()
maven { url = "http://maven.aliyun.com/nexus/content/groups/public" }
mavenCentral()
}
dependencies {
compile('org.mybatis.spring.boot:mybatis-spring-boot-starter:1.3.0')
compile('org.springframework.boot:spring-boot-starter-web')
compile('org.springframework.boot:spring-boot-starter-aop')
compile('org.springframework.boot:spring-boot-starter-data-jpa')
compile('com.github.pagehelper:pagehelper-spring-boot-starter:1.1.1')
compile('com.alibaba:druid:1.0.31')
runtime('mysql:mysql-connector-java')
testCompile('org.springframework.boot:spring-boot-starter-test')
}
//以下为jar配置
jar {
baseName = "coupon"
manifest {
attributes 'Main-Class': 'net.mrpotato.CouponApplication'
}
}
build.doLast {
copy{
from('build/libs')
into('build/libs')
include('coupon-'+version+'.jar')
rename{'coupon.jar'}
}
}
//以下为docker配置
group = 'coupon'
//mainClassName = 'net.mrpotato.CouponApplication'
task buildDocker(type: Docker, dependsOn: build, group: "coupon") {
push = false
applicationName = jar.baseName
dockerfile = file('src/main/docker/Dockerfile')
doFirst {
copy {
from jar
into stageDir
}
}
}
build.gradle简要说明
- 新增buildscript下的dependencies新增依赖
classpath 'se.transmode.gradle:gradle-docker:1.2'
- 新增插件"docker"
- 设置group,用于docker镜像区分
- 设置docker构建配置,主要是Dockerfile文件位置
Dockerfile内容
FROM java:8u111-jre
VOLUME /tmp
ADD coupon-1.0.0.jar coupon.jar
RUN sh -c 'touch /coupon.jar'
ENV JAVA_OPTS=""
ENTRYPOINT [ "sh", "-c", "java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar /coupon.jar" ]
Dockerfile简要说明
- 由于工程使用java8,故使用镜像java:8u111-jre(根据开发环境可选择其他版本)
- VOLUME 容器挂载卷
- ADD 拷贝jar到容器中
- RUN 运行
网友评论