Skip to content

Commit

Permalink
- Add gradle project and project structure
Browse files Browse the repository at this point in the history
  • Loading branch information
zero88 committed Feb 8, 2021
1 parent 57cd1c7 commit 8b8df12
Show file tree
Hide file tree
Showing 14 changed files with 761 additions and 1 deletion.
12 changes: 12 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[*]
charset = utf-8
end_of_line = lf
insert_final_newline = true
indent_style = space
indent_size = 4

[*.{json, yml, yaml}]
indent_size = 2

[*.{bat, ps1}]
end_of_line = crlf
7 changes: 7 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#
# https://help.github.com/articles/dealing-with-line-endings/
#
# These are explicitly windows files and should use crlf
* text eof=lf
*.bat text eol=crlf
*.ps1 text eol=crlf
7 changes: 6 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
.gradle
/build/

# Ignore Gradle GUI config
gradle-app.setting
Expand All @@ -12,3 +11,9 @@ gradle-app.setting

# # Work around https://youtrack.jetbrains.com/issue/IDEA-116898
# gradle/wrapper/gradle-wrapper.properties

# Ignore Gradle build output directory
**/build/
**/generated/

.idea
217 changes: 217 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@
import org.gradle.internal.jvm.Jvm
import org.gradle.util.GradleVersion
import java.time.Instant
import java.util.jar.Attributes.Name

plugins {
`java-library`
`maven-publish`
jacoco
signing
id(PluginLibs.sonarQube) version PluginLibs.Version.sonarQube
id(PluginLibs.nexusStaging) version PluginLibs.Version.nexusStaging
}
val jacocoHtml: String? by project
val semanticVersion: String by project
val buildHash: String by project
project.tasks["sonarqube"].group = "analysis"
project.tasks["sonarqube"].dependsOn("build", "jacocoRootReport")

allprojects {
group = "io.github.zero88.qwe"

repositories {
mavenLocal()
maven { url = uri("https://oss.sonatype.org/content/groups/public/") }
maven { url = uri("https://maven.mangoautomation.net/repository/ias-snapshot/") }
maven { url = uri("https://maven.mangoautomation.net/repository/ias-release/") }
mavenCentral()
jcenter()
}
}

subprojects {
apply(plugin = "java-library")
apply(plugin = "eclipse")
apply(plugin = "idea")
apply(plugin = "jacoco")
apply(plugin = "signing")
apply(plugin = "maven-publish")
project.version = "$version$semanticVersion"
project.ext.set("baseName", ProjectUtils.computeBaseName(project))
project.ext.set("title", findProperty("title") ?: project.ext.get("baseName"))
project.ext.set("description", findProperty("description") ?: "A Vertx framework for microservice: ${project.name}")

afterEvaluate {
if (setOf("iot", "connectors", "bacnet").contains(project.name)) {
project.tasks.forEach { it.enabled = false }
} else {
println("- Project Name: ${project.ext.get("baseName")}")
println("- Project Title: ${project.ext.get("title")}")
println("- Project Group: ${project.group}")
println("- Project Version: ${project.version}")
println("- Gradle Version: ${GradleVersion.current()}")
println("- Java Version: ${Jvm.current()}")
}
}

java {
sourceCompatibility = JavaVersion.VERSION_1_8
withJavadocJar()
withSourcesJar()
}

dependencies {
compileOnly(UtilLibs.lombok)
annotationProcessor(UtilLibs.lombok)

testImplementation(TestLibs.junit5Api)
testImplementation(TestLibs.junit5Engine)
testImplementation(TestLibs.junit5Vintage)
testImplementation(TestLibs.jsonAssert)
testCompileOnly(UtilLibs.lombok)
testAnnotationProcessor(UtilLibs.lombok)
}

tasks {
jar {
manifest {
attributes(
mapOf(
Name.MANIFEST_VERSION.toString() to "1.0",
Name.IMPLEMENTATION_TITLE.toString() to archiveBaseName.get(),
Name.IMPLEMENTATION_VERSION.toString() to project.version,
"Created-By" to GradleVersion.current(),
"Build-Jdk" to Jvm.current(),
"Build-By" to project.property("buildBy"),
"Build-Hash" to project.property("buildHash"),
"Build-Date" to Instant.now()
)
)
}
}
javadoc {
title = "${project.ext.get("title")} ${project.version} API"
options {
this as StandardJavadocDocletOptions
tags = mutableListOf(
"apiNote:a:API Note:", "implSpec:a:Implementation Requirements:",
"implNote:a:Implementation Note:"
)
}
}
test {
useJUnitPlatform()
}
withType<Jar>().configureEach {
archiveBaseName.set(project.ext.get("baseName") as String)
}
withType<Sign>().configureEach {
onlyIf { project.hasProperty("release") }
}
}

publishing {
publications {
create<MavenPublication>("maven") {
groupId = project.group as String?
artifactId = project.ext.get("baseName") as String
version = project.version as String?
from(components["java"])

versionMapping {
usage("java-api") {
fromResolutionOf("runtimeClasspath")
}
usage("java-runtime") {
fromResolutionResult()
}
}
pom {
name.set(project.ext.get("title") as String)
description.set(project.ext.get("description") as String)
url.set("https://github.com/zero88/qwe")
licenses {
license {
name.set("The Apache License, Version 2.0")
url.set("https://github.com/zero88/qwe/blob/master/LICENSE")
}
}
developers {
developer {
id.set("zero88")
email.set("[email protected]")
}
}
scm {
connection.set("scm:git:git://[email protected]:zero88/qwe.git")
developerConnection.set("scm:git:ssh://[email protected]:zero88/qwe.git")
url.set("https://github.com/zero88/qwe")
}
}
}
}
repositories {
maven {
val path = if (project.hasProperty("github")) {
"${project.property("github.nexus.url")}/${project.property("nexus.username")}/${rootProject.name}"
} else {
val releasesRepoUrl = project.property("ossrh.release.url")
val snapshotsRepoUrl = project.property("ossrh.snapshot.url")
if (project.hasProperty("release")) releasesRepoUrl else snapshotsRepoUrl
}
url = path?.let { uri(it) }!!
credentials {
username = project.property("nexus.username") as String?
password = project.property("nexus.password") as String?
}
}
}
}

signing {
useGpgCmd()
sign(publishing.publications["maven"])
}
}

