如何在自定义 gradle 插件中创建 .jar 文件?
How to create a .jar file in a custom 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 plugin/task 来创建 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 项目。 我想创建 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 plugin/task 来创建 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}")
}
}
}
}
}