All Downloads are FREE. Search and download functionalities are using the official Maven repository.

er4.bb4-gradle.1.8.source-code.bb4-publish.gradle Maven / Gradle / Ivy

The newest version!
// Copyright by Barry G. Becker, 2013 - 2021. Licensed under MIT License: http://www.opensource.org/licenses/MIT

/**
 * This common gradle build file is used to publish bb4 artifacts to maven Sonatype repository.
 * Any project on github can include it using
 * apply from: project.buildscript.classLoader.getResource('bb4-publish.gradle').toURI()
 */

/** javadoc jar used for the whole project. Used only if jarMap was not specified. */
task javadocJar(type: Jar, dependsOn: javadoc) {
    description = 'Create a jar file containing all project javadoc. Needed by Sonatype release.'
    duplicatesStrategy = DuplicatesStrategy.EXCLUDE
    archiveClassifier.set('javadoc')
    from javadoc.destinationDir
}

/** scaladoc jar used for the whole project. Used only if jarMap was not specified. */
task scaladocJar(type: Jar, dependsOn: scaladoc) {
    description = 'Create a jar file containing all project scaladoc. Needed by Sonatype release.'
    duplicatesStrategy = DuplicatesStrategy.EXCLUDE
    archiveClassifier.set('scaladoc')
    from scaladoc.destinationDir
}

/** sources jar used for the whole project. Used only if jarMap was not specified. */
task sourcesJar(type: Jar, dependsOn: classes) {
    description = 'Create a jar file containing all project sources. Needed by Sonatype release.'
    duplicatesStrategy = DuplicatesStrategy.EXCLUDE
    archiveClassifier.set('sources')
    from sourceSets.main.allSource
}

javadoc.doLast {
    description = 'Copy images from source to javadoc in case they are referenced by documentation'
    copy {
        from sourceSets.main.allSource
        into javadoc.destinationDir
        include '**/package.png'
        duplicatesStrategy = DuplicatesStrategy.EXCLUDE
    }
}

/**
 * If a project has multiple apps, it should define them in a map called jarMap before
 * applying this bb4-publish.gradle file.
 * Skip building the default jars that contain everything if there is a specific list.
 */
if (hasProperty('jarMap')) {
    jar.enabled = false
    sourcesJar.enabled = false
    javadocJar.enabled = false
    scaladocJar.enabled = false
} else {
    ext.jarMap = new LinkedHashMap()
}

/**
 * Create jar, java/scaladocJar, and sourceJar for every entry in jarMap (if there are any).
 */
jarMap.each { String key, Map value ->
    task "${key}Jar"(type: Jar)  {
        archiveBaseName = "bb4-${key}"
        from sourceSets.main.output
        value['include'].each { pattern -> include pattern }
        if (value.containsKey('exclude'))
            value['exclude'].each { pattern -> exclude pattern }
        manifest {
            attributes 'Implementation-Title': "${key} code",
                    'Implementation-Version': archiveVersion,
                    'Application-Name': "${key}",
                    'Built-By': System.properties['user.name'],
                    'provider': 'gradle'
        }
    }
    task "${key}JavadocJar"(type: Jar, dependsOn: javadoc) {
        description = "Creates a javadoc jar file for ${key}. Needed for publishing to Sonatype."
        duplicatesStrategy = DuplicatesStrategy.EXCLUDE
        classifier = 'javadoc'
        archiveBaseName = "bb4-${key}"
        from javadoc.destinationDir
        value['include'].each { pattern -> include pattern }
    }
    task "${key}ScaladocJar"(type: Jar, dependsOn: scaladoc) {
        description = "Creates a scaladoc jar file for ${key}. Needed for publishing to Sonatype."
        duplicatesStrategy = DuplicatesStrategy.EXCLUDE
        classifier = 'scaladoc'
        baseName = "bb4-${key}"
        from javadoc.destinationDir
        value['include'].each { pattern -> include pattern }
    }
    task "${key}SourcesJar"(type: Jar, dependsOn: classes) {
        description = "Creates a source jar file for ${key}. Needed for publishing to Sonatype."
        duplicatesStrategy = DuplicatesStrategy.EXCLUDE
        classifier = 'sources'
        baseName = "bb4-${key}"
        from sourceSets.main.allSource
        value['include'].each { pattern -> include pattern }
        if (value.containsKey('exclude'))
            value['exclude'].each { pattern -> exclude pattern }
    }
}

