Skip to content
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

feat: add useGlobalMavenExecutable configuration #72

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
```
Comment on lines +137 to +141
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As you can see in MavenEnvironment, this is not true :)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, sorry.
MavenEnvironment indicates use of maven.home,
whereas README says ${env.M2_HOME}/bin/mvn in How the hook works section.
I'll remove this line, but maybe the other sections needs updating too?


If you want to use the global `mvn` variable, you can configure it using the `useGlobalMavenExecutable` property.

```xml
<configuration>
<useGlobalMavenExecutable>true</useGlobalMavenExecutable>
</configuration>
```

### Advanced pre-commit pipeline hook
If you wish to modify the output of the pre-commit hook, you can set the `preCommitHookPipeline` configuration.

Expand Down
11 changes: 9 additions & 2 deletions src/main/java/com/cosium/code/format/InstallHooksMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,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;
Copy link
Member

@reda-alaoui reda-alaoui Jun 4, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you using a maven wrapper? If not, what are you using exactly?
Isn't there some environment variable or anything set by the executed process to find its path?
That would allow us to avoid setting this property and instead add an automatic path detection to MavenEnvironment.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not using a maven wrapper. I am using IntelliJ IDEA to run maven tasks.

It does infact set the maven.home property when any maven command is run via the IDE but the plugin somehow does not consider it.

IntelliJ sets the following property when running any maven command:

-Dmaven.home=/Applications/JetBrains/apps/IDEA-U/ch-0/211.7442.40/IntelliJ IDEA.app/Contents/plugins/maven/lib/maven3

This is an example how the package command is run:

/Library/Java/JavaVirtualMachines/jdk1.8.0_202.jdk/Contents/Home/bin/java -Dmaven.multiModuleProjectDirectory=/Users/varijkapil/Documents/Work/Development/JavaEE/work-service -Dmaven.home=/Applications/JetBrains/apps/IDEA-U/ch-0/211.7442.40/IntelliJ IDEA.app/Contents/plugins/maven/lib/maven3 -Dclassworlds.conf=/Applications/JetBrains/apps/IDEA-U/ch-0/211.7442.40/IntelliJ IDEA.app/Contents/plugins/maven/lib/maven3/bin/m2.conf -Dmaven.ext.class.path=/Applications/JetBrains/apps/IDEA-U/ch-0/211.7442.40/IntelliJ IDEA.app/Contents/plugins/maven/lib/maven-event-listener.jar -javaagent:/Applications/JetBrains/apps/IDEA-U/ch-0/211.7442.40/IntelliJ IDEA.app/Contents/lib/idea_rt.jar=54014:/Applications/JetBrains/apps/IDEA-U/ch-0/211.7442.40/IntelliJ IDEA.app/Contents/bin -Dfile.encoding=UTF-8 -classpath /Applications/JetBrains/apps/IDEA-U/ch-0/211.7442.40/IntelliJ IDEA.app/Contents/plugins/maven/lib/maven3/boot/plexus-classworlds.license:/Applications/JetBrains/apps/IDEA-U/ch-0/211.7442.40/IntelliJ IDEA.app/Contents/plugins/maven/lib/maven3/boot/plexus-classworlds-2.6.0.jar org.codehaus.classworlds.Launcher -Didea.version=2021.1.2 package

I see in MavenEnvironment that maven.home is given preference, but cannot figure out why it is not considered when this command is run? Instead it writes the following in the pre-commit bash script
/Users/varijkapil/Documents/Work/Development/JavaEE/work-service/mvn

Maybe a little help in this direction would be helpful.


public void execute() throws MojoExecutionException {
if (!isExecutionRoot()) {
getLog().debug("Not in execution root. Do not execute.");
Expand Down Expand Up @@ -107,15 +114,15 @@ 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
.getOrCreateExecutableScript(hooksDirectory.resolve(pluginPreCommitHookFileName()))
.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");
Expand Down