All Downloads are FREE. Search and download functionalities are using the official Maven repository.

love.forte.gradle.common.publication.configure.Publications.kt Maven / Gradle / Ivy

package love.forte.gradle.common.publication.configure

import love.forte.gradle.common.core.Gpg
import love.forte.gradle.common.core.project.ProjectDetail
import love.forte.gradle.common.core.property.of
import love.forte.gradle.common.core.property.ofIf
import love.forte.gradle.common.core.repository.Repository
import org.gradle.api.Action
import org.gradle.api.Project
import org.gradle.api.artifacts.dsl.RepositoryHandler
import org.gradle.api.publish.PublishingExtension
import org.gradle.api.publish.maven.MavenPom
import org.gradle.api.publish.maven.MavenPublication
import org.gradle.plugins.signing.SigningExtension


val Project.publishingExtension: PublishingExtension
    get() =
        (this as org.gradle.api.plugins.ExtensionAware).extensions.getByName("publishing") as PublishingExtension

fun Project.publishingExtension(configure: Action): Unit =
    (this as org.gradle.api.plugins.ExtensionAware).extensions.configure("publishing", configure)


/**
 * Retrieves the [signing][org.gradle.plugins.signing.SigningExtension] extension.
 */
val Project.signingExtension: SigningExtension
    get() =
        (this as org.gradle.api.plugins.ExtensionAware).extensions.getByName("signing") as SigningExtension

/**
 * Configures the [signing][org.gradle.plugins.signing.SigningExtension] extension.
 */
fun Project.signingExtension(configure: Action): Unit =
    (this as org.gradle.api.plugins.ExtensionAware).extensions.configure("signing", configure)


fun PublishingExtension.commonConfigPublishingRepositories(config: MavenCommonPublishingConfigExtensions) {
    repositories {
        config.repositoriesConfig(it)
    }
}

fun Project.commonPublicationSignConfig(config: MavenCommonPublishingConfigExtensions) {
    signingExtension {
        val gpg = config.gpg ?: return@signingExtension
        if (!config.signConfig(it)) return@signingExtension
        
        val (keyId, secretKey, password) = gpg
        it.useInMemoryPgpKeys(keyId, secretKey, password)
        it.sign(publishingExtension.publications)
    }
}

fun MavenPublication.commonConfigMavenPublication(project: Project, config: MavenCommonPublishingConfigExtensions) {
    config.artifacts.forEach {
        artifact(it)
    }
    
    val configProject = config.project
    
    //groupId = configProject.group
    //artifactId = project.name
    //version = configProject.version.toString()
    
    setupPom(project.name, configProject)
}

fun MavenPublication.setupPom(name: String, project: ProjectDetail) {
    pom {
        it.name of name
        it.setup(project)
    }
}

fun MavenPom.setup(project: ProjectDetail) {
    description of project.description
    url of project.homepage
    licenses {
        project.licenses.forEach { license ->
            it.license {
                it.name ofIf license.name
                it.url ofIf license.url
                it.distribution ofIf license.distribution
                it.comments ofIf license.comments
            }
        }
    }
    project.scm.also { scm ->
        scm {
            it.url ofIf scm.url
            it.connection ofIf scm.connection
            it.developerConnection ofIf scm.developerConnection
            it.tag ofIf scm.tag
            
        }
    }
    developers {
        project.developers.forEach { developer ->
            it.developer {
                it.id ofIf developer.id
                it.name ofIf developer.name
                it.email ofIf developer.email
                it.url ofIf developer.url
                it.organization ofIf developer.organization
                it.organizationUrl ofIf developer.organizationUrl
                it.timezone ofIf developer.timezone
                it.roles.addAll(developer.roles)
                it.properties.putAll(developer.properties)
            }
        }
    }
}

open class MavenCommonPublishingConfigExtensions {
    var isSnapshot = false
    var snapshotRepository: Repository? = null
    var releasesRepository: Repository? = null
    
    var repositoriesConfig: RepositoryHandler.() -> Unit = {
        mavenLocal()
        if (isSnapshot) {
            snapshotRepository?.also(::configPublishMaven)
        } else {
            releasesRepository?.also(::configPublishMaven)
        }
    }
    
    /**
     * 例如 javadoc jar task
     *
     */
    var artifacts: List = emptyList()
    
    fun artifact(source: Any) {
        artifacts = artifacts + source
    }
    
    lateinit var project: ProjectDetail
    
    var gpg: Gpg? = Gpg.ofSystemPropOrNull()
    
    var signConfig: SigningExtension.() -> Boolean = { true }
}

fun RepositoryHandler.configPublishMaven(repository: Repository) {
    repository.apply {
        configMaven()
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy