Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Maven Runner build config #2911

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
tidying up, tests working
  • Loading branch information
aSemy committed Mar 8, 2023
commit d3c4c7c67c9fa2515d5aa19db7c698d21e91c5fa
45 changes: 0 additions & 45 deletions buildSrc/src/main/kotlin/org/jetbrains/SetupMaven.kt

This file was deleted.

46 changes: 34 additions & 12 deletions runners/maven-plugin/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import org.gradle.kotlin.dsl.support.appendReproducibleNewLine
import org.jetbrains.CrossPlatformExec
import org.jetbrains.registerDokkaArtifactPublication

Expand All @@ -7,8 +8,6 @@ plugins {
org.jetbrains.conventions.`maven-cli-setup`
}

val pomTemplateFile = layout.projectDirectory.file("pom.tpl.xml")

dependencies {
implementation(project(":core"))
implementation("org.apache.maven:maven-core:${setupMavenProperties.mavenVersion}")
Expand All @@ -18,12 +17,17 @@ dependencies {
implementation(kotlin("stdlib-jdk8"))
}

val mavenPluginTaskGroup = "maven plugin"

val generatePom by tasks.registering(Sync::class) {
description = "Generate pom.xml for Maven Plugin Plugin"
group = mavenPluginTaskGroup

val dokka_version: String by project
inputs.property("dokka_version", dokka_version)

val pomTemplateFile = layout.projectDirectory.file("pom.tpl.xml")
aSemy marked this conversation as resolved.
Show resolved Hide resolved

from(pomTemplateFile) {
rename { it.replace(".tpl.xml", ".xml") }

Expand All @@ -37,31 +41,49 @@ val generatePom by tasks.registering(Sync::class) {
into(temporaryDir)
Copy link
Contributor Author

@aSemy aSemy Mar 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of worrying about Sync/Copy tasks overwriting, and having to use preserve {}, I think it's best to always use Sync into the task's temporary dir, and use a final Sync task (prepareMavenPluginBuildDir) to sync the files from the other Sync tasks in one go.

}

val prepareCompiledClasses by tasks.registering(Sync::class) {
description = "Prepare compiled classes for Maven Plugin task execution"
val prepareMavenPluginBuildDir by tasks.registering(Sync::class) {
description = "Prepares all files for Maven Plugin task execution"
group = mavenPluginTaskGroup

from(tasks.compileKotlin.flatMap { it.destinationDirectory })
from(tasks.compileJava.flatMap { it.destinationDirectory })
into(temporaryDir)
}
from(tasks.compileKotlin.flatMap { it.destinationDirectory }) { into("classes/java/main") }
from(tasks.compileJava.flatMap { it.destinationDirectory }) { into("classes/java/main") }

val prepareMavenPluginBuildDir by tasks.registering(Sync::class) {
description = "Prepare files for Maven Plugin task execution"
from(prepareCompiledClasses) { into("classes/java/main") }
from(generatePom)

into(setupMavenProperties.mavenBuildDir)
}

val helpMojo by tasks.registering(CrossPlatformExec::class) {
group = mavenPluginTaskGroup

dependsOn(tasks.installMavenBinary, prepareMavenPluginBuildDir)
workingDir(setupMavenProperties.mavenBuildDir)
executable(setupMavenProperties.mvn.get())
args("-e", "-B", "org.apache.maven.plugins:maven-plugin-plugin:helpmojo")

outputs.dir(setupMavenProperties.mavenBuildDir)

doLast("normalize maven-plugin-help.properties") {
// The maven-plugin-help.properties file contains a timestamp by default.
// It should be removed as it is not reproducible and impacts Gradle caching
val pluginHelpProperties = workingDir.resolve("maven-plugin-help.properties")
pluginHelpProperties.writeText(
buildString {
val lines = pluginHelpProperties.readText().lines().iterator()
// the first line is a descriptive comment
appendReproducibleNewLine(lines.next())
// the second line is the timestamp, which should be ignored
lines.next()
// the remaining lines are properties
lines.forEach { appendReproducibleNewLine(it) }
}
)
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately the generated maven-plugin-help.properties contains a timestamp, so Gradle will always consider the output out-of-date.

I wrote this code to fix it - but maybe there's a Maven flag that will disable the timestamp?

This is perhaps a bit OTT/pre-optimising.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually don't understand what helpMojo is supposed to achieve, and I can't google anything sensible. It looks like it should either generate the help goal (which Dokka's Maven plugin doesn't have) or maybe add some help documentation? Were you able to figure it out?

Nothing looks broken to me, so I think it's fine to ignore the timestamp. Perhaps, at some point our Maven plugin should be revisited as well 😅

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it auto generates a 'help' command based on the properties in the Maven plugin

The file it generates is in runners/maven-plugin/build/maven/generated-sources

But, like you, I'm not sure! I've never developed a Maven plugin.

package org.jetbrains.dokka.maven;

/**
 * Display help information on dokka-maven-plugin.<br>
 * Call <code>mvn dokka:help -Ddetail=true -Dgoal=&lt;goal-name&gt;</code> to display parameter details.
 * @author maven-plugin-tools
 * @goal help
 * @requiresProject false
 * @threadSafe
 */
public class HelpMojo
    extends AbstractMojo
{
    /**
     * If <code>true</code>, display all settable properties for each goal.
     *
     * @parameter property="detail" default-value="false"
     */
    private boolean detail;

    /**
     * The name of the goal for which to show help. If unspecified, all goals will be displayed.
     *
     * @parameter property="goal"
     */
    private java.lang.String goal;

    /**
     * The maximum length of a display line, should be positive.
     *
     * @parameter property="lineLength" default-value="80"
     */
    private int lineLength;

...

I was looking at other options, and I found this Gradle plugin: https://github.com/britter/maven-plugin-development. It might help with developing a Maven plugin, but on the other hand, the current approach seems to be working.

}

val pluginDescriptor by tasks.registering(CrossPlatformExec::class) {
group = mavenPluginTaskGroup

dependsOn(tasks.installMavenBinary, prepareMavenPluginBuildDir)
workingDir(setupMavenProperties.mavenBuildDir)
executable(setupMavenProperties.mvn.get())
Expand All @@ -73,7 +95,7 @@ val pluginDescriptor by tasks.registering(CrossPlatformExec::class) {
tasks.jar {
dependsOn(pluginDescriptor, helpMojo)
metaInf {
from("${setupMavenProperties.mavenBuildDir}/classes/java/main/META-INF")
from(setupMavenProperties.mavenBuildDir.map { it.dir("classes/java/main/META-INF") })
}
manifest {
attributes("Class-Path" to configurations.runtimeClasspath.map { configuration ->
Expand Down