/**
 * These artifacts need to be published so other projects can depend on them.
 * The default jars are only included if there is not an explicit jarMap.
 */
artifacts {
    if (jarMap.size() == 0) {
        archives scaladocJar
        archives jar
        archives sourcesJar
        archives javadocJar
    }

    jarMap.each { String key, Map value ->
        archives tasks["${key}Jar"]
        archives tasks["${key}JavadocJar"]
        archives tasks["${key}ScaladocJar"]
        archives tasks["${key}SourcesJar"]
        archives file("build/libs/bb4-${key}-${version}.jar.asc")
    }
}

/** bb4 pom configuration for core jar artifacts */
def jarPomConfig = {
    name = "${project.name}"
    packaging = 'jar'
    description = "${project.name} java code."
    url = "https://github.com/barrybecker4/${project.name}"

    scm {
        url = "scm:[email protected]:barrybecker4/${project.name}.git"
        connection = "scm:[email protected]:barrybecker4/${project.name}.git"
        developerConnection = "scm:[email protected]:barrybecker4/${project.name}.git"
    }

    licenses {
        license {
            name = 'The MIT license'
            url = 'http://www.opensource.org/licenses/MIT'
            distribution = 'repo'
        }
    }

    developers {
        developer {
            id = 'barrybecker4'
            name = 'Barry G. Becker'
            email = '[email protected]'
        }
    }
}

/**
 * Used to publish artifacts to Sonatype repository
 * See http://jedicoder.blogspot.com/2011/11/automated-gradle-project-deployment-to.html
 * Go to https://oss.sonatype.org to view staged repositories after publishing.
 */
publishing {
    publications {
        if (jarMap.size() == 0) {
            mavenJava(MavenPublication) {
                artifactId = "${project.name}"
                groupId = 'com.barrybecker4'
                from components.java

                artifact sourcesJar
                artifact javadocJar
                artifact scaladocJar
                pom jarPomConfig
            }
        }
        else {
            jarMap.each { String key, Map value ->
                "maven${key}"(MavenPublication) {
                    from components.java

                    afterEvaluate {
                        artifactId = "bb4-$key"
                        groupId = 'com.barrybecker4'

                        artifact tasks["${key}Jar"]
                        artifact tasks["${key}JavadocJar"]
                        artifact tasks["${key}ScaladocJar"]
                        artifact tasks["${key}SourcesJar"]
                        artifact file("build/libs/bb4-${key}-${version}.jar.asc")
                    }

                    pom jarPomConfig
                }
            }
        }
    }

    repositories {
        maven {
            def releasesRepoUrl = 'https://oss.sonatype.org/service/local/staging/deploy/maven2/'
            def snapshotRepoUrl = 'https://oss.sonatype.org/content/repositories/snapshots/'
            url = version.endsWith('SNAPSHOT') ? snapshotRepoUrl : releasesRepoUrl
            credentials {
                username = sonatypeUsername
                password = sonatypePassword
            }
        }
    }
}

/** release jars need to be signed when published to Sonatype so the publisher can be verified. */
signing {
    required {
        isReleaseVersion
    }
    sign publishing.publications
}

// Try and make gradle 7 validation happy
jarMap.each { String key, Map value ->
    publish.configure {
        mustRunAfter "signMaven${key}Publication"
    }
}

/** Effectively the same as publish, but I like this name better */
task publishArtifacts (dependsOn:publish) {
    description = 'Publish artifacts to Sonatype repository'
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy