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

Conversation

aSemy
Copy link
Contributor

@aSemy aSemy commented Mar 8, 2023

Part of #2703

Updates the Maven runner build logic to work better with the Gradle API

  • Removes the explicit cross-project dependency that was used to download the Maven exe twice
  • Creates a convention plugin for downloading the Maven exe
  • Removes eager downloading of the Maven exe
  • Implements normalization (removes a timestamp) of a Maven property file
  • Some minor build config tweaks to be a little more succint/Gradley/use newer functionality.

import java.io.File

@Suppress("LeakingThis")
open class SetupMaven : Sync() {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The properties in this file were moved to an extension in the new maven-cli-setup.gradle.kts convention plugin

}

init {
from(mavenBinaryConfiguration.map { file -> project.zipTree(file) })
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This line caused eager configuration, so the binary was always downloaded whether it was necessary or not.

Comment on lines -19 to -20
val setupMavenTask = project(":runners:maven-plugin").tasks.withType<SetupMaven>().single()
dependsOn(setupMavenTask)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Instead of using a cross-project dependency to re-use the Maven exec, just download it again.

Gradle will cache the downloaded dependency, so it's fine to set it up twice in different subprojects.

}

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.

Comment on lines 66 to 81
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.

Comment on lines 34 to 38
expand(
"mavenVersion" to setupMavenProperties.mavenVersion,
"dokka_version" to dokka_version,
"mavenPluginToolsVersion" to setupMavenProperties.mavenPluginToolsVersion,
)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

using the Gradle expand () is a little nicer to use and look at than the previous manual find and replace.

To achieve this, I needed to update the properties in pom.tpl.xml to be of the format ${mavenVersion}

@IgnatBeresnev IgnatBeresnev self-requested a review March 9, 2023 11:40
Copy link
Member

@IgnatBeresnev IgnatBeresnev left a comment

Choose a reason for hiding this comment

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

Looks very neat, and thank you for the explanation comments! Only have optional nitpicks

I'll start the integration tests and, if everything's good, will merge it

runners/maven-plugin/build.gradle.kts Outdated Show resolved Hide resolved
integration-tests/maven/build.gradle.kts Outdated Show resolved Hide resolved
Comment on lines 66 to 81
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
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 😅

Comment on lines 14 to 15
val mavenVersion = "3.5.0"
val mavenPluginToolsVersion = "3.5.2"
Copy link
Member

Choose a reason for hiding this comment

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

Very much optional, while we're at it: I think we actually forget to check/update the Maven version because it's buried in a place we don't visit that much. Extracting it to gradle.properties (or into the toml in the other PR) would not hurt

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point, I think so too. For now I'll put it in gradle.properties, and I think that later they can be moved to libs.versions.toml as part of #2884

Copy link
Contributor Author

@aSemy aSemy Mar 9, 2023

Choose a reason for hiding this comment

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

It was a little more work than I thought to refactor this, but I think it was worth it.

It revealed that the CrossPlatformExec task wasn't really needed, and could be replaced with a Provider that would determine the current operating system and use mvn.cmd on Windows, else use mvn.

Hopefully the GitHub Workflow tests pass 🤞

Copy link
Member

Choose a reason for hiding this comment

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

Oof, sorry for asking you to do that then, and thanks for doing! I do think it was worth it though!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

no apology needed :)

@aSemy aSemy force-pushed the feat/refactor_maven_runner_build_config branch from 120cb21 to baefed3 Compare March 9, 2023 18:59
@IgnatBeresnev
Copy link
Member

After master was merged in, the maven-cli convention was needed to be moved into build-logic. I hope you don't mind I pushed it into your branch

I'll start integration tests once again, will merge it once they pass 👍 👍

@IgnatBeresnev IgnatBeresnev merged commit 12e2a3c into Kotlin:master Mar 16, 2023
29 checks passed
@aSemy
Copy link
Contributor Author

aSemy commented Mar 16, 2023

After master was merged in, the maven-cli convention was needed to be moved into build-logic. I hope you don't mind I pushed it into your branch

Not at all! Good job noticing it, and thanks for fixing it.

@aSemy aSemy deleted the feat/refactor_maven_runner_build_config branch March 16, 2023 11:08
@aSemy aSemy mentioned this pull request Jun 6, 2023
@aSemy aSemy mentioned this pull request Jul 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants