apply plugin: 'java'
sourceCompatibility = 1.8
targetCompatibility = 1.8

group = 'de.erichseifert.vectorgraphics2d'
version = getVersionString()
description = 'A library for adding vector export to Java(R) Graphics2D.'

ext {
    inceptionYear = 2010

    owner1_id    = 'eseifert'
    owner1_name  = 'Erich Seifert'
    owner1_email = 'dev[at]erichseifert.de'
    owner2_id    = 'mseifert'
    owner2_name  = 'Michael Seifert'
    owner2_email = 'mseifert[at]error-reports.org'

    website = 'https://github.com/eseifert/vectorgraphics2d/'

    // Determine the location of rt.jar (required for ProGuard)
    if (System.getProperty('os.name').startsWith('Mac')) {
        runtimeJar = "${System.getProperty("java.home")}/bundle/Classes/classes.jar"
    } else {
        runtimeJar = "${System.getProperty("java.home")}/lib/rt.jar"
    }
}

repositories {
    maven {
        url 'https://plugins.gradle.org/m2/'
    }
}

dependencies {
    testCompile 'junit:junit:4.12'
    testCompile 'org.hamcrest:hamcrest-library:1.3'
    testCompile 'org.ghost4j:ghost4j:1.0.1'
    testCompile 'org.apache.xmlgraphics:batik-transcoder:1.9.1'
    testRuntime 'org.apache.xmlgraphics:batik-codec:1.9.1'  // Required for images with data: URLs
}

buildscript {
    repositories {
        maven {
            url 'https://plugins.gradle.org/m2/'
        }
    }
    dependencies {
        classpath 'net.sf.proguard:proguard-gradle:5.2.+'
        classpath 'nl.javadude.gradle.plugins:license-gradle-plugin:0.11.+'
        classpath 'net.saliman:gradle-cobertura-plugin:3.0.0'
        classpath 'org.kt3k.gradle.plugin:coveralls-gradle-plugin:2.8.1'
    }
}

apply plugin: 'net.saliman.cobertura'
cobertura.coverageFormats = ['html', 'xml']

apply plugin: 'com.github.kt3k.coveralls'

apply plugin: 'license'
license {
    header(rootProject.file("${projectDir}/src/etc/header.txt"))
    strictCheck(true)
    mapping {
        java = 'SLASHSTAR_STYLE'
    }

    def currentYear = new GregorianCalendar().get(Calendar.YEAR)
    ext.year = "${inceptionYear}-${currentYear}"
    ext.owner1 = owner1_name
    ext.email1 = owner1_email
    ext.owner2 = owner2_name
    ext.email2 = owner2_email
}

jar {
    manifest {
      attributes('Automatic-Module-Name': 'de.erichseifert.vectorgraphics2d')
    }
}

task shrinkJar(type: proguard.gradle.ProGuardTask, dependsOn: jar) {
    description = 'Uses ProGuard to reduce the code size of this project.'
    group = 'Build'
    // Configure ProGuard
    configuration("${projectDir}/src/etc/proguard.conf")
    target(targetCompatibility.toString())
    injars(jar.archivePath)
    outjars("${libsDir}/shrunk/${jar.archiveName}")
    libraryjars(runtimeJar)
    libraryjars(configurations.runtime)
}

task sourceJar(type: Jar) {
    description = 'Assembles a jar archive containing the source code of the main classes.'
    group = 'Build'
    from sourceSets.main.allJava
    classifier 'sources'
}

task javadocJar(type: Jar) {
    description = 'Assembles a jar archive containing the API doc.'
    group = 'Build'
    from javadoc
    classifier 'javadoc'
}

apply plugin: 'maven'
apply plugin: 'signing'

artifacts {
    archives shrinkJar.getOutJarFileCollection().getSingleFile(), sourceJar, javadocJar
}

signing {
    required { hasProperty('signing.keyId') && gradle.taskGraph.hasTask('uploadArchives') }
    sign configurations.archives
}

uploadArchives {
    repositories {
        mavenDeployer {
            beforeDeployment { MavenDeployment deployment -> signing.signPom(deployment) }

            repository(url: 'https://oss.sonatype.org/service/local/staging/deploy/maven2/') {
                authentication(userName: project.hasProperty('ossrhUsername')?ossrhUsername:'', password: project.hasProperty('ossrhPassword')?ossrhPassword:'')
            }

            snapshotRepository(url: 'https://oss.sonatype.org/content/repositories/snapshots/') {
                authentication(userName: project.hasProperty('ossrhUsername')?ossrhUsername:'', password: project.hasProperty('ossrhPassword')?ossrhPassword:'')
            }

            pom.project {
                name rootProject.name
                packaging 'jar'
                description project.description
                url website
                inceptionYear inceptionYear
                licenses {
                    license {
                        name 'GNU Library or Lesser General Public License (LGPL)'
                        url 'http://www.gnu.org/licenses/lgpl.txt'
                    }
                }
                developers {
                    developer {
                        id owner1_id
                        name owner1_name
                        email owner1_email
                    }
                    developer {
                        id owner2_id
                        name owner2_name
                        email owner2_email
                    }
                }
                scm {
                    connection 'scm:git:git://github.com/eseifert/vectorgraphics2d.git'
                    developerConnection 'scm:git:git@github.com:eseifert/vectorgraphics2d.git'
                    url website
                }
                issueManagement {
                    system 'GitHub Issues'
                    url website
                }
            }
        }
    }
}
signArchives.dependsOn(shrinkJar)

/*
 * This method must not be named getVersion, because it would
 * overwrite the implicit getter of the version property in the
 * current Project object.
 */
def getVersionString() {
    def out = new ByteArrayOutputStream()
    exec {
        commandLine('git', 'describe', '--tags', '--always')
        standardOutput = out
    }
    return out.toString().trim()
}
