Gradle maven-publish plugin: specify POM properties for all publications (including subprojects)

121 views Asked by At

I have a gradle project containing multiple subprojects. The subprojects use the maven-publish plugin to publish one or multiple maven artifacts. I want to add license and company (and maybe other) information to the POMs of the artifacts.

I found how to do this, like this:

publishing {
    publications {
        mySubprojectsPublication(MavenPublication) {
            artifact subprojectsPackageTask
    
            pom {
                licenses {
                    license { 
                        name = '...'
                        url = '...'
                        distribution = 'repo' 
                        comments = '...' 
                    }
                }
                organization { 
                    name = 'My Organization' 
                    url = 'https://www.MyOrgUrl.com' 
                }
            }
        }
    }

With multiple subprojects (and potentially mutliple publications per subproject), it seems to be suboptimal and hard to maintain to copy&paste the pom { ... } section to each of the publications across multiple build.gradle files.

QUESTION: Is there a way to specify this for all publications in the project? (All publications share the same license type)

1

There are 1 answers

0
Marco Freudenberger On BEST ANSWER

I found a solution myself...

I add the following to the root build.gradle:

allprojects {
    // add license to each (=.all) publications in all subprojects.
    // "model" block is required to access the publications AFTER they have been configured.
    model {
        publishing {
            publications.all { publication -> pom {
                    licenses {
                        license { 
                            name = '...'
                            url = '...'
                            distribution = 'repo' 
                            comments = '...' 
                        }
                    }
                    organization { 
                        name = 'My Organization' 
                        url = 'https://www.MyOrgUrl.com' 
                    }
                }
            }
        }
    }
}

If the root build.gradle doesn't already use the maven-publish plugin itself, you need to add it as well:

plugins {
    id 'maven-publish'
}

The model {} block is required, so that the pom info is added to all configured publications of the project after they have been configured.