Skip to content

Commit

Permalink
加入 mybatis 配置支持
Browse files Browse the repository at this point in the history
  • Loading branch information
endink committed Dec 9, 2021
1 parent b76042d commit 8932fc4
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 10 deletions.
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ dependencies {
api("io.github.gradle-nexus:publish-plugin:${Constants.publishingPluginVersion}")
api("org.jetbrains.kotlin:kotlin-allopen:${Constants.kotlinVersion}")
api("org.jetbrains.kotlin:kotlin-gradle-plugin:${Constants.kotlinVersion}")
api("${Constants.mybatisPluginLib}:${Constants.mybatisPluginVersion}")

compileOnly("com.google.devtools.ksp:com.google.devtools.ksp.gradle.plugin:${Constants.kspPluginVersion}")
}
Expand Down
5 changes: 4 additions & 1 deletion buildSrc/src/main/kotlin/Constants.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ object Constants {
const val kotlinVersion = "1.6.0"
const val publishingPluginVersion = "1.1.0"
const val testPlugin = false
const val projectVersion = "1.0.5"
const val projectVersion = "1.0.6"
const val kspPluginVersion = "1.6.0-1.0.1"
const val mybatisPluginVersion = "2.4"
const val mybatisPluginLib = "gradle.plugin.com.thinkimi.gradle:mybatis-generator-plugin"
}

14 changes: 11 additions & 3 deletions src/main/kotlin/com/labijie/infra/gradle/BuildConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ internal object BuildConfig {
jvmVersion: String = "1.8",
includeSource: Boolean = true,
useMavenProxy: Boolean = true,
bomVersion: String? = null,
bomVersion: String? = null
) {
if (this.parent == null) {
this.buildscript.repositories.apply {
Expand Down Expand Up @@ -112,15 +112,23 @@ internal object BuildConfig {
}

this.dependencies.apply {
this.add("api", "org.jetbrains.kotlin:kotlin-stdlib-jdk8:${kotlinVersion}")
this.add("api", "org.jetbrains.kotlin:kotlin-reflect:${kotlinVersion}")

if (!bomVersion.isNullOrBlank()) {
this.add("implementation", platform("com.labijie.bom:lib-dependencies:${bomVersion}"))
this.add("testImplementation", "org.jetbrains.kotlin:kotlin-test-junit5")
this.add("testImplementation", "org.junit.jupiter:junit-jupiter-api")
this.add("testImplementation", "org.junit.jupiter:junit-jupiter-engine")
this.add("testImplementation", "org.mockito:mockito-all")
}else{
val junitVersion = "5.8.2"
val mockitoVersion = "1.10.19"
this.add("testImplementation", "org.jetbrains.kotlin:kotlin-test-junit5:${kotlinVersion}")
this.add("testImplementation", "org.junit.jupiter:junit-jupiter-api:${junitVersion}")
this.add("testImplementation", "org.junit.jupiter:junit-jupiter-engine:${junitVersion}")
this.add("testImplementation", "org.mockito:mockito-all:${mockitoVersion}")
}
this.add("api", "org.jetbrains.kotlin:kotlin-stdlib-jdk8:${kotlinVersion}")
this.add("api", "org.jetbrains.kotlin:kotlin-reflect:${kotlinVersion}")
/**
testImplementation "org.jetbrains.kotlin:kotlin-test-junit5:$kotlin_version"
testImplementation "org.junit.jupiter:junit-jupiter-api"
Expand Down
63 changes: 57 additions & 6 deletions src/main/kotlin/com/labijie/infra/gradle/InfraExtension.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import com.labijie.infra.gradle.BuildConfig.useNexusPublishPlugin
import com.labijie.infra.gradle.BuildConfig.usePublishing
import com.labijie.infra.gradle.Utils.apply
import com.labijie.infra.gradle.Utils.configureFor
import com.labijie.infra.gradle.Utils.getProjectFile
import com.labijie.infra.gradle.internal.*
import com.thinkimi.gradle.MybatisGeneratorExtension
import org.gradle.api.Action
import org.gradle.api.Project
import org.gradle.api.internal.provider.MissingValueException
Expand All @@ -32,23 +34,23 @@ open class InfraExtension(private val project: Project) {
}
}

fun useInfraOrmGenerator(version: String = "1.0.0", outputDir: String? = null, packageName:String? = null) {
fun useInfraOrmGenerator(version: String = "1.0.0", outputDir: String? = null, packageName: String? = null) {
if (!project.pluginManager.hasPlugin("com.google.devtools.ksp")) {
project.apply(plugin = "com.google.devtools.ksp")
}
project.dependencies.apply {
this.add("ksp", "com.labijie.orm:exposed-generator:${version}")
}
if(!outputDir.isNullOrBlank() || !packageName.isNullOrBlank()){
project.configureFor(KspExtension::class.java){
if(!outputDir.isNullOrBlank()){
if (!outputDir.isNullOrBlank() || !packageName.isNullOrBlank()) {
project.configureFor(KspExtension::class.java) {
if (!outputDir.isNullOrBlank()) {
var dir: String = outputDir
if(!File(dir).isAbsolute){
if (!File(dir).isAbsolute) {
dir = Path(project.projectDir.absolutePath, outputDir).toString()
}
this.arg("exg_out_dir", dir)
}
if(!packageName.isNullOrBlank()){
if (!packageName.isNullOrBlank()) {
this.arg("exg_package", packageName)
}
}
Expand Down Expand Up @@ -79,4 +81,53 @@ open class InfraExtension(private val project: Project) {
this.project.usePublishing(pom, pom.idGeneration)
}

/**
* 使用 mybatis 代码生成器, 可以配合 itfsw 插件
* @see <a href="https://github.com/itfsw/mybatis-generator-plugin">https://github.com/itfsw/mybatis-generator-plugin</a>
*
* @param configFile XML 配置文件路径,相对路径表示相对项目根目录的路径,也可以设置据对路径
* @param propertiesFile XML 配置文件中 properties 节使用的文件,相对路径表示相对项目根目录的路径,也可以设置据对路径。
* 通常格式为: &lt;properties url="file:///${propertiesFile}"></properties>
*
* @param isMysqlDataSource 是否使用 Mysql 数据源生成代码
* @param enableItfswPlug 是否使用 itfsw 增强插件
* @param generatorCoreVersion mybatis 官方 generator-core 包版本
* @param itfswPluginVersion itfsw 插件版本
* @param mysqlConnectorVersion mysql-connector-java 包版本
* @param propertiesFileConfigKey XML 中 properties 文件使用的 key
*
*/
fun useMybatis(
configFile: String,
propertiesFile: String,
isMysqlDataSource: Boolean = true,
enableItfswPlug: Boolean = false,
generatorCoreVersion: String = "1.4.0",
itfswPluginVersion: String = "1.3.10",
mysqlConnectorVersion: String = "8.0.27",
propertiesFileConfigKey: String = "propertiesFile"
) {
val config = "mybatisGenerator"
project.apply(plugin = "com.thinkimi.gradle.MybatisGenerator")
project.dependencies.apply {
add(config, "org.mybatis.generator:mybatis-generator-core:${generatorCoreVersion}")
if(enableItfswPlug){
add(config, "com.itfsw:mybatis-generator-plugin:${itfswPluginVersion}")
}
if(isMysqlDataSource){
add(config, "mysql:mysql-connector-java:${mysqlConnectorVersion}")
}
}
project.configureFor(MybatisGeneratorExtension::class.java) {
this.configFile = project.getProjectFile(configFile)
this.overwrite = true
this.verbose = true
if(propertiesFileConfigKey.isNotBlank()) {
this.mybatisProperties = mapOf(
propertiesFileConfigKey to propertiesFile
)
}
}
}

}
11 changes: 11 additions & 0 deletions src/main/kotlin/com/labijie/infra/gradle/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.labijie.infra.gradle

import org.gradle.api.Project
import org.gradle.api.plugins.PluginAware
import java.io.File
import kotlin.reflect.KClass
import kotlin.reflect.typeOf

Expand Down Expand Up @@ -33,4 +34,14 @@ object Utils {
@Suppress("deprecation") convention.findByType(extensionType.java)
?: @Suppress("deprecation") convention.findPlugin(extensionType.java)
?: @Suppress("deprecation") convention.getByType(extensionType.java)


fun Project.getProjectFile(file: String): String {
val f = File(file)
return if(f.isAbsolute){
f.absolutePath
}else{
File(this.projectDir, file).absolutePath
}
}
}

0 comments on commit 8932fc4

Please sign in to comment.