gradle - 如何在自定义gradle插件中创建.jar文件?

标签 gradle kotlin jar gradle-plugin

所以目前,我正在尝试创建一个自定义的gradle项目。
我想创建jar(这是一个kotlin项目),然后获取对该jar以及该jar的所有依赖项的引用。

我已经在一个自定义的maven插件中完成了此操作,它看起来像这样:

@Mojo(name = "custom-plugin", defaultPhase = LifecyclePhase.PROCESS_RESOURCES, requiresDependencyCollection = ResolutionScope.COMPILE_PLUS_RUNTIME, requiresDependencyResolution = ResolutionScope.COMPILE_PLUS_RUNTIME)
class CustomMavenMojo : AbstractMojo() {
    @Parameter(defaultValue = "\${project}", readonly = true, required = true)
    private val mavenProject: MavenProject? = null

    @Throws(MojoExecutionException::class, MojoFailureException::class)
    override fun execute() {
        val targetFolder = File(mavenProject.model.build.directory) // the build folder
        val jarFile = mavenProject.artifact.file // This is the build jar file
        val dependencies = mavenProject.artifacts // The jar's dependencies
    }
}

因此,我可以理解,在gradle中,我必须对此有所不同。
我想我必须调用jar插件/任务来创建jar,多数民众赞成在它开始变得有点模糊的地方。我看到一个可以调用插件,但是哪个是正确的插件,然后我将如何访问创建的jar?另外,我很想了解依赖项。

这是到目前为止我得到的:
class CustomGradlePlugin : Plugin<Project> {
    override fun apply(project: Project) {
        project.task("custom-jar-task") {
            project.plugins.apply('???') // which plugin do i call here to create the .jar file?
            val createdJar = ... // and how do i get the created jar file?

            project.configurations.forEach { configuration ->
                configuration.dependencies.forEach { dependency ->
                    val dependencyJarFile = ... // how would i get the depencenies jar file here?

                }
            }
        }
    }
}

最佳答案

据我所知,我已经基本解决了。

我发现一个人可以依赖于另一项任务,在这种情况下,我依赖于jar任务-这种jar任务之前已经执行过。之后,我只需要通过jar任务获取创建的jar,然后通过运行时配置获取依赖项。

这里有些事情要注意。第一个可能会在自定义插件中应用java-plugin或org.jetbrains.kotlin.jvm插件,我想这些会影响创建的jar吗?另外,我不太确定jar任务的配置将如何影响这一点,但是我猜想它们只是在之前应用过的,无论如何也要获取完成的jar。
同样很明显,这将针对jar任务创建的jar,针对其他jar(自定义fatJar或类似的东西),则必须相应地更改任务。

class CustomGradlePlugin : Plugin<Project> {
    override fun apply(project: Project) {
        project.task("custom-jar-task") {
            // First depend on jar task to make sure the .jar file is build
            it.dependsOn(
                project.tasks.getByName("jar")
            )

            it.doLast {
                // Get the jar task
                val jarTask = project.tasks.getByName("jar") as Jar
                // Get the .jar file through the jar task
                val jarFile = jarTask.archiveFile.orNull

                println("Build Jar: ${jarFile?.asFile?.name}")

                println("Runtime dependencies:")
                // Get the runtime configuration resolve it and iterate over all dependencies.
                project.configurations.getByName("runtime").resolve().forEach {
                    println("-- ${it.name}")
                }
            }
        }
    }
}

关于gradle - 如何在自定义gradle插件中创建.jar文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59426645/

相关文章:

java - 如何在 Torque Clusterr 上查看 .jar 作业的进度

java - Gradle 从不同的模块运行 Spock 集成测试

Android Studio 3 - 覆盖 Gradle 版本

java - Spring Boot 显示对索引的 http 请求,但不显示任何其他映射

kotlin - 在 Kotlin 中锁定 serialVersionUID

android - 在 kotlin android 中显示 Unresolved 引用

unit-testing - 如何配置 pom 以运行打包在 jar 中的测试?

android - 构建新项目时出错 - aapt 已完成,退出值非零 1

android - 如何测试 Kotlin Coroutine actors

scala - 将.jar添加到classpath(Scala)