Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add logging & improve version detection for plugin{} block usage #371

Merged
merged 3 commits into from
Dec 13, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ import static com.bmuschko.gradle.nexus.NexusPlugin.getSIGNING_KEYRING
*/
class GrailsPublishGradlePlugin implements Plugin<Project> {

public static String NEXUS_PUBLISH_PLUGIN_ID = 'io.github.gradle-nexus.publish-plugin'

String getErrorMessage(String missingSetting) {
return """No '$missingSetting' was specified. Please provide a valid publishing configuration. Example:

Expand Down Expand Up @@ -86,19 +88,26 @@ The credentials and connection url must be specified as a project property or an
NEXUS_PUBLISH_URL
NEXUS_PUBLISH_SNAPSHOT_URL
NEXUS_PUBLISH_STAGING_PROFILE_ID

When using `NEXUS_PUBLISH`, the property `signing.secretKeyRingFile` must be set to the path of the GPG keyring file.

Note: if project properties are used, the properties must be defined prior to applying this plugin.
"""
}

@Override
void apply(Project project) {
project.rootProject.logger.lifecycle("Applying Grails Publish Gradle Plugin for `${project.name}`...");

final ExtensionContainer extensionContainer = project.extensions
final TaskContainer taskContainer = project.tasks
final GrailsPublishExtension gpe = extensionContainer.create('grailsPublish', GrailsPublishExtension)

final String mavenPublishUsername = project.findProperty('mavenPublishUsername') ?: System.getenv('MAVEN_PUBLISH_USERNAME') ?: ''
final String mavenPublishPassword = project.findProperty('mavenPublishPassword') ?: System.getenv('MAVEN_PUBLISH_PASSWORD') ?: ''
final String mavenPublishUrl = project.findProperty('mavenPublishUrl') ?: System.getenv('MAVEN_PUBLISH_URL') ?: ''

// the maven publish url can technically be a directory so do not force to String type
final def mavenPublishUrl = project.findProperty('mavenPublishUrl') ?: System.getenv('MAVEN_PUBLISH_URL') ?: ''

final String nexusPublishUrl = project.findProperty('nexusPublishUrl') ?: System.getenv('NEXUS_PUBLISH_URL') ?: ''
final String nexusPublishSnapshotUrl = project.findProperty('nexusPublishSnapshotUrl') ?: System.getenv('NEXUS_PUBLISH_SNAPSHOT_URL') ?: ''
final String nexusPublishUsername = project.findProperty('nexusPublishUsername') ?: System.getenv('NEXUS_PUBLISH_USERNAME') ?: ''
Expand All @@ -111,22 +120,46 @@ The credentials and connection url must be specified as a project property or an
extraPropertiesExtension.setProperty(SIGNING_PASSWORD, project.findProperty(SIGNING_PASSWORD) ?: System.getenv('SIGNING_PASSPHRASE'))
extraPropertiesExtension.setProperty(SIGNING_KEYRING, project.findProperty(SIGNING_KEYRING) ?: System.getenv('SIGNING_KEYRING'))


PublishType snapshotPublishType = gpe.snapshotPublishType
PublishType releasePublishType = gpe.releasePublishType

boolean isSnapshot = project.version.endsWith('SNAPSHOT')
String detectedVersion = (project.version == Project.DEFAULT_VERSION ? (project.findProperty('projectVersion') ?: Project.DEFAULT_VERSION) : project.version) as String
if (detectedVersion == Project.DEFAULT_VERSION) {
throw new IllegalStateException("Project `${project.name}` has an unspecified version (neither `version` or the property `projectVersion` is defined). Release state cannot be determined.")
}
project.rootProject.logger.info("Detected Version $detectedVersion for Project `${project.name}`")
jdaugherty marked this conversation as resolved.
Show resolved Hide resolved

boolean isSnapshot = detectedVersion.endsWith('SNAPSHOT')
if (isSnapshot) {
project.rootProject.logger.info("Snapshot detected for Project `${project.name}`")
jdaugherty marked this conversation as resolved.
Show resolved Hide resolved
}
boolean isRelease = !isSnapshot
boolean mavenPublish = (isSnapshot && snapshotPublishType == PublishType.MAVEN_PUBLISH) || (isRelease && releasePublishType == PublishType.MAVEN_PUBLISH)
boolean nexusPublish = (isSnapshot && snapshotPublishType == PublishType.NEXUS_PUBLISH) || (isRelease && releasePublishType == PublishType.NEXUS_PUBLISH)
if (isRelease) {
project.rootProject.logger.info("Release detected for Project `${project.name}`")
}

final PluginManager projectPluginManager = project.getPluginManager()
final PluginManager rootProjectPluginManager = project.rootProject.getPluginManager()
boolean useMavenPublish = (isSnapshot && snapshotPublishType == PublishType.MAVEN_PUBLISH) || (isRelease && releasePublishType == PublishType.MAVEN_PUBLISH)
if (useMavenPublish) {
project.rootProject.logger.info("Maven Publish is enabled for Project `${project.name}`")
jdaugherty marked this conversation as resolved.
Show resolved Hide resolved
}
boolean useNexusPublish = (isSnapshot && snapshotPublishType == PublishType.NEXUS_PUBLISH) || (isRelease && releasePublishType == PublishType.NEXUS_PUBLISH)
if (useNexusPublish) {
project.rootProject.logger.info("Nexus Publish is enabled for Project `${project.name}`")
jdaugherty marked this conversation as resolved.
Show resolved Hide resolved
}

// Required for the pom always
final PluginManager projectPluginManager = project.pluginManager
projectPluginManager.apply(MavenPublishPlugin)

if (nexusPublish) {
if (useNexusPublish) {
// The nexus plugin is special since it must always be applied to the root project.
// Handle when multiple subprojects exist and grailsPublish is defined in each one instead of at the root.
final PluginManager rootProjectPluginManager = project.rootProject.pluginManager
boolean hasNexusPublishApplied = rootProjectPluginManager.hasPlugin(NEXUS_PUBLISH_PLUGIN_ID)
if (hasNexusPublishApplied) {
jdaugherty marked this conversation as resolved.
Show resolved Hide resolved
project.rootProject.logger.info("Nexus Publish Plugin already applied to root project")
}

rootProjectPluginManager.apply(NexusPublishPlugin)
jdaugherty marked this conversation as resolved.
Show resolved Hide resolved
projectPluginManager.apply(SigningPlugin)

Expand All @@ -138,26 +171,28 @@ The credentials and connection url must be specified as a project property or an
onlyIf { isRelease }
}

project.rootProject.nexusPublishing {
repositories {
sonatype {
if (nexusPublishUrl) {
nexusUrl = project.uri(nexusPublishUrl)
}
if (nexusPublishSnapshotUrl) {
snapshotRepositoryUrl = project.uri(nexusPublishSnapshotUrl)
if (!hasNexusPublishApplied) {
project.rootProject.nexusPublishing {
repositories {
sonatype {
if (nexusPublishUrl) {
nexusUrl = project.uri(nexusPublishUrl)
}
if (nexusPublishSnapshotUrl) {
snapshotRepositoryUrl = project.uri(nexusPublishSnapshotUrl)
}
username = nexusPublishUsername
password = nexusPublishPassword
stagingProfileId = nexusPublishStagingProfileId
}
username = nexusPublishUsername
password = nexusPublishPassword
stagingProfileId = nexusPublishStagingProfileId
}
}
}
}

project.afterEvaluate {
project.publishing {
if (mavenPublish) {
if (useMavenPublish) {
System.setProperty('org.gradle.internal.publish.checksums.insecure', true as String)
repositories {
maven {
Expand All @@ -167,8 +202,9 @@ The credentials and connection url must be specified as a project property or an
}

if (!mavenPublishUrl) {
// throw new RuntimeException('Could not locate a project property of `mavenPublishUrl` or an environment variable of `MAVEN_PUBLISH_URL`')
throw new RuntimeException('Could not locate a project property of `mavenPublishUrl` or an environment variable of `MAVEN_PUBLISH_URL`. A URL is required for maven publishing.')
jdaugherty marked this conversation as resolved.
Show resolved Hide resolved
}

url = mavenPublishUrl
}
}
Expand Down Expand Up @@ -296,13 +332,13 @@ The credentials and connection url must be specified as a project property or an
}
}
}
}

if(nexusPublish && isRelease) {
extensionContainer.configure(SigningExtension, {
it.required = isRelease
it.sign project.publishing.publications.maven
})
}
if (useNexusPublish) {
extensionContainer.configure(SigningExtension, {
it.required = isRelease
it.sign project.publishing.publications.maven
})
}

def installTask = taskContainer.findByName('install')
Expand Down Expand Up @@ -330,10 +366,10 @@ The credentials and connection url must be specified as a project property or an

protected Map<String, String> getDefaultExtraArtifact(Project project) {
String pluginXml = "${project.sourceSets.main.groovy.getClassesDirectory().get().getAsFile()}/META-INF/grails-plugin.xml".toString()
new File(pluginXml).exists()? [
source : pluginXml,
classifier: getDefaultClassifier(),
extension : 'xml'
new File(pluginXml).exists() ? [
source : pluginXml,
classifier: getDefaultClassifier(),
extension : 'xml'
] : null
}

Expand Down
Loading