Custom Gradle Plugin Transitive Dependencies

From this Forum Post I see that all the transitive dependencies of the plugin are added to the classpath of the of the build where it is applied. I am right now running into a issue where my plugin’s transitive dependencies are not applied to my build where I am using it.
In my case my custom plugin is a standalone plugin and its published to local maven repository and I am using published plugin in another build.

  • plugin build.gradle
buildscript {
	repositories {
		ivy {
			url 'https://bndtools.ci.cloudbees.com/job/bnd.master/lastSuccessfulBuild/artifact/dist/bundles'
			layout 'pattern', { artifact '[module]/[artifact]-[revision].[ext]' /* OSGi repo pattern */ }
		}
		mavenCentral()
	}
	dependencies {
		classpath group:'biz.aQute.bnd', name: 'biz.aQute.bnd', version: '3.0.0'
		classpath group: 'biz.aQute.bnd', name: 'biz.aQute.bnd.gradle', version: '3.0.0'
	}
}

apply plugin: 'groovy'
apply plugin: 'maven-publish'

sourceCompatibility = 1.7
targetCompatibility = 1.7
version = '1.0-SNAPSHOT'

repositories {
	ivy {
		url 'https://bndtools.ci.cloudbees.com/job/bnd.master/lastSuccessfulBuild/artifact/dist/bundles'
		layout 'pattern', { artifact '[module]/[artifact]-[revision].[ext]' /* OSGi repo pattern */ }
	}
	mavenCentral()
	jcenter()
}


dependencies {
	compile gradleApi()
	compile localGroovy()
	compile 'biz.aQute.bnd:biz.aQute.bnd:3.0.0'
	compile 'biz.aQute.bnd:biz.aQute.bnd.gradle:3.0.0'
	compile 'commons-io:commons-io:2.4'
	compile 'org.freemarker:freemarker:2.3.22'
	testCompile group: 'junit', name: 'junit', version: '4.+'
}

publishing {
	publications {
		maven(MavenPublication) {
			groupId 'org.workspace7.gradle'
			artifactId 'osgidoc-plugin'
			version "${version}"
			artifact jar
		}
	}
}

– plugin consumer build.gradle

buildscript {
	repositories {
		ivy {
			url 'https://bndtools.ci.cloudbees.com/job/bnd.master/lastSuccessfulBuild/artifact/dist/bundles'
			layout 'pattern', { artifact '[module]/[artifact]-[revision].[ext]' /* OSGi repo pattern */ }
		}
		mavenLocal()
		mavenCentral()
	}
	dependencies {
		classpath 'biz.aQute.bnd:biz.aQute.bnd.gradle:3.0.0'
		classpath group:'org.workspace7.gradle',name:'osgidoc-plugin',version:'1.0-SNAPSHOT'
	}
}

apply plugin: 'org.workspace7.gradle.plugins.osgidoc'

sourceCompatibility = 1.7

repositories { mavenCentral() }

dependencies {
	compile 'org.osgi:org.osgi.core:6.0.0'
	compile 'org.osgi:org.osgi.compendium:5.0.0'
	compile 'biz.aQute.bnd:biz.aQute.bnd.annotation:2.4.0'
	compile 'org.slf4j:slf4j-api:1.7.7'
}

task wrapper(type:Wrapper){ gradleVersion = '2.4' }

Exception with --stacktrace option has this Caused by: java.lang.ClassNotFoundException: freemarker.template.Template - which I suppose the plugins dependencies are not added to classpath. An observation noted that the generated pom.xml does not have any dependencies section too .

Any pointers on why its not working is much appreciated.

-Kamesh

1 Like

Without the FreeMarker dependency (and other dependencies of your plugin) being listed in the <dependencies> section of the generated pom.xml, the consuming Gradle build isn’t aware that your plugin has any transitive dependencies when resolving from the maven repository.

This points to an issue with the configuration of the MavenPublication. By default, the MavenPublication will publish the artifacts from public configurations. Take a look at the Publications section in the Maven Publication chapter of the user guide. The table shows the software components that you can publish and then an example of the configuration needed to publish a java software component which includes both artifacts and dependencies.

Updating the configuration for the MavenPublication to include the line from components.java should cause your dependencies to show up in the generated pom.xml and your consuming build to properly resolve your plugin’s transitive dependencies.

publishing {
    publications {
        maven(MavenPublication) {
            groupId 'org.workspace7.gradle'
            artifactId 'osgidoc-plugin'
            version "${version}"
            artifact jar
            from components.java // ADD THIS LINE
        }
    }
}

Cool thanks Justinic. adding from components.java made the difference