PublishingPlugin.groovy
001 /*
002  * SPDX-License-Identifier: Apache-2.0
003  *
004  * Copyright 2018-2019 Andres Almiray.
005  *
006  * Licensed under the Apache License, Version 2.0 (the "License");
007  * you may not use this file except in compliance with the License.
008  * You may obtain a copy of the License at
009  *
010  *     http://www.apache.org/licenses/LICENSE-2.0
011  *
012  * Unless required by applicable law or agreed to in writing, software
013  * distributed under the License is distributed on an "AS IS" BASIS,
014  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015  * See the License for the specific language governing permissions and
016  * limitations under the License.
017  */
018 package org.kordamp.gradle.plugin.publishing
019 
020 import groovy.transform.CompileDynamic
021 import groovy.transform.CompileStatic
022 import org.gradle.api.Project
023 import org.gradle.api.Task
024 import org.gradle.api.plugins.JavaBasePlugin
025 import org.gradle.api.publish.maven.MavenPublication
026 import org.gradle.api.publish.maven.plugins.MavenPublishPlugin
027 import org.gradle.plugins.signing.SigningPlugin
028 import org.kordamp.gradle.plugin.AbstractKordampPlugin
029 import org.kordamp.gradle.plugin.apidoc.ApidocPlugin
030 import org.kordamp.gradle.plugin.base.BasePlugin
031 import org.kordamp.gradle.plugin.base.ProjectConfigurationExtension
032 import org.kordamp.gradle.plugin.base.model.Credentials
033 import org.kordamp.gradle.plugin.base.model.Repository
034 import org.kordamp.gradle.plugin.base.plugins.util.PublishingUtils
035 import org.kordamp.gradle.plugin.buildinfo.BuildInfoPlugin
036 import org.kordamp.gradle.plugin.groovydoc.GroovydocPlugin
037 import org.kordamp.gradle.plugin.jar.JarPlugin
038 import org.kordamp.gradle.plugin.javadoc.JavadocPlugin
039 import org.kordamp.gradle.plugin.source.SourceJarPlugin
040 
041 import static org.kordamp.gradle.PluginUtils.resolveEffectiveConfig
042 import static org.kordamp.gradle.StringUtils.isNotBlank
043 import static org.kordamp.gradle.plugin.base.BasePlugin.isRootProject
044 
045 /**
046  * Configures artifact publication.
047  *
048  @author Andres Almiray
049  @since 0.2.0
050  */
051 @CompileStatic
052 class PublishingPlugin extends AbstractKordampPlugin {
053     Project project
054 
055     void apply(Project project) {
056         this.project = project
057 
058         if (isRootProject(project)) {
059             if (project.childProjects.size()) {
060                 project.childProjects.values().each {
061                     configureProject(it)
062                 }
063             else {
064                 configureProject(project)
065             }
066         else {
067             configureProject(project)
068         }
069     }
070 
071     static void applyIfMissing(Project project) {
072         if (!project.plugins.findPlugin(PublishingPlugin)) {
073             project.plugins.apply(PublishingPlugin)
074         }
075         if (!project.plugins.findPlugin(SigningPlugin)) {
076             project.plugins.apply(SigningPlugin)
077         }
078     }
079 
080     private void configureProject(Project project) {
081         if (hasBeenVisited(project)) {
082             return
083         }
084         setVisited(project, true)
085 
086         BasePlugin.applyIfMissing(project)
087         BuildInfoPlugin.applyIfMissing(project)
088         SourceJarPlugin.applyIfMissing(project)
089         ApidocPlugin.applyIfMissing(project)
090         JarPlugin.applyIfMissing(project)
091 
092         project.plugins.withType(JavaBasePlugin) {
093             if (!project.plugins.findPlugin(MavenPublishPlugin)) {
094                 project.plugins.apply(MavenPublishPlugin)
095             }
096         }
097 
098         project.afterEvaluate {
099             project.plugins.withType(JavaBasePlugin) {
100                 updatePublications(project)
101             }
102         }
103     }
104 
105     @CompileDynamic
106     private void updatePublications(Project project) {
107         ProjectConfigurationExtension effectiveConfig = resolveEffectiveConfig(project)
108 
109         if (!effectiveConfig.publishing.enabled || !project.sourceSets.findByName('main')) {
110             setEnabled(false)
111             return
112         }
113 
114         Task javadocJar = project.tasks.findByName(JavadocPlugin.JAVADOC_JAR_TASK_NAME)
115         Task groovydocJar = project.tasks.findByName(GroovydocPlugin.GROOVYDOC_JAR_TASK_NAME)
116         Task sourceJar = project.tasks.findByName(SourceJarPlugin.SOURCE_JAR_TASK_NAME)
117 
118         project.publishing {
119             publications {
120                 main(MavenPublication) {
121                     from project.components.java
122 
123                     if (javadocJar?.enabledartifact javadocJar
124                     if (groovydocJar?.enabledartifact groovydocJar
125                     if (sourceJar?.enabledartifact sourceJar
126 
127                     PublishingUtils.configurePom(pom, effectiveConfig, effectiveConfig.publishing.pom)
128                 }
129             }
130 
131             String repositoryName = effectiveConfig.release ? effectiveConfig.publishing.releasesRepository : effectiveConfig.publishing.snapshotsRepository
132             if (isNotBlank(repositoryName)) {
133                 Repository repo = effectiveConfig.info.repositories.getRepository(repositoryName)
134                 if (repo == null) {
135                     throw new IllegalStateException("Repository '${repositoryName}' was not found")
136                 }
137 
138                 repositories {
139                     maven {
140                         name = repositoryName
141                         url = repo.url
142                         Credentials creds = effectiveConfig.info.credentials.getCredentials(repo.name)
143                         if (repo.credentials && !repo.credentials.empty) {
144                             credentials {
145                                 username = repo.credentials.username
146                                 password = repo.credentials.password
147                             }
148                         else if (creds && !creds.empty) {
149                             credentials {
150                                 username = creds.username
151                                 password = creds.password
152                             }
153                         }
154                     }
155                 }
156             }
157         }
158 
159         if (effectiveConfig.publishing.signing) {
160             project.signing {
161                 sign project.publishing.publications.main
162             }
163         }
164     }
165 }