plugins {
    id('com.github.dkorotych.gradle-maven-exec')
}

println "Validate work with Gradle: ${gradle.gradleVersion}"

def gradleVersionLessThen9 = false
def mavenexec
def operations
if (gradle.gradleVersion < '9') {
    gradleVersionLessThen9 = true
    mavenexec = project.convention.plugins.get("mavenexec")
    operations = null
} else {
    mavenexec = project.properties['mavenexec']
    operations = project.properties['exec-operations']
}

tasks.register("realUseCaseTest") {
    group = "verification"
    dependsOn = ["checkMavenApplication", "checkMavenApplication2", "checkMavenApplication3"]
}

def replaceTargetSourceInPom(File applicationDir) {
    def file = file("${applicationDir}/pom.xml")
    def content = file.text
    content = content.
            replaceFirst("\\Q<maven.compiler.source>1.7</maven.compiler.source>\\E", "<maven.compiler.source>1.8</maven.compiler.source>").
            replaceFirst("\\Q<maven.compiler.target>1.7</maven.compiler.target>\\E", "<maven.compiler.target>1.8</maven.compiler.target>")
    file.text = content
}

def groupId = 'com.mycompany.app'
def artifactId = 'my-app'

tasks.register('generateMavenApplication', MavenExec) {
    def buildDir = layout.buildDirectory.asFile.get()
    buildDir.mkdirs()
    ext.outputDir = mkdir("$buildDir/$artifactId")
    delete outputDir
    workingDir buildDir
    goals 'archetype:generate'
    options {
        define = [
                'groupId'            : groupId,
                'artifactId'         : artifactId,
                'archetypeArtifactId': 'maven-archetype-quickstart',
                'archetypeVersion'   : '1.4',
                'interactiveMode'    : 'false'
        ]
    }
}

tasks.register('replaceTargetSourceInPom') {
    doLast {
        replaceTargetSourceInPom(generateMavenApplication.outputDir)
    }
}

tasks.register('packageMavenApplication', MavenExec) {
    dependsOn = ['generateMavenApplication', 'replaceTargetSourceInPom']
    outputs.dir mkdir("${generateMavenApplication.outputDir}/target")
    workingDir generateMavenApplication.outputDir
    goals 'package'
}

tasks.register('checkMavenApplication', JavaExec) {
    dependsOn 'packageMavenApplication'
    def buildDir = layout.buildDirectory.asFile.get()
    def outputFile = file("$buildDir/output.txt")
    workingDir generateMavenApplication.outputDir
    classpath file("${generateMavenApplication.outputDir}/target/${artifactId}-1.0-SNAPSHOT.jar")
    mainClass = "${groupId}.App"
    standardOutput new FileOutputStream(outputFile)
    doLast {
        assert outputFile.text.trim() == 'Hello World!'
    }
}

tasks.register('checkMavenApplication2') {
    doLast {
        def buildDir = layout.buildDirectory.asFile.get()
        buildDir.mkdirs()
        def application = artifactId + '-second'
        def applicationDir = file("$buildDir/$application")
        delete applicationDir
        def outputFile = file("$buildDir/output.txt")
        mavenexec {
            workingDir buildDir
            goals 'archetype:generate'
            define([
                    'groupId'            : groupId,
                    'artifactId'         : application,
                    'archetypeArtifactId': 'maven-archetype-quickstart',
                    'archetypeVersion'   : '1.4',
                    'interactiveMode'    : 'false'
            ])
            quiet true
        }
        replaceTargetSourceInPom(applicationDir)
        mavenexec {
            workingDir applicationDir
            goals 'package'
            threads '2C'
            quiet true
        }
        def javaExecSpecification = {
            workingDir applicationDir
            classpath file("$applicationDir/target/$application-1.0-SNAPSHOT.jar")
            mainClass = "${groupId}.App"
            standardOutput new FileOutputStream(outputFile)
        }
        if (gradleVersionLessThen9) {
            javaexec javaExecSpecification
        } else {
            operations.javaexec javaExecSpecification
        }
        assert outputFile.text.trim() == 'Hello World!'
    }
}

tasks.register('checkMavenApplication3') {
    doLast {
        def buildDir = layout.buildDirectory.asFile.get()
        buildDir.mkdirs()
        def application = artifactId + '-third'
        def applicationDir = file("$buildDir/$application")
        delete applicationDir
        mavenexec {
            workingDir buildDir
            goals 'archetype:generate'
            define([
                    'groupId'            : groupId,
                    'artifactId'         : application,
                    'archetypeArtifactId': 'maven-archetype-j2ee-simple',
                    'archetypeVersion'   : '1.4',
                    'interactiveMode'    : 'false'
            ])
            quiet true
        }
        replaceTargetSourceInPom(applicationDir)
        def file = file("${applicationDir}/pom.xml")
        def content = file.text
        content = content.
                replaceAll("2.20.1", "2.22.0").
                replaceAll("2.10.1", "3.0.1")
        file.text = content
        mavenexec {
            workingDir applicationDir
            goals 'package'
            threads '1C'
            quiet true
        }
    }
}