从Gradle插件使用单独的类路径执行Java类 [英] Execute Java class with separate classpath from Gradle plugin

查看:308
本文介绍了从Gradle插件使用单独的类路径执行Java类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的插件中,我需要从外部JAR执行静态自举方法来执行一个分析.目前,我直接使用MutationCoverageReport.main(arg)从代码中调用它,但是这迫使我在插件中创建了一个编译时依赖项,而该依赖项很难在执行插件时进行修改(选择其他JAR版本).

In my plugin I need to execute static bootstrapping method from an external JAR to perform an analysis. Currently I call it directly from code with MutationCoverageReport.main(arg), but this forces me to create a compile time dependency in my plugin which is hard to modify (choose a different JAR version) when executing the plugin.

我想使用反射调用它,并在Gradle代码中找到了一些示例,例如WorkerProcessBuilderJavaExecHandleBuilder.不过,它们在org.gradle.process.internal软件包中,我不确定是否建议从外部插件中使用它.

I would like to call it using reflection and found some examples in Gradle code like WorkerProcessBuilder or JavaExecHandleBuilder. Nevertheless they are in org.gradle.process.internal package and I'm not sure if it is recommended to use it from the external plugin.

问题. Gradle插件从具有指定类路径的外部类运行任意Java方法的推荐方法是什么?

Question. What is the recommended way for Gradle plugin to run arbitrary Java method from external class with specified classpath?

顺便说一句,我宁愿不要使用Ant任务,因为它是一个单独的项目,并且具有潜在的错误和局限性,是另一个抽象层.

Btw, I would prefer to not use an Ant task as it is a separate project and another layer of abstraction with its potential bugs and limitations.

推荐答案

您可以采用两种方法来调用外部Jar中的main方法:

There are a couple of approaches that you could take to invoke a main method in an external Jar:

  1. 使用JavaExec任务,或使用project.javaexec方法的自定义任务.
  2. 将外部Jar放在构建脚本类路径上,并直接或从自定义任务中反射性地调用其main方法.外部Jar可能是插件Jar的传递依赖项.另外,也可以让用户将外部Jar放在构建脚本类路径(buildscript { dependencies { classpath ... } })上.
  3. 编写一个自定义任务,该任务将创建自己的类加载器,将外部Jar放在其上,并以反射方式调用其main方法.
  1. Use a JavaExec task, or a custom task that leverages the project.javaexec method.
  2. Put the external Jar on the build script class path and invoke its main method directly or reflectively from a custom task. The external Jar could be a transitive dependency of the plugin Jar. Alternatively, it could be left to the user to put the external Jar on the build script class path (buildscript { dependencies { classpath ... } }).
  3. Write a custom task that creates its own class loader, puts the external Jar on it, and calls its main method reflectively.

第一种方法通常是在单独的JVM中运行代码.第二种方法和第三种方法也可以调用除主要方法以外的方法.在第一种或第三种方法中,插件可以添加配置,该配置允许用户通过声明dependencies块中的依赖项来提供外部Jar. (该插件可以提供默认的依赖项.)这是许多内置Gradle插件所做的,例如代码质量插件(checkstyle等).您可能想研究他们的代码以获得一些启发.

The first approach will run the code in a separate JVM, which is often desirable. The second and third approaches can also invoke methods other than main methods. In the first or third approach, the plugin could add a configuration which allows the user to supply the external Jar by declaring a dependency in the dependencies block. (The plugin could provide a default dependency.) This is what many built-in Gradle plugins do, for example the code quality plugins (checkstyle etc.). You may want to study their code to get some inspiration.

外部插件只能使用Gradle公共API中的类,即 Javadoc Groovydoc . (并非所有内部类的软件包名称中都有internal.)

External plugins should only use classes from Gradle's public API, that is classes that are documented in the Javadoc or Groovydoc. (Not all internal classes have internal in the package name.)

这篇关于从Gradle插件使用单独的类路径执行Java类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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