task<JacocoReport>("jacocoRootReport") {
dependsOn(subprojects.map { it.tasks.withType<Test>() })
dependsOn(subprojects.map { it.tasks.withType<JacocoReport>() })
additionalSourceDirs.setFrom(subprojects.map { it.sourceSets.main.get().allSource.srcDirs })
sourceDirectories.setFrom(subprojects.map { it.sourceSets.main.get().allSource.srcDirs })
classDirectories.setFrom(subprojects.map { it.sourceSets.main.get().output })
executionData.setFrom(project.fileTree(".") {
include("**/build/jacoco/test.exec")
})
reports {
csv.isEnabled = false
xml.isEnabled = true
xml.destination = file("${buildDir}/reports/jacoco/coverage.xml")
html.isEnabled = (jacocoHtml ?: "true").toBoolean()
html.destination = file("${buildDir}/reports/jacoco/html")
}
}

sonarqube {
properties {
property("jacocoHtml", "false")
property("sonar.sourceEncoding", "UTF-8")
property("sonar.coverage.jacoco.xmlReportPaths", "${buildDir}/reports/jacoco/coverage.xml")
}
}

task<Sign>("sign") {
dependsOn(subprojects.map { it.tasks.withType<Sign>() })
}

nexusStaging {
packageGroup = "io.github.zero88"
username = project.property("nexus.username") as String?
password = project.property("nexus.password") as String?
}

tasks.test {
// Use junit platform for unit tests.
useJUnitPlatform()
}
7 changes: 7 additions & 0 deletions buildSrc/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
plugins {
`kotlin-dsl`
}

repositories {
jcenter()
}
54 changes: 54 additions & 0 deletions buildSrc/src/main/groovy/ProjectUtils.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import org.gradle.api.Project

class ProjectUtils {

static boolean isSubProject(Project project, String projectName) {
if (project.parent == null) {
return project.name == projectName
}
return project.parent.name == projectName || isSubProject(project.parent, projectName)
}

static String computeGroup(Project project) {
if (project.parent == null) {
return project.group
}
def suffix = project.parent == project.rootProject ? "" : ("." + project.parent.name)
return computeGroup(project.parent) + suffix
}

static String computeBaseName(Project project) {
return computeProjectName(project, "-")
}

static String computeDockerName(Project project) {
return computeProjectName(project, "-", "/")
}

def static loadSecretProps(Project project, secretFile) {
def sf = new File(secretFile.toString())
if (sf.exists()) {
def props = new Properties()
sf.withInputStream { props.load(it) }
props.findAll { Strings.isBlank(extraProp(project, it.key.toString())) }
.each { k, v -> project.ext.set(k, v) }
}
}

static String extraProp(Project project, String key) {
return extraProp(project, key, null)
}

static String extraProp(Project project, String key, String fallback) {
return project.ext.has(key) && !Strings.isBlank((String) project.ext.get(key)) ? (String) project.ext.get(key) :
fallback
}

private static String computeProjectName(Project project, String sep, String firstSep = null) {
if (project.parent == null) {
return extraProp(project, "baseName", project.name)
}
final def s = project.parent.parent == null && firstSep ? firstSep : sep
return computeProjectName(project.parent, sep, firstSep) + s + project.projectDir.name
}
}
37 changes: 37 additions & 0 deletions buildSrc/src/main/groovy/Strings.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
class Strings {

static boolean isBlank(String text) {
return text == null || "" == text.trim()
}

static String requireNotBlank(String text) {
return requireNotBlank(text, "Not blank")
}

static String requireNotBlank(String text, String message) {
if (isBlank(text)) {
throw new IllegalArgumentException(message)
}
return text.trim()
}

static String toSnakeCase(String text, boolean upper = true) {
if (upper && text == text.toUpperCase()) {
return text
}
if (!upper && text == text.toLowerCase()) {
return text
}
def regex = upper ? "A-Z" : "a-z"
def t = text.replaceAll(/([$regex])/, /_$1/).replaceAll(/^_/, '')
return upper ? t.toUpperCase() : t.toLowerCase()
}

static String replaceJsonSuffix(String name) {
return name.replaceAll(toRegexIgnoreCase("_JSON(_ARRAY)?|_ARRAY\$"), "")
}

static String toRegexIgnoreCase(String name) {
return "(?i:${name})"
}
}
Loading

0 comments on commit 8b8df12

Please sign in to comment.