From 928e20d0a3dbb69da9a06d7419322b4b1afd63db Mon Sep 17 00:00:00 2001 From: Marc Nuri Date: Thu, 3 Aug 2023 11:26:06 +0200 Subject: [PATCH] feat: extension remains quiet when -q flag is passed Signed-off-by: Marc Nuri --- src/it/gradle-7/pom.xml | 7 ---- src/it/gradle-7/verify.groovy | 2 + src/it/gradle-8/pom.xml | 7 ---- src/it/gradle-8/verify.groovy | 2 + src/it/no-log/invoker.properties | 2 + src/it/no-log/pom.xml | 37 +++++++++++++++++ src/it/no-log/verify.groovy | 2 + .../plugins/gradle/api/GradleApi.java | 4 +- .../gradle/api/GradleApiExtension.java | 4 +- .../plugins/gradle/api/GradleApiLog.java | 40 +++++++++++++++++++ 10 files changed, 88 insertions(+), 19 deletions(-) create mode 100644 src/it/gradle-7/verify.groovy create mode 100644 src/it/gradle-8/verify.groovy create mode 100644 src/it/no-log/invoker.properties create mode 100644 src/it/no-log/pom.xml create mode 100644 src/it/no-log/verify.groovy create mode 100644 src/main/java/com/marcnuri/plugins/gradle/api/GradleApiLog.java diff --git a/src/it/gradle-7/pom.xml b/src/it/gradle-7/pom.xml index 6c6a564..87fa3dc 100644 --- a/src/it/gradle-7/pom.xml +++ b/src/it/gradle-7/pom.xml @@ -31,13 +31,6 @@ gradle-api-maven-plugin @project.version@ true - - - - gradle-api - - - diff --git a/src/it/gradle-7/verify.groovy b/src/it/gradle-7/verify.groovy new file mode 100644 index 0000000..e5bbe36 --- /dev/null +++ b/src/it/gradle-7/verify.groovy @@ -0,0 +1,2 @@ +def buildLog = new File(basedir, 'build.log').text +assert buildLog.contains('Extracting Gradle 7.') diff --git a/src/it/gradle-8/pom.xml b/src/it/gradle-8/pom.xml index d1bbd2a..4bca2a3 100644 --- a/src/it/gradle-8/pom.xml +++ b/src/it/gradle-8/pom.xml @@ -31,13 +31,6 @@ gradle-api-maven-plugin @project.version@ true - - - - gradle-api - - - diff --git a/src/it/gradle-8/verify.groovy b/src/it/gradle-8/verify.groovy new file mode 100644 index 0000000..3d0d303 --- /dev/null +++ b/src/it/gradle-8/verify.groovy @@ -0,0 +1,2 @@ +def buildLog = new File(basedir, 'build.log').text +assert buildLog.contains('Extracting Gradle 8.') diff --git a/src/it/no-log/invoker.properties b/src/it/no-log/invoker.properties new file mode 100644 index 0000000..bc8dfc3 --- /dev/null +++ b/src/it/no-log/invoker.properties @@ -0,0 +1,2 @@ +invoker.goals.1=verify +invoker.quiet=true diff --git a/src/it/no-log/pom.xml b/src/it/no-log/pom.xml new file mode 100644 index 0000000..7f86e8d --- /dev/null +++ b/src/it/no-log/pom.xml @@ -0,0 +1,37 @@ + + + 4.0.0 + com.marcnuri.plugins.it + no-action + 0.1-SNAPSHOT + Maven Integration Test :: Gradle API :: No Action + + + + org.codehaus.groovy + groovy-all + ${version.groovy} + pom + provided + + + org.gradle + gradle-all + ${version.gradle.8} + provided + + + + + + + com.marcnuri.plugins + gradle-api-maven-plugin + @project.version@ + true + + + + diff --git a/src/it/no-log/verify.groovy b/src/it/no-log/verify.groovy new file mode 100644 index 0000000..f6b971a --- /dev/null +++ b/src/it/no-log/verify.groovy @@ -0,0 +1,2 @@ +def buildLog = new File(basedir, 'build.log').text +assert !buildLog.contains('Extracting Gradle') diff --git a/src/main/java/com/marcnuri/plugins/gradle/api/GradleApi.java b/src/main/java/com/marcnuri/plugins/gradle/api/GradleApi.java index e35400a..f7ee420 100644 --- a/src/main/java/com/marcnuri/plugins/gradle/api/GradleApi.java +++ b/src/main/java/com/marcnuri/plugins/gradle/api/GradleApi.java @@ -26,13 +26,13 @@ public class GradleApi implements Callable> { static final String GRADLE_GROUP_ID = "org.gradle"; static final String GRADLE_ALL_ARTIFACT_ID = "gradle-all"; private static final String GRADLE_DISTRIBUTION_BASE_URL = "https://services.gradle.org/distributions/"; - private final Log log; + private final GradleApiLog log; private final boolean forceUpdate; private final String gradleVersion; private final Path repositoryBaseDir; private final Path gradleBinZip; - public GradleApi(Log log, boolean forceUpdate, String gradleVersion, Path repositoryBaseDir) { + public GradleApi(GradleApiLog log, boolean forceUpdate, String gradleVersion, Path repositoryBaseDir) { this.log = log; this.gradleVersion = gradleVersion; this.forceUpdate = forceUpdate; diff --git a/src/main/java/com/marcnuri/plugins/gradle/api/GradleApiExtension.java b/src/main/java/com/marcnuri/plugins/gradle/api/GradleApiExtension.java index bc8a719..2bd91ea 100644 --- a/src/main/java/com/marcnuri/plugins/gradle/api/GradleApiExtension.java +++ b/src/main/java/com/marcnuri/plugins/gradle/api/GradleApiExtension.java @@ -3,8 +3,6 @@ import org.apache.maven.AbstractMavenLifecycleParticipant; import org.apache.maven.execution.MavenSession; import org.apache.maven.model.Dependency; -import org.apache.maven.plugin.logging.Log; -import org.apache.maven.plugin.logging.SystemStreamLog; import org.apache.maven.project.MavenProject; import javax.inject.Named; @@ -24,7 +22,7 @@ public class GradleApiExtension extends AbstractMavenLifecycleParticipant { @Override public void afterProjectsRead(MavenSession session) { - final Log log = new SystemStreamLog(); + final GradleApiLog log = new GradleApiLog(session.getRequest().getLoggingLevel()); final Map projects = new HashMap<>(); for (MavenProject project : session.getProjects()) { project.getDependencies().stream() diff --git a/src/main/java/com/marcnuri/plugins/gradle/api/GradleApiLog.java b/src/main/java/com/marcnuri/plugins/gradle/api/GradleApiLog.java new file mode 100644 index 0000000..2c38a81 --- /dev/null +++ b/src/main/java/com/marcnuri/plugins/gradle/api/GradleApiLog.java @@ -0,0 +1,40 @@ +package com.marcnuri.plugins.gradle.api; + +import org.apache.maven.execution.MavenExecutionRequest; +import org.apache.maven.plugin.logging.Log; +import org.apache.maven.plugin.logging.SystemStreamLog; + +import java.util.function.Consumer; + +public final class GradleApiLog { + + private final int loggingLevel; + private final Log delegate; + + GradleApiLog(int loggingLevel) { + this.loggingLevel = loggingLevel; + delegate = new SystemStreamLog(); + } + + public void debug(CharSequence content) { + log(MavenExecutionRequest.LOGGING_LEVEL_DEBUG, delegate::debug, content); + } + + public void info(CharSequence content) { + log(MavenExecutionRequest.LOGGING_LEVEL_INFO, delegate::info, content); + } + + public void warn(CharSequence content) { + log(MavenExecutionRequest.LOGGING_LEVEL_WARN, delegate::warn, content); + } + + public void error(CharSequence content) { + log(MavenExecutionRequest.LOGGING_LEVEL_ERROR, delegate::error, content); + } + + private void log(int level, Consumer logFunc, CharSequence content) { + if (loggingLevel <= level) { + logFunc.accept(content); + } + } +}