Gradle compile dependencies not included in Jar

2019-07-20 12:17发布

问题:

I have a jar, build-plugins.jar with a gradle plugin that is build with this in build.gradle:

apply plugin 'java'
    dependencies {
    compile gradleApi()
       compile localGroovy()
       compile('eviware:maven-soapui-plugin:4.5.1')
       compile('org.antlr:stringtemplate:4.0.2')
       compile('commons-io:commons-io:2.4')
       compile('joda-time:joda-time:2.1')
    }

This builds build-plugins.jar. And the project that consumes the plugin references the plugin jar by file

apply plugin 'thepluginwahoo'
buildscript {
dependencies {
        classpath 'org.jfrog.buildinfo:build-info-extractor-gradle:2.2.1'
        classpath files('/path/to/build-plugins.jar')
    }
}

The problem is when I run any task of the second project, I get "class proxy could not be created for class xyz" with the root cause being that the four dependencies (joda-time, commons-io, stringtemplate, maven-soapui-plugin) are not there. If I add the dependencies to the plugin-consuming project then it works just fine:

apply plugin 'thepluginwahoo'
buildscript {
    dependencies {
        classpath 'org.jfrog.buildinfo:build-info-extractor-gradle:2.2.1'
        classpath files('/path/to/build-plugins.jar')
        classpath 'eviware:maven-soapui-plugin:4.5.1'
        classpath 'org.antlr:stringtemplate:4.0.2'
        classpath 'joda-time:joda-time:2.1'
        classpath 'commons-io:commons-io:2.4'
    }

}

My question is why don't the classes of the "compile" dependencies in the plugin project appear in the plugin-consuming project when the jar is included in the classpath of the buildscript of the plugin-consuming project.

回答1:

Jars typically do not contain their dependencies. Instead, they are published to a repository along with some kind of metadata descriptor (pom.xml or ivy.xml) which describes the artifact's dependencies. When you refer to the jar file directly as a dependency, Gradle has no way of knowing what its transitive dependencies are. You have a couple of ways to deal with this:

  1. Publish your plugin jar to a repository, along with the necessary metadata (which Gradle will do for you) and bring it in as an external module dependency
  2. Explicitly declare the plugin's transitive dependencies using a client module dependency.
  3. Use something like the Gradle fatjar or shadow plugins to bundle dependencies within your jar.