Gradle编译不包含在Jar中的依赖关系 [英] Gradle compile dependencies not included in Jar

查看:998
本文介绍了Gradle编译不包含在Jar中的依赖关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 我有一个jar,build-plugins.jar和一个gradle插件, > apply plugin'java'
dependencies {
编译gradleApi()
编译localGroovy()
编译('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 ')
}

这会构建build-plugins.jar。并且使用该插件的项目通过文件引用插件jar

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


$ / code>

问题是当我运行第二个项目的任何任务时,我得到了类代理无法为类xyz创建,其根本原因是四个依赖项(joda-time,commons-io,stringtemplate,maven-soapui-plugin)不在那里。如果我添加依赖到插件消耗项目,那么它工作得很好:

  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'
}

我的问题是为什么插件项目中的消耗插件的项目的buildscript的类路径。

解决方案

Jars通常不包含它们的依赖关系。相反,它们会与某种描述工件依赖关系的元数据描述符(pom.xml或ivy.xml)一起发布到存储库中。当你直接将jar文件作为依赖项引用时,Gradle无法知道它的传递依赖关系是什么。您有几种方法可以解决这个问题:


  1. 将插件jar发布到一个存储库以及必要的元数据(Gradle将会为你做),并将其作为 a>

  2. 使用客户端模块依赖项 使用类似Gradle fatjar shadow 插件将依赖项捆绑到您的jar中。


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.

解决方案

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.

这篇关于Gradle编译不包含在Jar中的依赖关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