plugins {
    id 'java'
    id 'maven-publish'
    id 'jacoco'
    id 'jacoco-report-aggregation'
    id 'pmd'
	id 'checkstyle'
	id 'jvm-test-suite'
    id 'io.quarkus'
    id "org.sonarqube" version "3.3"
    alias(versionsLibs.plugins.asciidoctorConvert)
    alias(versionsLibs.plugins.asciidoctorPdf)
    alias(versionsLibs.plugins.asciidoctorGems)
}

repositories {
    ruby.gems()
    maven {
        url "https://nexus.thpeetz.de/nexus/content/groups/public/"
    }
    mavenCentral()
    mavenLocal()
}

dependencies {
    implementation enforcedPlatform("${quarkusPlatformGroupId}:${quarkusPlatformArtifactId}:${quarkusPlatformVersion}")
    implementation 'io.quarkus:quarkus-resteasy-reactive'
    implementation 'io.quarkus:quarkus-resteasy-reactive-jackson'
    implementation 'io.quarkus:quarkus-arc'
    testImplementation 'io.quarkus:quarkus-junit5'
    testImplementation 'io.rest-assured:rest-assured'
    testImplementation("io.quarkus:quarkus-jacoco")
}

final BUILD_DATE = new Date().format('dd.MM.yyyy').toString()
def pdfFile = layout.buildDirectory.file("docs/asciidocPdf/" + project.name + ".pdf")
def pdfArtifact = artifacts.add('archives', pdfFile.get().asFile) {
	type 'pdf'
	builtBy asciidoctorPdf
}

tasks.withType(GenerateModuleMetadata).configureEach {
	suppressedValidationErrors.add('enforced-platform')
}

publishing {
    publications {
        quarkusApp(MavenPublication){
            from components.java
        }
        docs(MavenPublication) {
            groupId = group + '.docs'
            artifactId = project.name
            artifact pdfArtifact
        }
    }
    repositories {
        maven {
			name = 'nexusPeetz'
            def releasesRepoUrl = "https://nexus.thpeetz.de/nexus/content/repositories/releases/"
            def snapshotsRepoUrl = "https://nexus.thpeetz.de/nexus/content/repositories/snapshots/"
            url = version.endsWith('SNAPSHOT') ? snapshotsRepoUrl : releasesRepoUrl
            credentials(PasswordCredentials)
        }
    }
}

java {
    sourceCompatibility = JavaVersion.VERSION_11
    targetCompatibility = JavaVersion.VERSION_11
}

tasks.withType(GenerateModuleMetadata).configureEach {
    suppressedValidationErrors.add('enforced-platform')
}

compileJava {
    options.encoding = 'UTF-8'
    options.compilerArgs << '-parameters'
}

compileTestJava {
    options.encoding = 'UTF-8'
}

jacocoTestReport {
    reports {
        xml.required = true
    }
}

test.finalizedBy jacocoTestReport


pmd {
	//ruleSetFiles = files("custom-pmd-ruleset.xml")
	ruleSets = ["category/java/errorprone.xml", "category/java/bestpractices.xml"]
	ignoreFailures = true
}

tasks.withType(Checkstyle) {
	reports {
		xml.required = true
		html.required = true
	}
}

testing {
	suites {
		test {
			useJUnitJupiter()
		}
	}
}

pmdTest.dependsOn("compileQuarkusTestGeneratedSourcesJava")
checkstyleTest.dependsOn("compileQuarkusTestGeneratedSourcesJava")
jacocoTestReport.dependsOn("compileQuarkusGeneratedSourcesJava")
pmdMain.dependsOn("compileQuarkusGeneratedSourcesJava")

sonarqube {
    properties {
        property "sonar.host.url", "https://sonar.thpeetz.de"
        property "sonar.qualitygate.wait", true
        property "sonar.sourceEncoding", "UTF-8"
		property "sonar.projectKey", "kontor_kontor-quarkus_AYQek8Mxz0hBjLSV8I8O"
		property "sonar.login", "5ecd90dee57806857e07443a9b0efd3cd7774a81"
    }
}

tasks.named('sonarqube').configure {
    dependsOn 'test', 'pmdMain', 'checkstyleMain', 'jacocoTestReport'
}

asciidoctorPdf {
	baseDirFollowsSourceFile()

	asciidoctorj {
		attributes 'build-gradle':       file('build.gradle'),
				   'endpoint-url':       'https://www.thpeetz.de',
				   'source-highlighter': 'rouge',
				   'imagesdir':          './images',
				   'toc':                'left',
				   'toc-title':          'Inhaltsverzeichnis',
				   'revdate':            BUILD_DATE,
				   'revnumber':          { project.version.toString() },
				   'revremark':          'Entwurf',
				   'chapter-label':      '',
				   'icons':              'font',
				   'idprefix':           'id_',
				   'idseparator':        '-',
				   'docinfo1':           ''
	}
}

wrapper {
    gradleVersion = "7.5"
}
