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