如何让用户更改Gradle插件使用的库的版本? [英] How to allow user to change the version of the library which is used by a plugin in Gradle?

查看:200
本文介绍了如何让用户更改Gradle插件使用的库的版本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的插件中,我调用静态引导方法 MutationCoverageReport.main (arg),它是我的插件的编译依赖项。我希望允许插件用户更改应该使用的库的版本(假设选择的版本具有兼容的API)。



我可以建议用户排除一个来自我的插件的传递依赖项,并在请求的版本中添加一个依赖项到build.gradle中的buildscript.dependencies.classpath,但这不是很优雅:

  buildscript {
(...)
依赖关系{
classpath('info.solidsoft.gradle.pitest:gradle-pitest-plugin:0.32 .0'){
exclude(group:'org.pitest')
}
classpath'org.pitest:pitest-command-line:0.33'
}
}

我希望能够在配置闭包中使用libVersion参数,但我有一些我试过两种解决方案的问题。



1。我可能需要从我的插件中删除传递依赖图书馆版本on和它的依赖关系),并在请求的版本中添加一个库(从配置关闭)作为构建脚本的依赖关系。



在afterEvaluate之后(配置已解决)



2。我可以成功挂钩beforeEvaluate(是否触发了单个模块项目?改变一个用于执行静态方法的类路径。

为此,我可以创建一个自定义类加载器来使用我的库类(在请求的版本中)在委托给父类加载器之前。我还必须使用新的类加载器用反射调用替换一个带有 MutationCoverageReport.main(arg)的班轮。



也许有一种简单的方法来修改类路径,通过一个插件来调用上述方法?



主要问题。什么是允许插件用户定义插件执行库的最佳方式?

关于使用单独的类路径执行Java类的另一个问题Peter Peter Niederwieser提供了3种方法。最后修改我的任务以扩展 JavaExec 。我设置了显式集执行类路径,它与插件执行类路径分开,并且可以包含不同的库版本。



缺点是只允许运行main方法,但它不是在我的情况下是一个问题。


In my plugin I call static bootstrapping method MutationCoverageReport.main(arg) from a library which is a compile dependency of my plugin. I would like to allow a plugin user to change the version of the library which should be used (assuming the selected version has compatible API).

I could advice user to exclude a transitive dependency from my plugin and add a dependency to the library in the requested version manually to buildscript.dependencies.classpath in his build.gradle, but this is not very elegant:

buildscript {
    (...)
    dependencies {
        classpath('info.solidsoft.gradle.pitest:gradle-pitest-plugin:0.32.0') {
            exclude(group: 'org.pitest')
        }
        classpath 'org.pitest:pitest-command-line:0.33'
    }
}

I would like to be able to use libVersion parameter in my configuration closure, but I have some problems with two solutions I tried.

1. I could need to remove transitive dependencies from my plugin (an original library version and its dependencies) and add a library in requested version (from configuration closure) as a buildscript dependency.

In afterEvaluate it is to late (configurations are resolved) and I had problem to successfully hook with beforeEvaluate (is it triggered for single module project?).

2. I could change a classpath which is used to execute a static method from a library.

To do that I could create a custom class loader which would use my library classes (in requested version) before delegating to the parent class loader. I also would have to replace one liner with MutationCoverageReport.main(arg) with the reflection call using the new class loader.

Maybe there is a simpler way to modify a classpath by a plugin with which the mentioned method will be called?

Main questions. What would be the best way to allow plugin users to define the version of the library the plugin executes?

解决方案

In another question about executing Java class with a separate classpath Peter Niederwieser provided 3 approaches. In the end modify my task to extend JavaExec. I set explicit set execution classpath which is separate from plugin execution classpath and can contain different library version.

The drawback is allows only to run main method, but it wasn't a problem in my case.

这篇关于如何让用户更改Gradle插件使用的库的版本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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