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.Action
023 import org.gradle.api.Project
024 import org.gradle.api.Task
025 import org.gradle.api.plugins.JavaBasePlugin
026 import org.gradle.api.publish.maven.MavenPublication
027 import org.gradle.api.publish.maven.plugins.MavenPublishPlugin
028 import org.gradle.plugins.signing.SigningPlugin
029 import org.kordamp.gradle.plugin.AbstractKordampPlugin
030 import org.kordamp.gradle.plugin.apidoc.ApidocPlugin
031 import org.kordamp.gradle.plugin.base.BasePlugin
032 import org.kordamp.gradle.plugin.base.ProjectConfigurationExtension
033 import org.kordamp.gradle.plugin.base.model.Credentials
034 import org.kordamp.gradle.plugin.base.model.Repository
035 import org.kordamp.gradle.plugin.base.plugins.util.PublishingUtils
036 import org.kordamp.gradle.plugin.buildinfo.BuildInfoPlugin
037 import org.kordamp.gradle.plugin.groovydoc.GroovydocPlugin
038 import org.kordamp.gradle.plugin.jar.JarPlugin
039 import org.kordamp.gradle.plugin.javadoc.JavadocPlugin
040 import org.kordamp.gradle.plugin.source.SourceJarPlugin
041 
042 import static org.kordamp.gradle.PluginUtils.resolveEffectiveConfig
043 import static org.kordamp.gradle.StringUtils.isNotBlank
044 import static org.kordamp.gradle.plugin.base.BasePlugin.isRootProject
045 
046 /**
047  * Configures artifact publication.
048  *
049  @author Andres Almiray
050  @since 0.2.0
051  */
052 @CompileStatic
053 class PublishingPlugin extends AbstractKordampPlugin {
054     Project project
055 
056     void apply(Project project) {
057         this.project = project
058 
059         if (isRootProject(project)) {
060             if (project.childProjects.size()) {
061                 project.childProjects.values().each {
062                     configureProject(it)
063                 }
064             else {
065                 configureProject(project)
066             }
067         else {
068             configureProject(project)
069         }
070     }
071 
072     static void applyIfMissing(Project project) {
073         if (!project.plugins.findPlugin(PublishingPlugin)) {
074             project.plugins.apply(PublishingPlugin)
075         }
076         if (!project.plugins.findPlugin(SigningPlugin)) {
077             project.plugins.apply(SigningPlugin)
078         }
079     }
080 
081     private void configureProject(Project project) {
082         if (hasBeenVisited(project)) {
083             return
084         }
085         setVisited(project, true)
086 
087         BasePlugin.applyIfMissing(project)
088         BuildInfoPlugin.applyIfMissing(project)
089         SourceJarPlugin.applyIfMissing(project)
090         ApidocPlugin.applyIfMissing(project)
091         JarPlugin.applyIfMissing(project)
092 
093         project.plugins.withType(JavaBasePlugin) {
094             if (!project.plugins.findPlugin(MavenPublishPlugin)) {
095                 project.plugins.apply(MavenPublishPlugin)
096             }
097         }
098 
099         project.afterEvaluate {
100             project.plugins.withType(JavaBasePlugin) {
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 configuration.'
111                 }
112             })
113 
114         project.tasks.addRule('Pattern: <ID>PublicationSettings: Displays configuration 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 configuration for 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                 main(MavenPublication) {
149                     from project.components.java
150 
151                     if (javadocJar?.enabledartifact javadocJar
152                     if (groovydocJar?.enabledartifact groovydocJar
153                     if (sourceJar?.enabledartifact sourceJar
154 
155                     PublishingUtils.configurePom(pom, effectiveConfig, effectiveConfig.publishing.pom)
156                 }
157             }
158 
159             String repositoryName = effectiveConfig.release ? effectiveConfig.publishing.releasesRepository : effectiveConfig.publishing.snapshotsRepository
160             if (isNotBlank(repositoryName)) {
161                 Repository repo = effectiveConfig.info.repositories.getRepository(repositoryName)
162                 if (repo == null) {
163                     throw new IllegalStateException("Repository '${repositoryName}' was not found")
164                 }
165 
166                 repositories {
167                     maven {
168                         name = repositoryName
169                         url = repo.url
170                         Credentials creds = effectiveConfig.info.credentials.getCredentials(repo.name)
171                         if (repo.credentials && !repo.credentials.empty) {
172                             credentials {
173                                 username = repo.credentials.username
174                                 password = repo.credentials.password
175                             }
176                         else if (creds && !creds.empty) {
177                             credentials {
178                                 username = creds.username
179                                 password = creds.password
180                             }
181                         }
182                     }
183                 }
184             }
185         }
186 
187         PublishingUtils.configureSigning(effectiveConfig, project)
188     }
189 }