Skip to content

Commit 19918d4

Browse files
dtruckenktoso
authored andcommitted
fix for Allow initialization once per run (with multiple modules) #53
Added a new optional configuration parameter ‘runOnlyOnce’
1 parent 6f9abe8 commit 19918d4

File tree

4 files changed

+55
-3
lines changed

4 files changed

+55
-3
lines changed

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,21 @@ It's really simple to setup this plugin; below is a sample pom that you may base
173173
-->
174174
<skip>false</skip>
175175

176+
<!-- @since 2.1.12 -->
177+
<!--
178+
Use with caution!
179+
180+
In a multi-module build, only run once. This means that the plugins effects will only execute once, for the parent project.
181+
This probably won't "do the right thing" if your project has more than one git repository.
182+
183+
Important: If you're using `generateGitPropertiesFile`, setting `runOnlyOnce` will make the plugin
184+
only generate the file in the directory where you started your build (!).
185+
186+
The `git.*` maven properties are available in all modules.
187+
Default value is `false`.
188+
-->
189+
<runOnlyOnce>false</runOnlyOnce>
190+
176191
<!-- @since 2.1.9 -->
177192
<!--
178193
Can be used to exclude certain properties from being emited into the resulting file.
@@ -543,6 +558,7 @@ Optional parameters:
543558
* **failOnNoGitDirectory** - `(default: true)` *(available since v2.0.4)* - Specify whether the plugin should fail when a .git directory can not be found. When set to false and no .git directory is found the plugin will skip execution.
544559
* **failOnUnableToExtractRepoInfo** - `(default: true)` By default the plugin will fail the build if unable to obtain enough data for a complete run, if you don't care about this, you may want to set this value to false.
545560
* **skip** - `(default: false)` *(available since v2.1.8)* - Skip the plugin execution completely.
561+
* **runOnlyOnce** - `(default: false)` *(available since v2.1.12)* - Use with caution! In a multi-module build, only run once. This means that the plugins effects will only execute once, for the parent project. This probably won't "do the right thing" if your project has more than one git repository. Important: If you're using `generateGitPropertiesFile`, setting `runOnlyOnce` will make the plugin only generate the file in the directory where you started your build :warning:. The `git.*` maven properties are available in all modules.
546562
* **excludeProperties** - `(default: empty)` *(available since v2.1.9)* - Allows to filter out properties that you *don't* want to expose. This feature was implemented in response to [this issue](https://github.com/ktoso/maven-git-commit-id-plugin/issues/91), so if you're curious about the use-case, check that issue.
547563
* **useNativeGit** - `(default: false)` *(available since v2.1.10)* - Uses the native `git` binary instead of the custom `jgit` implementation shipped with this plugin to obtain all information. Although this should usualy give your build some performance boost, it may randomly break if you upgrade your git version and it decides to print information in a different format suddenly. As rule of thumb, keep using the default `jgit` implementation (keep this option set to `false`) until you notice performance problems within your build (usualy when you have *hundreds* of maven modules).
548564
* **abbrevLength** - `(default: 7)` Configure the "git.commit.id.abbrev" property to be at least of length N (see gitDescribe abbrev for special case abbrev = 0).

pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,11 @@
8888
<artifactId>maven-plugin-api</artifactId>
8989
<version>${maven-plugin-api.version}</version>
9090
</dependency>
91+
<dependency>
92+
<groupId>org.apache.maven</groupId>
93+
<artifactId>maven-core</artifactId>
94+
<version>${maven-plugin-api.version}</version>
95+
</dependency>
9196
<dependency>
9297
<groupId>org.apache.maven</groupId>
9398
<artifactId>maven-project</artifactId>

src/main/java/pl/project13/jgit/DescribeCommand.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,8 @@ public DescribeCommand always(boolean always) {
162162
@NotNull
163163
public DescribeCommand forceLongFormat(@Nullable Boolean forceLongFormat) {
164164
if (forceLongFormat != null && forceLongFormat) {
165-
this.forceLongFormat = forceLongFormat;
166-
log("--long =", forceLongFormat);
165+
this.forceLongFormat = true;
166+
log("--long =", true);
167167
}
168168
return this;
169169
}

src/main/java/pl/project13/maven/git/GitCommitIdMojo.java

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import com.google.common.collect.Lists;
2626
import com.google.common.io.Closeables;
2727
import com.google.common.io.Files;
28+
import org.apache.maven.execution.MavenSession;
2829
import org.apache.maven.plugin.AbstractMojo;
2930
import org.apache.maven.plugin.MojoExecutionException;
3031
import org.apache.maven.project.MavenProject;
@@ -257,6 +258,19 @@ public class GitCommitIdMojo extends AbstractMojo {
257258
@SuppressWarnings("UnusedDeclaration")
258259
private boolean skip = false;
259260

261+
/**
262+
* In a multi-module build, only run once. This probably won't "do the right thing" if your project has more than
263+
* one git repository. If you use this with the option 'generateGitPropertiesFile', it will only generate (or update)
264+
* the file in the directory where you started your build.
265+
*
266+
* The git.* maven properties are available in all modules.
267+
*
268+
* @parameter default-value="false"
269+
* @since 2.1.12
270+
*/
271+
@SuppressWarnings("UnusedDeclaration")
272+
private boolean runOnlyOnce = false;
273+
260274
/**
261275
* Can be used to exclude certain properties from being emited into the resulting file.
262276
* May be useful when you want to hide {@code git.remote.origin.url} (maybe because it contains your repo password?),
@@ -273,6 +287,16 @@ public class GitCommitIdMojo extends AbstractMojo {
273287
@SuppressWarnings("UnusedDeclaration")
274288
private List<String> excludeProperties = Collections.emptyList();
275289

290+
/**
291+
* The Maven Session Object
292+
*
293+
* @parameter expression="${session}"
294+
* @required
295+
* @readonly
296+
*/
297+
@SuppressWarnings("UnusedDeclaration")
298+
protected MavenSession session;
299+
276300
/**
277301
* The properties we store our data in and then expose them
278302
*/
@@ -288,10 +312,17 @@ public void execute() throws MojoExecutionException {
288312
loggerBridge.setVerbose(verbose);
289313

290314
if (skip) {
291-
log("skip is true, return");
315+
log("skip is enabled, skipping execution!");
292316
return;
293317
}
294318

319+
if (runOnlyOnce) {
320+
if (!session.getExecutionRootDirectory().equals(session.getCurrentProject().getBasedir().getAbsolutePath())) {
321+
log("runOnlyOnce is enabled and this project is not the top level project, skipping execution!");
322+
return;
323+
}
324+
}
325+
295326
if (isPomProject(project) && skipPoms) {
296327
log("isPomProject is true and skipPoms is true, return");
297328
return;

0 commit comments

Comments
 (0)