From 8a57da96b75d3324044c724396ea22bb5d514239 Mon Sep 17 00:00:00 2001 From: Varij Kapil Date: Tue, 1 Jun 2021 18:37:57 +0200 Subject: [PATCH 1/3] feat: allow `useGlobalMavenExecutable` option to override maven executable path --- README.md | 15 +++++++++++++++ pom.xml | 2 +- .../com/cosium/code/format/InstallHooksMojo.java | 16 +++++++++++++--- 3 files changed, 29 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 9be303d..e54c343 100644 --- a/README.md +++ b/README.md @@ -133,6 +133,21 @@ set -e On `pre-commit` git phase, the hook triggers the `git-code-format:on-pre-commit` which formats the code of the modified java files using `google-java-format`. +### Configuring Maven Executable Path +By default, the plugin will generate the following maven path + +```shell +"${env.M2_HOME}/bin/mvn" +``` + +If you want to use the global `mvn` variable, you can configure it using the `useGlobalMavenExecutable` property. + +```xml + + true + +``` + ### Advanced pre-commit pipeline hook If you wish to modify the output of the pre-commit hook, you can set the `preCommitHookPipeline` configuration. diff --git a/pom.xml b/pom.xml index cc56b6e..cafbf56 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ com.cosium.code git-code-format-maven-plugin - 2.7 + 2.8 maven-plugin Git Code Format Maven Plugin diff --git a/src/main/java/com/cosium/code/format/InstallHooksMojo.java b/src/main/java/com/cosium/code/format/InstallHooksMojo.java index 7cd5be1..dd907c3 100644 --- a/src/main/java/com/cosium/code/format/InstallHooksMojo.java +++ b/src/main/java/com/cosium/code/format/InstallHooksMojo.java @@ -2,6 +2,7 @@ import static java.util.Optional.ofNullable; + import com.cosium.code.format.executable.Executable; import com.cosium.code.format.executable.ExecutableManager; import com.cosium.code.format.maven.MavenEnvironment; @@ -74,6 +75,13 @@ public class InstallHooksMojo extends AbstractMavenGitCodeFormatMojo { @Parameter(property = "gcf.preCommitHookPipeline", defaultValue = "") private String preCommitHookPipeline; + /** + * Set to `true` to use the global `mvn` executable in the hooks script otherwise will default to + * the complete path for `mvn` found via `maven.home` variable + */ + @Parameter(property = "gcf.useGlobalMavenExecutable", defaultValue = "false") + private boolean useGlobalMavenExecutable; + public void execute() throws MojoExecutionException { if (!isExecutionRoot()) { getLog().debug("Not in execution root. Do not execute."); @@ -107,7 +115,7 @@ private void doExecute() throws IOException { private void writePluginHooks(Path hooksDirectory) throws IOException { getLog().debug("Removing legacy pre commit hook file"); Files.deleteIfExists(hooksDirectory.resolve(legacyPluginPreCommitHookFileName())); - getLog().debug("Rmeoved legacy pre commit hook file"); + getLog().debug("Removed legacy pre commit hook file"); getLog().debug("Writing plugin pre commit hook file"); executableManager @@ -115,7 +123,7 @@ private void writePluginHooks(Path hooksDirectory) throws IOException { .truncateWithTemplate( () -> getClass().getResourceAsStream(BASE_PLUGIN_PRE_COMMIT_HOOK), StandardCharsets.UTF_8.toString(), - mavenEnvironment.getMavenExecutable(debug).toAbsolutePath(), + useGlobalMavenExecutable ? "mvn" : mavenEnvironment.getMavenExecutable(debug).toAbsolutePath(), pomFile().toAbsolutePath(), mavenCliArguments()); getLog().debug("Written plugin pre commit hook file"); @@ -136,7 +144,9 @@ private void configureHookBaseScripts(Path hooksDirectory) throws IOException { private String mavenCliArguments() { Stream propagatedProperties = - ofNullable(propertiesToPropagate).map(Arrays::asList).orElse(Collections.emptyList()) + ofNullable(propertiesToPropagate) + .map(Arrays::asList) + .orElse(Collections.emptyList()) .stream() .filter(prop -> System.getProperty(prop) != null) .map(prop -> "-D" + prop + "=" + System.getProperty(prop)); From d442c793197107e5eb9b46b78a6ee638e5db00fe Mon Sep 17 00:00:00 2001 From: Varij Kapil Date: Tue, 1 Jun 2021 18:49:57 +0200 Subject: [PATCH 2/3] chore: revert version in pom.xml --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index cafbf56..cc56b6e 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ com.cosium.code git-code-format-maven-plugin - 2.8 + 2.7 maven-plugin Git Code Format Maven Plugin From 474006725d2697f4aee80c831762eca73eebfc98 Mon Sep 17 00:00:00 2001 From: Varij Kapil Date: Tue, 1 Jun 2021 18:51:36 +0200 Subject: [PATCH 3/3] chore: revert formatting back to original --- src/main/java/com/cosium/code/format/InstallHooksMojo.java | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/main/java/com/cosium/code/format/InstallHooksMojo.java b/src/main/java/com/cosium/code/format/InstallHooksMojo.java index dd907c3..a5dada6 100644 --- a/src/main/java/com/cosium/code/format/InstallHooksMojo.java +++ b/src/main/java/com/cosium/code/format/InstallHooksMojo.java @@ -2,7 +2,6 @@ import static java.util.Optional.ofNullable; - import com.cosium.code.format.executable.Executable; import com.cosium.code.format.executable.ExecutableManager; import com.cosium.code.format.maven.MavenEnvironment; @@ -144,9 +143,7 @@ private void configureHookBaseScripts(Path hooksDirectory) throws IOException { private String mavenCliArguments() { Stream propagatedProperties = - ofNullable(propertiesToPropagate) - .map(Arrays::asList) - .orElse(Collections.emptyList()) + ofNullable(propertiesToPropagate).map(Arrays::asList).orElse(Collections.emptyList()) .stream() .filter(prop -> System.getProperty(prop) != null) .map(prop -> "-D" + prop + "=" + System.getProperty(prop));