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

io.spring.gradle.release.SpringPublishingPlugin.groovy Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2017 Pivotal Software, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.spring.gradle.release

import io.spring.gradle.bintray.SpringBintrayExtension
import io.spring.gradle.bintray.SpringBintrayPlugin
import io.spring.gradle.bintray.task.PublishTask
import nebula.plugin.contacts.ContactsPlugin
import nebula.plugin.info.InfoPlugin
import nebula.plugin.publishing.maven.MavenPublishPlugin
import nebula.plugin.publishing.maven.license.MavenApacheLicensePlugin
import nebula.plugin.publishing.publications.JavadocJarPlugin
import nebula.plugin.publishing.publications.SourceJarPlugin
import org.ajoberstar.grgit.Grgit
import org.ajoberstar.grgit.operation.OpenOp
import org.eclipse.jgit.errors.RepositoryNotFoundException
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.Task
import org.gradle.api.publish.plugins.PublishingPlugin
import org.gradle.api.tasks.javadoc.Javadoc
import org.gradle.api.tasks.testing.Test
import org.jfrog.gradle.plugin.artifactory.ArtifactoryPlugin

class SpringPublishingPlugin implements Plugin {
    private Project project

    @Override
    void apply(Project project) {
        this.project = project

        project.plugins.apply PublishingPlugin
        project.plugins.apply MavenPublishPlugin
        project.plugins.apply MavenApacheLicensePlugin
        project.plugins.apply JavadocJarPlugin
        project.plugins.apply SourceJarPlugin
        project.plugins.apply InfoPlugin
        project.plugins.apply ContactsPlugin
        project.plugins.apply ArtifactoryPlugin

        project.tasks.create('downloadDependencies', DownloadDependenciesTask.class)

        project.tasks.withType(Javadoc) {
            failOnError = false
        }

        project.tasks.withType(Test) { Test testTask ->
            testTask.testLogging.exceptionFormat = 'full'
        }

        project.plugins.apply SpringBintrayPlugin

        SpringBintrayExtension bintray = project.extensions.getByType(SpringBintrayExtension)

        String[] githubRemote = findGithubRemote()
        String githubProject, githubOrg
        if(githubRemote)
            (githubOrg, githubProject) = githubRemote

        bintray.with {
            repo = 'jars'
            org = 'spring'

            publication = 'nebula'

            websiteUrl = "https://github.com/$githubOrg/$githubProject"
            vcsUrl = "https://github.com/$githubOrg/${githubProject}.git"
            issueTrackerUrl = "https://github.com/$githubOrg/$githubProject/issues"

            ossrhUser = project.findProperty('sonatypeUsername')
            ossrhPassword = project.findProperty('sonatypePassword')

            gpgPassphrase = project.findProperty('gpgPassphrase')

            licenses = ['Apache-2.0']
        }

        configureArtifactoryPlugin()
        project.afterEvaluate {
            project.rootProject.tasks.findByName('snapshot')?.dependsOn('artifactoryPublish')
        }

        // When you want to generate a candidate or final release, sync to JCenter
        project.tasks.withType(PublishTask) { Task task ->
            // the nebula release plugin should only be applied at the root
            project.rootProject.tasks.findByName('candidate')?.dependsOn(task)
            project.rootProject.tasks.findByName('final')?.dependsOn(task)
        }
    }

    // used only for snapshots
    private void configureArtifactoryPlugin() {
        def artifactoryConvention = project.convention.plugins.artifactory

        artifactoryConvention.contextUrl = 'https://repo.spring.io'
        artifactoryConvention.publish {
            repository {
                repoKey = 'libs-snapshot-local'
                if (project.hasProperty('springArtifactoryUser')) {
                    username = project.property('springArtifactoryUser')
                    password = project.property('springArtifactoryPassword')
                }
            }
            defaults {
                publications 'nebula'
            }
        }
    }

    private String[] findGithubRemote() {
        try {
            Grgit git = new OpenOp(dir: project.rootProject.rootDir).call()

            // Remote URLs will be formatted like one of these:
            //  https://github.com/spring-gradle-plugins/spring-project-plugin.git
            //  [email protected]:spring-gradle-plugins/spring-release-plugin.git
            def repoParts = git.remote.list().collect { it.url =~ /github\.com[\/:]([^\/]+)\/(.+)\.git/ }
                    .find { it.count == 1 }

            if (repoParts == null) {
                // no remote configured yet, do nothing
                return null
            }

            return repoParts[0].drop(1)
        } catch (RepositoryNotFoundException ignored) {
            // do nothing
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy