Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ allprojects {
repositories {
mavenCentral()
jcenter()
gradlePluginPortal()
}
}

Expand Down
2 changes: 1 addition & 1 deletion buildSrc/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ repositories {

kotlinDslPluginOptions {
experimentalWarning.set(false)
}
}
1 change: 1 addition & 0 deletions buildSrc/src/main/kotlin/Dependencies.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ object Libs {
const val DependencyManagement = "io.spring.gradle:dependency-management-plugin:${Versions.DependencyManagement}"
const val KotlinSpringGradlePlugin = "org.jetbrains.kotlin:kotlin-allopen:${Versions.Kotlin}"
const val KtLint = "com.pinterest:ktlint:${Versions.KtLint}"
const val GoogleJibPlugin = "gradle.plugin.com.google.cloud.tools:jib-gradle-plugin:${Versions.Jib}"
}

object MavenBom {
Expand Down
1 change: 1 addition & 0 deletions buildSrc/src/main/kotlin/Versions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ object Release {
}

object Versions {
const val Jib: String = "3.2.0"
const val Java = "1.8"
const val Kotlin = "1.4.32"
const val SpringBoot = "2.4.5"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ dependencies {
implementation(Libs.SpringBootGradlePlugin)
implementation(Libs.DependencyManagement)
implementation(Libs.KotlinSpringGradlePlugin)
implementation(Libs.GoogleJibPlugin)
implementation(Libs.KtLint) {
// ktlint 引用了1.4.31版本,和项目引入的1.4.32有冲突
exclude(group = "org.jetbrains.kotlin", module = "kotlin-stdlib-jdk8")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.tencent.devops

import com.tencent.devops.conventions.GoogleJibConvention
import com.tencent.devops.conventions.JUnitConvention
import com.tencent.devops.conventions.JavaConvention
import com.tencent.devops.conventions.KotlinConvention
Expand All @@ -26,6 +27,8 @@ class DevOpsBootPlugin : Plugin<Project> {
}
// add dependencyManagement
RepositoryConvention().apply(project)
// add jib to build container images
GoogleJibConvention().apply(project)
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.tencent.devops.conventions

import com.google.cloud.tools.jib.gradle.BuildImageTask
import com.google.cloud.tools.jib.gradle.JibExtension
import com.google.cloud.tools.jib.gradle.JibPlugin
import com.tencent.devops.enums.AssemblyMode
import com.tencent.devops.utils.findPropertyOrEmpty
import com.tencent.devops.utils.resolveAssemblyMode
import org.gradle.api.Project
import org.slf4j.LoggerFactory

/**
* Google Jib 插件配置
*/
class GoogleJibConvention {

fun apply(project: Project) {
if (project.findPropertyOrEmpty("devops.jib") != "true") {
logger.debug("devops.jib is not true , google convention is disabled")
return
}

if (resolveAssemblyMode(project) != AssemblyMode.KUBERNETES) {
logger.warn("Google Jib Convention is failed , project must use KUBERNETES Mode")
return
}

with(project) {
pluginManager.apply(JibPlugin::class.java)
logger.debug("Apply GoogleJibConvention success!")
}

}

companion object {
private val logger = LoggerFactory.getLogger(GoogleJibConvention::class.java)
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.tencent.devops.conventions

import com.tencent.devops.utils.findPropertyOrEmpty
import com.tencent.devops.enums.AssemblyMode
import com.tencent.devops.utils.resolveAssemblyMode
import org.gradle.api.Project
import org.jetbrains.kotlin.gradle.utils.IMPLEMENTATION

Expand All @@ -16,16 +17,6 @@ class SpringCloudConvention {
}
}

/**
* 解析打包模式
*/
private fun resolveAssemblyMode(project: Project): AssemblyMode {
val property = project.findPropertyOrEmpty(ASSEMBLY_MODE).trim()
return AssemblyMode.ofValueOrDefault(property).apply {
println("Project[${project.name}] assembly mode: $this")
}
}

/**
* 添加微服务相关依赖项
*/
Expand All @@ -43,11 +34,6 @@ class SpringCloudConvention {
}

companion object {
/**
* 打包模式
*/
private const val ASSEMBLY_MODE = "devops.assemblyMode"

/**
* consul依赖
*/
Expand All @@ -61,16 +47,3 @@ class SpringCloudConvention {
private const val K8S_DISCOVERY = "org.springframework.cloud:spring-cloud-starter-kubernetes-client-config"
}
}

enum class AssemblyMode {
CONSUL,
K8S,
KUBERNETES;

companion object {
fun ofValueOrDefault(value: String): AssemblyMode {
val upperCase = value.toUpperCase()
return values().find { it.name == upperCase } ?: CONSUL
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.tencent.devops.enums

enum class AssemblyMode {
CONSUL,
K8S,
KUBERNETES;

companion object {
fun ofValueOrDefault(value: String): AssemblyMode {
val upperCase = value.toUpperCase()
return values().find { it.name == upperCase } ?: CONSUL
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.tencent.devops.utils

import com.tencent.devops.conventions.SpringCloudConvention
import com.tencent.devops.enums.AssemblyMode
import org.gradle.api.JavaVersion
import org.gradle.api.Project

Expand Down Expand Up @@ -68,3 +70,13 @@ fun Project.findPropertyOrEmpty(name: String): String {
fun Project.findPropertyOrDefault(name: String, default: String): String {
return findPropertyOrNull(name) ?: default
}

/**
* 判断构建模式
*/
fun resolveAssemblyMode(project: Project): AssemblyMode {
val property = project.findPropertyOrEmpty("devops.assemblyMode").trim()
return AssemblyMode.ofValueOrDefault(property).apply {
println("Project[${project.name}] assembly mode: $this")
}
}