-
Notifications
You must be signed in to change notification settings - Fork 344
Support Maven smoke test apps #11695
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,201 @@ | ||
| package datadog.buildlogic.smoketest | ||
|
|
||
| import org.gradle.api.DefaultTask | ||
| import org.gradle.api.GradleException | ||
| import org.gradle.api.file.DirectoryProperty | ||
| import org.gradle.api.file.FileTree | ||
| import org.gradle.api.file.RegularFileProperty | ||
| import org.gradle.api.model.ObjectFactory | ||
| import org.gradle.api.provider.ListProperty | ||
| import org.gradle.api.provider.MapProperty | ||
| import org.gradle.api.provider.Property | ||
| import org.gradle.api.tasks.CacheableTask | ||
| import org.gradle.api.tasks.IgnoreEmptyDirectories | ||
| import org.gradle.api.tasks.Input | ||
| import org.gradle.api.tasks.InputFile | ||
| import org.gradle.api.tasks.InputFiles | ||
| import org.gradle.api.tasks.Internal | ||
| import org.gradle.api.tasks.Nested | ||
| import org.gradle.api.tasks.Optional | ||
| import org.gradle.api.tasks.OutputDirectory | ||
| import org.gradle.api.tasks.PathSensitive | ||
| import org.gradle.api.tasks.PathSensitivity | ||
| import org.gradle.api.tasks.TaskAction | ||
| import org.gradle.jvm.toolchain.JavaLanguageVersion | ||
| import org.gradle.jvm.toolchain.JavaLauncher | ||
| import org.gradle.jvm.toolchain.JavaToolchainService | ||
| import java.io.File | ||
| import java.util.concurrent.TimeUnit | ||
| import javax.inject.Inject | ||
|
|
||
| /** | ||
| * Runs a nested Maven build inside [applicationDir] via the root Maven wrapper. | ||
| * | ||
| * The nested build is expected to honour `-Dtarget.dir=<path>` and write outputs under that | ||
| * directory so Gradle can track the artifact under [applicationBuildDir]. | ||
| */ | ||
| @CacheableTask | ||
| abstract class NestedMavenBuild @Inject constructor( | ||
| private val objects: ObjectFactory, | ||
| javaToolchains: JavaToolchainService, | ||
| ) : DefaultTask() { | ||
|
|
||
| init { | ||
| javaLauncher.convention( | ||
| javaToolchains.launcherFor { | ||
| languageVersion.set(JavaLanguageVersion.of(DEFAULT_NESTED_JAVA_VERSION)) | ||
| }, | ||
| ) | ||
| goals.convention(listOf("package")) | ||
| arguments.convention(emptyList()) | ||
| environment.convention(emptyMap()) | ||
| mavenOpts.convention("") | ||
| useMavenLocalRepository.convention(false) | ||
| } | ||
|
|
||
| @get:Internal | ||
| abstract val applicationDir: DirectoryProperty | ||
|
|
||
| @get:InputFiles | ||
| @get:IgnoreEmptyDirectories | ||
| @get:PathSensitive(PathSensitivity.RELATIVE) | ||
| val applicationSources: FileTree = | ||
| objects.fileTree().from(applicationDir).matching { | ||
| exclude("target/**") | ||
| } | ||
|
|
||
| @get:OutputDirectory | ||
| abstract val applicationBuildDir: DirectoryProperty | ||
|
|
||
| @get:InputFile | ||
| @get:PathSensitive(PathSensitivity.RELATIVE) | ||
| abstract val mavenExecutable: RegularFileProperty | ||
|
Comment on lines
+70
to
+72
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Because this cacheable task only declares the wrapper script itself as an input, changes to Useful? React with 👍 / 👎. |
||
|
|
||
| @get:InputFiles | ||
| @get:IgnoreEmptyDirectories | ||
| @get:PathSensitive(PathSensitivity.RELATIVE) | ||
| val mavenWrapperFiles: FileTree = | ||
| objects.fileTree().from(project.rootProject.layout.projectDirectory.dir(".mvn/wrapper")).matching { | ||
| include("maven-wrapper.properties", "maven-wrapper.jar") | ||
| } | ||
|
|
||
| @get:Nested | ||
| abstract val javaLauncher: Property<JavaLauncher> | ||
|
|
||
| @get:Input | ||
| abstract val goals: ListProperty<String> | ||
|
|
||
| @get:Input | ||
| abstract val arguments: ListProperty<String> | ||
|
|
||
| @get:Input | ||
| abstract val environment: MapProperty<String, String> | ||
|
|
||
| @get:Input | ||
| @get:Optional | ||
| abstract val mavenOpts: Property<String> | ||
|
|
||
| @get:Input | ||
| @get:Optional | ||
| abstract val mavenRepositoryProxy: Property<String> | ||
|
|
||
| @get:Input | ||
| abstract val useMavenLocalRepository: Property<Boolean> | ||
|
|
||
| /** Timeout, in seconds, for the nested Maven build process. */ | ||
| @get:Input | ||
| @get:Optional | ||
| abstract val buildTimeoutSeconds: Property<Long> | ||
|
|
||
| @get:Internal | ||
| abstract val mavenLocalRepository: DirectoryProperty | ||
|
|
||
| @TaskAction | ||
| fun runNestedBuild() { | ||
| val appDir = applicationDir.get().asFile | ||
| val appBuildDirFile = applicationBuildDir.get().asFile | ||
| val targetDir = appBuildDirFile.resolve("target") | ||
| val daemonJavaHome = javaLauncher.get().metadata.installationPath.asFile | ||
| val command = mavenCommand(mavenExecutable.get().asFile).apply { | ||
| add("-Dtarget.dir=${targetDir.absolutePath}") | ||
| if (useMavenLocalRepository.get()) { | ||
| add("-Dmaven.repo.local=${mavenLocalRepository.get().asFile.absolutePath}") | ||
| } | ||
| addAll(arguments.get()) | ||
| addAll(goals.get()) | ||
| } | ||
|
|
||
| val process = ProcessBuilder(command) | ||
| .directory(appDir) | ||
| .redirectOutput(ProcessBuilder.Redirect.INHERIT) | ||
| .redirectError(ProcessBuilder.Redirect.INHERIT) | ||
| .apply { | ||
| environment().apply { | ||
| clear() | ||
| putAll(nestedEnvironment(daemonJavaHome)) | ||
| } | ||
| } | ||
| .start() | ||
|
|
||
| try { | ||
| val timeoutSeconds = buildTimeoutSeconds.orNull | ||
| val completed = | ||
| if (timeoutSeconds == null) { | ||
| process.waitFor() | ||
| true | ||
| } else { | ||
| process.waitFor(timeoutSeconds, TimeUnit.SECONDS) | ||
| } | ||
| if (!completed) { | ||
| process.destroyForcibly() | ||
| throw GradleException( | ||
| "Nested Maven build timed out after $timeoutSeconds seconds: " + | ||
| command.joinToString(" "), | ||
| ) | ||
| } | ||
| } catch (e: InterruptedException) { | ||
| process.destroyForcibly() | ||
| Thread.currentThread().interrupt() | ||
| throw GradleException("Interrupted while running nested Maven build", e) | ||
| } | ||
| val exitCode = process.exitValue() | ||
| if (exitCode != 0) { | ||
| throw GradleException( | ||
| "Nested Maven build failed with exit code $exitCode: ${command.joinToString(" ")}", | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| private fun nestedEnvironment(daemonJavaHome: File): Map<String, String> { | ||
| val env = System.getenv().toMutableMap() | ||
| val repositoryProxy = mavenRepositoryProxy.orNull?.takeIf { it.isNotBlank() } | ||
| if (repositoryProxy != null) { | ||
| env["MAVEN_REPOSITORY_PROXY"] = repositoryProxy | ||
| env.putIfAbsent("MVNW_REPOURL", repositoryProxy.trimEnd('/')) | ||
| } | ||
| val opts = mavenOpts.orNull | ||
| if (!opts.isNullOrBlank()) { | ||
| env["MAVEN_OPTS"] = opts | ||
| } | ||
| env["JAVA_HOME"] = daemonJavaHome.absolutePath | ||
| env.putAll(environment.get()) | ||
| return env | ||
| } | ||
|
|
||
| private fun mavenCommand(executable: File): MutableList<String> = | ||
| if (isWindows()) { | ||
| mutableListOf("cmd", "/c", executable.absolutePath) | ||
| } else { | ||
| mutableListOf(executable.absolutePath) | ||
| } | ||
|
|
||
| companion object { | ||
| internal fun mavenWrapperName(osName: String = System.getProperty("os.name")): String = | ||
| if (isWindows(osName)) { | ||
| "mvnw.cmd" | ||
| } else { | ||
| "mvnw" | ||
| } | ||
|
|
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package datadog.buildlogic.smoketest | ||
|
|
||
| import java.util.Locale | ||
|
|
||
| internal fun isWindows(osName: String = System.getProperty("os.name")): Boolean = | ||
| osName.lowercase(Locale.ROOT).contains("windows") | ||
|
|
Uh oh!
There was an error while loading. Please reload this page.