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  *     https://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.Action
023 import org.gradle.api.Project
024 import org.gradle.api.Task
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.pluginManager.apply(PublishingPlugin)
074         }
075         if (!project.plugins.findPlugin(SigningPlugin)) {
076             project.pluginManager.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         ApidocPlugin.applyIfMissing(project)
089 
090         project.pluginManager.withPlugin('java-base') {
091             SourceJarPlugin.applyIfMissing(project)
092             JarPlugin.applyIfMissing(project)
093 
094             if (!project.pluginManager.hasPlugin('maven-publish')) {
095                 project.pluginManager.apply(MavenPublishPlugin)
096             }
097         }
098 
099         project.pluginManager.withPlugin('maven-publish') {
100             project.afterEvaluate {
101                 updatePublications(project)
102             }
103         }
104 
105         project.tasks.register('publicationSettings', PublicationSettingsTask,
106             new Action<PublicationSettingsTask>() {
107                 @Override
108                 void execute(PublicationSettingsTask t) {
109                     t.group = 'Insight'
110                     t.description = 'Display publication settings.'
111                 }
112             })
113 
114         project.tasks.addRule('Pattern: <PublicationName>PublicationSettings: Displays settings of a Publication.', new Action<String>() {
115             @Override
116             void execute(String publicationName) {
117                 if (publicationName.endsWith('PublicationSettings')) {
118                     String resolvedPublicationName = publicationName - 'Settings'
119                     project.tasks.register(publicationName, PublicationSettingsTask,
120                         new Action<PublicationSettingsTask>() {
121                             @Override
122                             void execute(PublicationSettingsTask t) {
123                                 t.group = 'Insight'
124                                 t.publication = resolvedPublicationName
125                                 t.description = "Display settings of the '${resolvedPublicationName}' Publication."
126                             }
127                         })
128                 }
129             }
130         })
131     }
132 
133     @CompileDynamic
134     private void updatePublications(Project project) {
135         ProjectConfigurationExtension effectiveConfig = resolveEffectiveConfig(project)
136 
137         if (!effectiveConfig.publishing.enabled || !project.sourceSets.findByName('main')) {
138             setEnabled(false)
139             return
140         }
141 
142         Task javadocJar = project.tasks.findByName(JavadocPlugin.JAVADOC_JAR_TASK_NAME)
143         Task groovydocJar = project.tasks.findByName(GroovydocPlugin.GROOVYDOC_JAR_TASK_NAME)
144         Task sourceJar = project.tasks.findByName(SourceJarPlugin.SOURCE_JAR_TASK_NAME)
145 
146         project.publishing {
147             publications {
148                 effectiveConfig.publishing.publications.each String pub ->
149                     "${pub}"(MavenPublication) {
150                         PublishingUtils.configurePom(pom, effectiveConfig, effectiveConfig.publishing.pom)
151                     }
152                 }
153 
154                 if (!effectiveConfig.publishing.publications.contains('main') && !effectiveConfig.publishing.publications) {
155                     main(MavenPublication) {
156                         from project.components.java
157 
158                         if (javadocJar?.enabledartifact javadocJar
159                         if (groovydocJar?.enabledartifact groovydocJar
160                         if (sourceJar?.enabledartifact sourceJar
161 
162                         PublishingUtils.configurePom(pom, effectiveConfig, effectiveConfig.publishing.pom)
163                     }
164                 }
165             }
166 
167             String repositoryName = effectiveConfig.release ? effectiveConfig.publishing.releasesRepository : effectiveConfig.publishing.snapshotsRepository
168             if (isNotBlank(repositoryName)) {
169                 Repository repo = effectiveConfig.info.repositories.getRepository(repositoryName)
170                 if (repo == null) {
171                     throw new IllegalStateException("Repository '${repositoryName}' was not found")
172                 }
173 
174                 repositories {
175                     maven {
176                         name = repositoryName
177                         url = repo.url
178                         Credentials creds = effectiveConfig.info.credentials.getCredentials(repo.name)
179                         if (repo.credentials && !repo.credentials.empty) {
180                             credentials {
181                                 username = repo.credentials.username
182                                 password = repo.credentials.password
183                             }
184                         else if (creds && !creds.empty) {
185                             credentials {
186                                 username = creds.username
187                                 password = creds.password
188                             }
189                         }
190                     }
191                 }
192             }
193         }
194 
195         List<String> publications = new ArrayList<>(effectiveConfig.publishing.publications)
196         if (!publicationspublications << 'main'
197         PublishingUtils.configureSigning(effectiveConfig, project, *publications)
198     }
199 }