Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 35 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,6 @@ Add this to your maven project **root** pom.xml :
<artifactId>maven-git-code-format</artifactId>
<version>${maven-git-code-format.version}</version>
<executions>
<!-- On commit, format the modified java files -->
<execution>
<id>install-formatter-hook</id>
<goals>
<goal>install-hooks</goal>
</goals>
</execution>
<!-- On Maven verify phase, fail if any file
(including unmodified) is badly formatted -->
<execution>
Expand All @@ -37,6 +30,34 @@ Add this to your maven project **root** pom.xml :
</plugin>
</plugins>
</build>

<profiles>
<profile>
<!-- Install pre-commit hook if missing-->
<id>install-pre-commit-hook</id>
<activation>
<file>
<missing>.git/hooks/pre-commit</missing>
</file>
</activation>
<build>
<plugins>
<plugin>
<groupId>com.cosium.code</groupId>
<artifactId>maven-git-code-format</artifactId>
<executions>
<execution>
<id>install-hook</id>
<goals>
<goal>install-hooks</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
```

### Manual code formatting
Expand Down Expand Up @@ -108,7 +129,13 @@ You can also manually [format](#manual-code-formatting) or [validate](#manual-co
On the `initialize` maven phase, `git-code-format:install-hooks` installs a git `pre-commit` hook that looks like this :
```bash
#!/bin/bash
/usr/share/apache-maven-3.5.0/bin/mvn git-code-format:on-pre-commit
"./.git/hooks/${project.artifactId}.maven-git-code-format.pre-commit.sh"
```
and `.git/hooks/${project.artifactId}.maven-git-code-format.pre-commit.sh` has the following content:
```bash
#!/bin/bash
set -e
"${env.M2_HOME} /bin/mvn" -f "${project.basedir}/pom.xml" git-code-format:on-pre-commit
```

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`.
24 changes: 15 additions & 9 deletions src/main/java/com/cosium/code/format/InstallHooksMojo.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
package com.cosium.code.format;

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;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.Collections;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static java.util.Optional.ofNullable;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;

/**
* Installs git hooks on each initialization. Hooks are always overriden in case changes in:
Expand Down Expand Up @@ -53,6 +52,10 @@ public class InstallHooksMojo extends AbstractMavenGitCodeFormatMojo {
@Parameter(property = "debug", defaultValue = "false")
private boolean debug;

/** Make the pre-commit hook quiet */
@Parameter(property = "quiet", defaultValue = "false")
private boolean quiet;

public void execute() throws MojoExecutionException {
if (!isExecutionRoot()) {
getLog().debug("Not in execution root. Do not execute.");
Expand Down Expand Up @@ -107,8 +110,11 @@ private String mavenCliArguments() {
.filter(prop -> System.getProperty(prop) != null)
.map(prop -> "-D" + prop + "=" + System.getProperty(prop));

return Stream.concat(propagatedProperties, Stream.of(propertiesToAdd))
.collect(Collectors.joining(" "));
Stream<String> properties = Stream.concat(propagatedProperties, Stream.of(propertiesToAdd));
if (quiet) {
properties = Stream.concat(properties, Stream.of(">/dev/null"));
}
return properties.collect(Collectors.joining(" "));
}

private Path prepareHooksDirectory() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/bin/bash
set -e
%s -f %s git-code-format:on-pre-commit %s
%s -f %s com.cosium.code:maven-git-code-format:on-pre-commit %s