gradle multiproject && docker build
备注:
环境准备 : docker , gradle(使用wrapper,或者全局安装),测试环境使用mac
1. gradle 安装
brew install gradle
2. docker
// 安装
yum install -y yum-utils
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
yum install docker-ce
// 加速
curl -sSL https://get.daocloud.io/daotools/set_mirror.sh | sh -s http://099430a4.m.daocloud.io
3. gradle 项目结构
gradle wrapper
├── README.md
├── build.gradle
├── gradle
│ └── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── lib
│ └── commons-codec.jar
├── moviesservice
│ ├── build.gradle
│ └── src
│ └── main
│ ├── java
│ │ └── com
│ │ └── dalong
│ │ └── moviesservice
│ │ └── Movies.java
│ └── resources
├── settings.gradle
├── src
│ ├── main
│ │ ├── docker
│ │ │ └── Dockerfile
│ │ ├── java
│ │ │ └── com
│ │ │ └── dalong
│ │ │ └── Application.java
│ │ └── resources
│ └── test
│ ├── java
│ └── resources
└── userservice
├── build.gradle
└── src
└── main
├── java
│ └── com
│ └── dalong
│ └── userservice
│ └── User.java
└── resources
4. 代码说明
多项目主要是 settings.gradle,具体代码如下:
1. settings.gradle
rootProject.name = 'parent'
include 'moviesservice'
include 'userservice'
2. build.gradle
group 'com.gradleapp'
version '1.0-SNAPSHOT'
buildscript {
repositories {
maven{ url 'http://maven.aliyun.com/nexus/content/groups/public/'}
}
dependencies {
classpath('se.transmode.gradle:gradle-docker:1.2')
}
}
apply plugin: 'java'
apply plugin: 'application'
apply plugin: "docker"
sourceCompatibility = 1.8
mainClassName = 'com.dalong.Application'
repositories {
mavenCentral()
maven{ url 'http://maven.aliyun.com/nexus/content/groups/public/'}
}
// 依赖引用
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
compile project(":userservice")
compile project(":moviesservice")
compile files("lib/commons-codec.jar")
}
// docker build task
task buildDocker(type:Docker,dependsOn:build){
push = false
applicationName ="gradlemultiproject"
project.group="dockerimagehost/mydemo"
dockerfile = file("${projectDir}/src/main/docker/Dockerfile")
doFirst {
copy {
from distZip
into stageDir
}
}
}
3. 子项目gradle 配置,比较简单
group 'com.gradleapp'
version '1.0-SNAPSHOT'
apply plugin: 'java'
sourceCompatibility = 1.8
repositories {
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
}
4. dockerfile
FROM openjdk:8-jre-alpine
WORKDIR /appdemo/
COPY parent-1.0-SNAPSHOT.zip /appdemo
RUN unzip /appdemo/parent-1.0-SNAPSHOT.zip && rm -rf parent-1.0-SNAPSHOT.zip
ENTRYPOINT [ "./parent-1.0-SNAPSHOT/bin/parent"]
5. 构建
gradle buildDocker or ./gradlew buildDocker
> Task :buildDocker
Sending build context to Docker daemon 224.8kB
Step 1/5 : FROM openjdk:8-jre-alpine
---> b1bd879ca9b3
Step 2/5 : WORKDIR /appdemo/
---> Using cache
---> 53e534f7f64f
Step 3/5 : COPY parent-1.0-SNAPSHOT.zip /appdemo
---> f07ca2806459
Step 4/5 : RUN unzip /appdemo/parent-1.0-SNAPSHOT.zip && rm -rf parent-1.0-SNAPSHOT.zip
---> Running in f42179c4ac39
Archive: /appdemo/parent-1.0-SNAPSHOT.zip
creating: parent-1.0-SNAPSHOT/
creating: parent-1.0-SNAPSHOT/lib/
inflating: parent-1.0-SNAPSHOT/lib/parent-1.0-SNAPSHOT.jar
inflating: parent-1.0-SNAPSHOT/lib/commons-codec.jar
inflating: parent-1.0-SNAPSHOT/lib/userservice-1.0-SNAPSHOT.jar
inflating: parent-1.0-SNAPSHOT/lib/moviesservice-1.0-SNAPSHOT.jar
creating: parent-1.0-SNAPSHOT/bin/
inflating: parent-1.0-SNAPSHOT/bin/parent.bat
inflating: parent-1.0-SNAPSHOT/bin/parent
Removing intermediate container f42179c4ac39
---> e322da1a4e8a
Step 5/5 : ENTRYPOINT [ "./parent-1.0-SNAPSHOT/bin/parent"]
---> Running in 8c279ec90d6f
Removing intermediate container 8c279ec90d6f
---> 39e705358ef1
Successfully built 39e705358ef1
Successfully tagged dockerimagehost/mydemo/gradlemultiproject:1.0-SNAPSHOT
6. 参考资料
https://docs.gradle.org/4.5/userguide/intro_multi_project_builds.html
https://github.com/Transmode/gradle-docker
https://github.com/rongfengliang/gradlemultiproject.git