Publish only subprojects that use Rpm plugin

I have a multi-module project with subprojects where only some of the subprojects use the Netflix packaging project to create RPMs. I now want to make sure only those subprojects are configured for publishing the RPMS to Artifactory.

I could add the necessary publishing section into each of the appropriate subprojects’ build.gradle files, but I’d rather apply DRY and configure this in the top-level build.gradle if possible.

I have tried the following…

plugins.withType(Rpm) {
  project.logger.info('my info message')
}

But it threw the following error:

FAILURE: Build failed with an exception.

* Where:
Build file 'C:\myapp\build.gradle' line: 98

* What went wrong:
A problem occurred evaluating root project 'foo'.
> Could not get unknown property 'Rpm' for project ':sub-bar' of type org.gradle.api.Project.

I’ve looked at other examples of this approach, but I’m a bit “at sea”, as I’m not sure where you get the “Type” name of a plugin!

For reference, the subprojects are configured to build RPMs as follows:

apply plugin: 'nebula.ospackage'
...
ospackage {
  ... some settings ...
}

buildRpm {
  ... some settings ...
}

and the publishing part (which works) I would paste into each relevant build.gradle is:

apply plugin: 'ivy-publish'
...
publishing {
    publications {
        rpm(IvyPublication) {
            artifact buildRpm.outputs.getFiles().getSingleFile()
            organisation 'dummy'
        }
    }
    repositories {
        ivy {
            url 'https://myrepo/artifactory/yum/'
            layout "pattern", {
                artifact "${buildRpm.outputs.getFiles().getSingleFile().getName()}"
            }
        }
    }
}

I worked it out eventually - the plugin simply needs to be referenced by its full name… “com.netflix.gradle.plugins.packaging.CommonPackagingPlugin”, so the solution is:

subprojects {
    apply plugin: 'ivy-publish'

    plugins.withType(com.netflix.gradle.plugins.packaging.CommonPackagingPlugin) {
        project.logger.info("$project.name uses the ospackaging plugin" )

        publishing {
            publications {
                rpm(IvyPublication) {
                    artifact buildRpm.outputs.getFiles().getSingleFile()
                    organisation 'none'
                }
            }
            repositories {
                ivy {
                    credentials {
                        username yumDeployUser
                        password yumDeployPassword
                    }
                    url yumRepo
                    layout "pattern", {
                        artifact "${buildRpm.outputs.getFiles().getSingleFile().getName()}"
                    }
                }
            }
        }

    }
}
2 Likes