Cannot apply plugin by id in init.gradle script

Example

//Snippet
settingsEvaluated { settings ->
    settings.pluginManagement {
        repositories {
            maven {
                credentials {
                    username "username"
                    password "password"
                }
                url "https://nexus.example.com"
                
            }
        }
    }
}
initscript {
    repositories {
        maven {
            credentials {
                username "username"
                password "password"
            }
            url "https://nexus.example.com"
            
        }
    }
    dependencies {
        classpath "com.netflix.nebula:gradle-ospackage-plugin:5.1.0"
        classpath "com.github.jengelman.gradle.plugins:shadow:4.0.4"
        classpath "gradle.plugin.com.selesse:gradle-git-changelog:0.3.0"
    }
}
allprojects {
    apply plugin: 'com.netflix.gradle.plugins.rpm.RpmPlugin'
    apply plugin: 'com.github.jengelman.gradle.plugins.shadow.ShadowPlugin'
    apply plugin: 'com.selesse.gradle.git.changelog.GitLogPlugin'
}
//...

Am I doing something wrong here.
Stacktrace is not very helpfull either.

Error Message

FAILURE: Build failed with an exception.

* Where:
Initialization script '/home/$USER/.gradle/init.gradle' line: 37

* What went wrong:
Plugin with id 'com.netflix.gradle.plugins.rpm.RpmPlugin' not found.

* Try:
Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Exception is:
org.gradle.api.plugins.UnknownPluginException: Plugin with id 'com.netflix.gradle.plugins.rpm.RpmPlugin' not found.
        at org.gradle.api.internal.plugins.DefaultPluginManager.apply(DefaultPluginManager.java:131)
        at org.gradle.api.internal.plugins.DefaultObjectConfigurationAction.applyType(DefaultObjectConfigurationAction.java:120)

You are using the implementation class of the plugins (ex: com.netflix.gradle.plugins.rpm.RpmPlugin) as the ID. The ID of that particular plugin is nebula.rpm.

However, you do need to use the implementation class, not the ID, in an init script. In order to do that, you should remove the quotes around the implementation classes:

allprojects {
    apply plugin: com.netflix.gradle.plugins.rpm.RpmPlugin
    apply plugin: com.github.jengelman.gradle.plugins.shadow.ShadowPlugin
    apply plugin: com.selesse.gradle.git.changelog.GitLogPlugin
}
1 Like

Got it working thanks!

Including

allprojects {
apply plugin: com.netflix.gradle.plugins.rpm.RpmPlugin
}

in init.gradle produces this error

Could not get unknown property ‘com’ for root project ‘sftp-client’ of type org.gradle.api.Project.

in gradle 6.0.1

If you try to reference a class in an init script without adding it to the classpath, that error will occur in any version of Gradle. The line you copied from the thread isn’t stand-alone. It’s within the context of the larger file posted by the original poster.

I too am getting the error “…unknown property ‘com’”

I have a project that creates a custom wrapper which includes a script in init.d named artifactory.gradle

initscript{
	Properties properties = new Properties()
	properties.load(new File(getGradleUserHomeDir(), 'gradle.properties').newDataInputStream())

	repositories {
		maven {
			url properties.artifactory_contextUrl + '/gradle-plugins-virtual'
			credentials {
				username = properties.artifactory_user
				password = properties.artifactory_password
			}
		}
	}
	dependencies {
		classpath "org.jfrog.buildinfo:build-info-extractor-gradle:4.17.2"
	}
}

apply plugin: com.jfrog.artifactory  //unknown property com
// apply plugin: 'com.jfrog.artifactory'  //gives plugin not found error

println "artifactory.gradle - called"

when I run a project configured to use this wrapper I get the errors indicated here.

Any help appreciated. How do I verify my classpath?
[Build Scan® | Gradle Cloud Services](https://build scan)

com.jfrog.artifactory does not look like a class name!