From a1db59fdb0784c57ac2505e0ece854790f95ac69 Mon Sep 17 00:00:00 2001 From: evanchooly Date: Sun, 27 Aug 2023 15:15:56 -0400 Subject: [PATCH] WIP --- mojo/pom.xml | 30 +++----- .../antwerkz/build/GradleInvocationMojo.kt | 76 ++++++++----------- .../com/antwerkz/build/ReplacementMojo.kt | 34 +++++---- 3 files changed, 64 insertions(+), 76 deletions(-) diff --git a/mojo/pom.xml b/mojo/pom.xml index b93d8ea..f7f8715 100644 --- a/mojo/pom.xml +++ b/mojo/pom.xml @@ -65,12 +65,23 @@ com.antwerkz.super-expressive super-expressive 0.2.0 + + + org.jetbrains.kotlin + kotlin-stdlib-jdk8 + + org.zeroturnaround zt-exec + + org.jetbrains.kotlin + kotlin-stdlib + ${kotlin.version} + com.github.zafarkhaja java-semver @@ -112,25 +123,6 @@ logback-classic - \ No newline at end of file diff --git a/mojo/src/main/kotlin/com/antwerkz/build/GradleInvocationMojo.kt b/mojo/src/main/kotlin/com/antwerkz/build/GradleInvocationMojo.kt index bc82bb8..97959a8 100644 --- a/mojo/src/main/kotlin/com/antwerkz/build/GradleInvocationMojo.kt +++ b/mojo/src/main/kotlin/com/antwerkz/build/GradleInvocationMojo.kt @@ -1,13 +1,16 @@ package com.antwerkz.build +import java.io.File +import java.io.FileOutputStream +import java.lang.Compiler.command import org.apache.maven.plugin.AbstractMojo +import org.apache.maven.plugin.MojoExecutionException import org.apache.maven.plugins.annotations.Component import org.apache.maven.plugins.annotations.Mojo import org.apache.maven.plugins.annotations.Parameter import org.apache.maven.project.MavenProject import org.codehaus.plexus.logging.Logger import org.zeroturnaround.exec.ProcessExecutor -import org.zeroturnaround.exec.stream.LogOutputStream @Mojo(name = "gradle") class GradleInvocationMojo : AbstractMojo() { @@ -15,10 +18,12 @@ class GradleInvocationMojo : AbstractMojo() { private val baseCommand = listOf( "java", + "-Xmx64m", + "-Xms64m", + "-Dorg.gradle.appname=gradlew", "-cp", "gradle/wrapper/gradle-wrapper.jar", "org.gradle.wrapper.GradleWrapperMain", - "--no-daemon", "--console", "plain" ) @@ -28,51 +33,36 @@ class GradleInvocationMojo : AbstractMojo() { @Parameter(defaultValue = "\${project}", required = true, readonly = true) lateinit var project: MavenProject - @Parameter lateinit var task: GradleTask + @Parameter(defaultValue = "true") var log = true - @Parameter(defaultValue = "true") var quiet = true - - /* - - java - ${project.basedir} - - -cp - gradle/wrapper/gradle-wrapper.jar - org.gradle.wrapper.GradleWrapperMain - classes - - true - - - */ override fun execute() { var command = baseCommand - if (quiet) { - command += "--quiet" - } - command += listOf("-i", task.name, *task.args.toTypedArray()) - logger.debug("executing command: ${command.joinToString(" ")}") - ProcessExecutor() - .command(command) - .directory(project.basedir) - .redirectOutput( - object : LogOutputStream() { - override fun processLine(line: String) { - if (!quiet) { - logger.info("> ${line}") - } - } - } + command += listOf(task.name, *task.args.toTypedArray()) + val executor = ProcessExecutor().command(command).directory(project.basedir) + if (log) { + logger.info( + "invoking gradle:\n\tdirectory: ${project.basedir}\n\tcommand: ${command.joinToString(" ")}" ) - .redirectError( - object : LogOutputStream() { - override fun processLine(line: String) { - logger.error("[error] > ${line}") - } - } - ) - .execute() + val infoLogFile = File(project.basedir, "build/graven/${task.name}-info.log") + infoLogFile.parentFile.mkdirs() + logger.info("Logging enabled. See build/graven for details.") + executor + .redirectOutput(FileOutputStream(infoLogFile)) + .redirectError( + FileOutputStream(File(project.basedir, "build/graven/${task.name}-error.log")) + ) + } + val execute = executor.execute() + + if (execute.exitValue != 0) { + if (log) { + throw MojoExecutionException("gradle run failed. check logs in build/graven/") + } else { + throw MojoExecutionException( + "gradle run failed. enabling logging to see the gradle output." + ) + } + } } } diff --git a/mojo/src/main/kotlin/com/antwerkz/build/ReplacementMojo.kt b/mojo/src/main/kotlin/com/antwerkz/build/ReplacementMojo.kt index e4d1325..65ec710 100644 --- a/mojo/src/main/kotlin/com/antwerkz/build/ReplacementMojo.kt +++ b/mojo/src/main/kotlin/com/antwerkz/build/ReplacementMojo.kt @@ -3,6 +3,7 @@ package com.antwerkz.build import com.antwerkz.expression.RegularExpression import com.antwerkz.expression.toRegex import java.io.File +import java.io.IOException import java.nio.charset.Charset import java.util.Properties import org.apache.maven.model.Dependency @@ -75,8 +76,9 @@ class ReplacementMojo : AbstractMojo() { .split(",") .map { it.trim() } .forEach { - val file = File(it) + val file = File(project.basedir, it) + logger.info("Updating ${file}") if (file.exists()) { file.writeText( file @@ -97,20 +99,24 @@ class ReplacementMojo : AbstractMojo() { } private fun updateGradleWrapper() { - val file = File("gradle/wrapper/gradle-wrapper.properties") - - file.writeText( - file - .readLines(Charset.defaultCharset()) - .map { line: String -> - if (line.startsWith("distributionUrl=", ignoreCase = true)) { - """distributionUrl=https://services.gradle.org/distributions/gradle-${gradleVersion}-bin.zip""" - } else { - line + val file = File(project.basedir, "gradle/wrapper/gradle-wrapper.properties") + + try { + file.writeText( + file + .readLines(Charset.defaultCharset()) + .map { line: String -> + if (line.startsWith("distributionUrl=", ignoreCase = true)) { + """distributionUrl=https://services.gradle.org/distributions/gradle-${gradleVersion}-bin.zip""" + } else { + line + } } - } - .joinToString("\n") - ) + .joinToString("\n") + ) + } catch (e: IOException) { + throw e + } } }