-
Notifications
You must be signed in to change notification settings - Fork 413
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update Maven tasks config to improve caching (#3480)
- implement custom MvnExec task, so it's easier to define inputs/outputs and path sensitivity, rather than having a load of Exec tasks with `outputs.cacheIf { true }`. - Manually remove timestamps from generated properties files. - Refactor Maven tasks to use new MvnExec task. - Add constant `project.build.outputTimestamp` to POM
- Loading branch information
Showing
4 changed files
with
155 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
build-logic/src/main/kotlin/dokkabuild/tasks/MvnExec.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
/* | ||
* Copyright 2014-2024 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
package dokkabuild.tasks | ||
|
||
import org.gradle.api.DefaultTask | ||
import org.gradle.api.file.ConfigurableFileCollection | ||
import org.gradle.api.file.DirectoryProperty | ||
import org.gradle.api.file.FileSystemOperations | ||
import org.gradle.api.file.RegularFileProperty | ||
import org.gradle.api.provider.ListProperty | ||
import org.gradle.api.provider.Property | ||
import org.gradle.api.tasks.* | ||
import org.gradle.api.tasks.Optional | ||
import org.gradle.api.tasks.PathSensitivity.NONE | ||
import org.gradle.api.tasks.PathSensitivity.RELATIVE | ||
import org.gradle.process.ExecOperations | ||
import org.gradle.work.NormalizeLineEndings | ||
import java.util.* | ||
import javax.inject.Inject | ||
|
||
/** | ||
* Runs a Maven task. | ||
* | ||
* See `dokkabuild.setup-maven-cli.gradle.kts` for details on the Maven CLI installation. | ||
*/ | ||
@CacheableTask | ||
abstract class MvnExec | ||
@Inject | ||
constructor( | ||
private val exec: ExecOperations, | ||
private val fs: FileSystemOperations, | ||
) : DefaultTask() { | ||
|
||
/** | ||
* Work directory. | ||
* | ||
* Be aware that any existing content will be replaced by [inputFiles]. | ||
*/ | ||
@get:OutputDirectory | ||
abstract val workDirectory: DirectoryProperty | ||
|
||
/** Input files - will be synced to [workDirectory]. */ | ||
@get:InputFiles | ||
@get:PathSensitive(RELATIVE) | ||
@get:NormalizeLineEndings | ||
abstract val inputFiles: ConfigurableFileCollection | ||
|
||
@get:InputFile | ||
@get:PathSensitive(NONE) | ||
abstract val mvnCli: RegularFileProperty | ||
|
||
@get:Input | ||
abstract val arguments: ListProperty<String> | ||
|
||
/** `-e` - Produce execution error messages. */ | ||
@get:Input | ||
@get:Optional | ||
abstract val showErrors: Property<Boolean> | ||
|
||
/** `-B` - Run in non-interactive (batch) mode. */ | ||
@get:Input | ||
@get:Optional | ||
abstract val batchMode: Property<Boolean> | ||
|
||
@TaskAction | ||
fun exec() { | ||
fs.sync { | ||
from(inputFiles) | ||
into(workDirectory) | ||
} | ||
|
||
val arguments = buildList { | ||
addAll(arguments.get()) | ||
if (showErrors.orNull == true) add("--errors") | ||
if (batchMode.orNull == true) add("--batch-mode") | ||
} | ||
|
||
exec.exec { | ||
workingDir(workDirectory) | ||
executable(mvnCli.get()) | ||
args(arguments) | ||
} | ||
|
||
makePropertiesFilesReproducible() | ||
} | ||
|
||
/** | ||
* Remove non-reproducible timestamps from any generated [Properties] files. | ||
*/ | ||
private fun makePropertiesFilesReproducible() { | ||
workDirectory.get().asFile.walk() | ||
.filter { it.isFile && it.extension == "properties" } | ||
.forEach { file -> | ||
logger.info("[MvnExec $path] removing timestamp from $file") | ||
// drop the last comment - java.util.Properties always adds a timestamp, which is not-reproducible. | ||
val comments = file.readLines() | ||
.takeWhile { it.startsWith('#') } | ||
.dropLast(1) | ||
|
||
val properties = file.readLines().dropWhile { it.startsWith('#') } | ||
|
||
val updatedProperties = (comments + properties).joinToString("\n") | ||
file.writeText(updatedProperties) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters