build.gradle.kts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. import java.util.Date
  2. import groovy.util.Node
  3. import groovy.util.NodeList
  4. import java.util.Calendar
  5. import java.text.SimpleDateFormat
  6. import nl.javadude.gradle.plugins.license.License
  7. buildscript {
  8. repositories {
  9. maven("https://plugins.gradle.org/m2/")
  10. }
  11. dependencies {
  12. classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.21")
  13. classpath("com.netflix.nebula:gradle-extra-configurations-plugin:3.0.3")
  14. classpath("gradle.plugin.com.hierynomus.gradle.plugins:license-gradle-plugin:0.15.0")
  15. }
  16. }
  17. plugins {
  18. `java-library`
  19. signing
  20. `maven-publish`
  21. }
  22. // versions
  23. val javaVersion = JavaVersion.VERSION_1_8
  24. val mybatisSpringVersion = "2.0.0"
  25. val mybatisVersion = "3.5.0"
  26. val springVersion = "5.1.4.RELEASE"
  27. val jsqlparserVersion = "1.4"
  28. val junitVersion = "5.4.0"
  29. val lombokVersion = "1.18.4"
  30. val cglibVersion = "3.2.10"
  31. // libs
  32. val lib = mapOf(
  33. "kotlin-reflect" to "org.jetbrains.kotlin:kotlin-reflect:1.3.21",
  34. "kotlin-stdlib-jdk8" to "org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.3.21",
  35. "jsqlparser" to "com.github.jsqlparser:jsqlparser:$jsqlparserVersion",
  36. "mybatis-spring" to "org.mybatis:mybatis-spring:$mybatisSpringVersion",
  37. "mybatis" to "org.mybatis:mybatis:$mybatisVersion",
  38. "spring-context-support" to "org.springframework:spring-context-support:$springVersion",
  39. "spring-jdbc" to "org.springframework:spring-jdbc:$springVersion",
  40. "spring-tx" to "org.springframework:spring-tx:$springVersion",
  41. "spring-web" to "org.springframework:spring-web:$springVersion",
  42. "spring-aop" to "org.springframework:spring-aop:$springVersion",
  43. "cglib" to "cglib:cglib:$cglibVersion",
  44. "lombok" to "org.projectlombok:lombok:$lombokVersion",
  45. "javax.servlet-api" to "javax.servlet:javax.servlet-api:4.0.1",
  46. "aspectjweaver" to "org.aspectj:aspectjweaver:1.9.2",
  47. "mockito" to "org.mockito:mockito-core:2.24.0",
  48. "mybatis-ehcache" to "org.mybatis.caches:mybatis-ehcache:1.1.0",
  49. "slf4j-api" to "org.slf4j:slf4j-api:1.7.25",
  50. "logback-classic" to "ch.qos.logback:logback-classic:1.2.3",
  51. // test
  52. "spring-test" to "org.springframework:spring-test:$springVersion",
  53. "assertj-core" to "org.assertj:assertj-core:3.12.2",
  54. "junit-jupiter-api" to "org.junit.jupiter:junit-jupiter-api:$junitVersion",
  55. "junit-jupiter-engine" to "org.junit.jupiter:junit-jupiter-engine:$junitVersion",
  56. "mockito-all" to "org.mockito:mockito-all:1.10.19",
  57. "fastjson" to "com.alibaba:fastjson:1.2.56",
  58. "jackson" to "com.fasterxml.jackson.core:jackson-databind:2.9.6",
  59. "tomcatjdbc" to "org.apache.tomcat:tomcat-jdbc:9.0.16",
  60. // datasource
  61. "p6spy" to "p6spy:p6spy:3.8.1",
  62. "hikaricp" to "com.zaxxer:HikariCP:3.3.0",
  63. "druid" to "com.alibaba:druid:1.1.13",
  64. "commons-dbcp2" to "org.apache.commons:commons-dbcp2:2.5.0",
  65. "sqlserver" to "com.microsoft.sqlserver:sqljdbc4:4.0",
  66. "postgresql" to "org.postgresql:postgresql:42.2.5",
  67. "oracle" to fileTree("libs/ojdbc-11.2.0.3-jdk16.jar"),
  68. "h2" to "com.h2database:h2:1.4.197",
  69. "mysql" to "mysql:mysql-connector-java:8.0.15",
  70. // code generator
  71. "velocity" to "org.apache.velocity:velocity-engine-core:2.0",
  72. "freemarker" to "org.freemarker:freemarker:2.3.28",
  73. "beetl" to "com.ibeetl:beetl:2.9.8",
  74. "lagarto" to "org.jodd:jodd-lagarto:5.0.7",
  75. "swagger-annotations" to "io.swagger:swagger-annotations:1.5.21"
  76. )
  77. // ext
  78. extra["lib"] = lib
  79. allprojects {
  80. group = "com.baomidou"
  81. version = "3.1.0"
  82. }
  83. description = "Mybatis 增强工具包 - 只做增强不做改变,简化CRUD操作"
  84. subprojects {
  85. // 插件
  86. apply(plugin = "org.gradle.java-library")
  87. apply(plugin = "org.gradle.maven-publish")
  88. apply(plugin = "org.gradle.signing")
  89. apply(plugin = "com.github.hierynomus.license")
  90. // Java 版本
  91. configure<JavaPluginConvention> {
  92. sourceCompatibility = javaVersion
  93. targetCompatibility = javaVersion
  94. }
  95. // 编译器配置
  96. tasks.withType<JavaCompile> {
  97. options.encoding = "UTF-8"
  98. options.isDeprecation = true
  99. options.compilerArgs.add("-parameters")
  100. }
  101. tasks.withType<Jar> {
  102. afterEvaluate {
  103. manifest {
  104. attributes["Implementation-Version"] = version
  105. attributes["Built-Gradle"] = gradle.gradleVersion
  106. attributes["Bundle-DocURL"] = "https://mybatis.plus/"
  107. attributes["Build-OS"] = System.getProperty("os.name")
  108. attributes["Built-By"] = System.getProperty("user.name")
  109. attributes["Build-Jdk"] = System.getProperty("java.version")
  110. attributes["Build-Timestamp"] = SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Date())
  111. }
  112. }
  113. }
  114. tasks.withType<License> {
  115. encoding = "UTF-8"
  116. header = rootProject.file("license.txt")
  117. include("**/*.java","**/*.kt")
  118. exclude ("**/*Test.java")
  119. mapping("java","SLASHSTAR_STYLE")
  120. mapping("kt","SLASHSTAR_STYLE")
  121. extra["year"] = Calendar.getInstance().get(Calendar.YEAR)
  122. extra["name"] = "hubin"
  123. extra["email"] = "jobob@qq.com"
  124. }
  125. repositories {
  126. mavenLocal()
  127. maven("https://maven.aliyun.com/repository/public")
  128. maven("https://oss.sonatype.org/content/repositories/snapshots/")
  129. jcenter()
  130. }
  131. dependencies {
  132. annotationProcessor("${lib["lombok"]}")
  133. compileOnly("${lib["lombok"]}")
  134. testAnnotationProcessor("${lib["lombok"]}")
  135. testCompileOnly("${lib["lombok"]}")
  136. testImplementation("${lib["assertj-core"]}")
  137. testImplementation("${lib["junit-jupiter-api"]}")
  138. testRuntimeOnly("${lib["junit-jupiter-engine"]}")
  139. testCompileOnly("${lib["mockito-all"]}")
  140. testImplementation("org.mockito:mockito-junit-jupiter:2.24.0")
  141. testImplementation("${lib["lagarto"]}")
  142. }
  143. val sourcesJar by tasks.registering(Jar::class) {
  144. dependsOn(JavaPlugin.CLASSES_TASK_NAME)
  145. archiveClassifier.set("sources")
  146. from(sourceSets["main"].allJava)
  147. }
  148. tasks.withType<Javadoc> {
  149. options.encoding = "UTF-8"
  150. isFailOnError = false
  151. (options as? StandardJavadocDocletOptions)?.also {
  152. it.charSet = "UTF-8"
  153. it.isAuthor = true
  154. it.isVersion = true
  155. it.links = listOf("https://docs.oracle.com/javase/8/docs/api")
  156. if (JavaVersion.current().isJava9Compatible) {
  157. it.addBooleanOption("html5", true)
  158. }
  159. }
  160. }
  161. tasks.withType<Test> {
  162. dependsOn("cleanTest", "generatePomFileForMavenJavaPublication")
  163. useJUnitPlatform()
  164. exclude("**/generator/**")
  165. exclude("**/postgres/**")
  166. exclude("**/mysql/**")
  167. }
  168. val javadocJar by tasks.registering(Jar::class) {
  169. dependsOn(JavaPlugin.JAVADOC_TASK_NAME)
  170. archiveClassifier.set("javadoc")
  171. from(tasks["javadoc"])
  172. }
  173. tasks.whenTaskAdded {
  174. if (this.name.contains("signMavenJavaPublication")) {
  175. this.enabled = File(project.property("signing.secretKeyRingFile") as String).isFile
  176. }
  177. }
  178. publishing {
  179. repositories {
  180. maven {
  181. val userName = System.getProperty("un")
  182. val passWord = System.getProperty("ps")
  183. val releasesRepoUrl = "https://oss.sonatype.org/service/local/staging/deploy/maven2/"
  184. val snapshotsRepoUrl = "https://oss.sonatype.org/content/repositories/snapshots/"
  185. setUrl(if (version.toString().endsWith("SNAPSHOT")) snapshotsRepoUrl else releasesRepoUrl)
  186. credentials {
  187. username = userName
  188. password = passWord
  189. }
  190. }
  191. }
  192. publications {
  193. register("mavenJava", MavenPublication::class) {
  194. pom {
  195. name.set("mybatis-plus")
  196. description.set("An enhanced toolkit of Mybatis to simplify development.")
  197. inceptionYear.set("2016")
  198. url.set("https://github.com/baomidou/mybatis-plus")
  199. artifactId = project.name
  200. groupId = "${project.group}"
  201. version = "${project.version}"
  202. packaging = "jar"
  203. organization {
  204. name.set("baomidou")
  205. }
  206. scm {
  207. connection.set("scm:git@github.com:Codearte/gradle-nexus-staging-plugin.git")
  208. developerConnection.set("scm:git@github.com:Codearte/gradle-nexus-staging-plugin.git")
  209. url.set("https://github.com/baomidou/mybatis-plus")
  210. }
  211. licenses {
  212. license {
  213. name.set("The Apache License, Version 2.0")
  214. url.set("https://www.apache.org/licenses/LICENSE-2.0.txt")
  215. }
  216. }
  217. developers {
  218. developer {
  219. id.set("baomidou")
  220. name.set("hubin")
  221. email.set("jobob@qq.com")
  222. }
  223. }
  224. withXml {
  225. fun Any?.asNode() = this as Node
  226. fun Any?.asNodeList() = this as NodeList
  227. val root = asNode()
  228. root["dependencies"].asNodeList().getAt("*").forEach {
  229. val dependency = it.asNode()
  230. if (dependency["scope"].asNodeList().text() == "runtime") {
  231. if (project.configurations.findByName("implementation")?.allDependencies?.none { dep ->
  232. dep.name == dependency["artifactId"].asNodeList().text()
  233. } == false) {
  234. dependency["scope"].asNodeList().forEach { it.asNode().setValue("compile") }
  235. dependency.appendNode("optional", true)
  236. }
  237. }
  238. }
  239. }
  240. }
  241. from(components["java"])
  242. artifact(sourcesJar.get())
  243. artifact(javadocJar.get())
  244. }
  245. }
  246. }
  247. signing {
  248. sign(publishing.publications.getByName("mavenJava"))
  249. }
  250. }