Skip to content

Commit

Permalink
fix: automatically add git version plugin to maven enforcer unChecked…
Browse files Browse the repository at this point in the history
…PluginList #171
  • Loading branch information
qoomon committed Nov 5, 2022
1 parent 43da9e2 commit 83a015e
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 30 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@

## 9.3.1

##### Fixes
- automatically add git version plugin to maven enforcer unCheckedPluginList #171

## 9.3.1

##### Fixes
- Extend gitlab ci support for env var CI_MERGE_REQUEST_SOURCE_BRANCH_NAME #218

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ create or update `${rootProjectDir}/.mvn/extensions.xml` file
<extension>
<groupId>me.qoomon</groupId>
<artifactId>maven-git-versioning-extension</artifactId>
<version>9.3.1</version>
<version>9.3.2</version>
</extension>

</extensions>
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

<groupId>me.qoomon</groupId>
<artifactId>maven-git-versioning-extension</artifactId>
<version>9.3.1</version>
<version>9.3.2</version>
<packaging>maven-plugin</packaging>

<name>Maven Git Versioning Extension</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.apache.maven.model.building.DefaultModelProcessor;
import org.apache.maven.model.building.ModelProcessor;
import org.apache.maven.session.scope.internal.SessionScope;
import org.codehaus.plexus.util.xml.Xpp3Dom;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
import org.slf4j.Logger;
Expand Down Expand Up @@ -508,22 +509,54 @@ private void addProjectProperties(Model projectModel) {
}

private void addBuildPlugin(Model projectModel) {
logger.debug("add version build plugin");

Plugin plugin = asPlugin();

PluginExecution execution = new PluginExecution();
execution.setId(GitVersioningMojo.GOAL);
execution.getGoals().add(GitVersioningMojo.GOAL);
logger.debug("add version build plugin");

plugin.getExecutions().add(execution);
Plugin gitVersioningPlugin = asPlugin();
{
PluginExecution execution = new PluginExecution();
execution.setId(GitVersioningMojo.GOAL);
execution.getGoals().add(GitVersioningMojo.GOAL);
gitVersioningPlugin.getExecutions().add(execution);
}

if (projectModel.getBuild() == null) {
projectModel.setBuild(new Build());
}

// add at index 0 to be executed before any other project plugin,
// to prevent malfunctions with other plugins
projectModel.getBuild().getPlugins().add(0, plugin);
projectModel.getBuild().getPlugins().add(0, gitVersioningPlugin);

// exclude this plugin from maven enforce plugin
{
LinkedList<Plugin> plugins = new LinkedList<>(projectModel.getBuild().getPlugins());
if (projectModel.getBuild().getPluginManagement() != null) {
plugins.addAll(projectModel.getBuild().getPluginManagement().getPlugins());
}
plugins.stream().filter(it -> it.getKey().equals("org.apache.maven.plugins:maven-enforcer-plugin")).forEach(plugin -> {
plugin.getExecutions().forEach(execution -> {
Xpp3Dom configuration = (Xpp3Dom) execution.getConfiguration();
if (configuration != null) {
Xpp3Dom rules = configuration.getChild("rules");
if (rules != null) {
Xpp3Dom requirePluginVersions = rules.getChild("requirePluginVersions");
if (requirePluginVersions != null) {
Xpp3Dom unCheckedPluginList = requirePluginVersions.getChild("unCheckedPluginList");
if (unCheckedPluginList == null) {
unCheckedPluginList = new Xpp3Dom("unCheckedPluginList");
requirePluginVersions.addChild(unCheckedPluginList);
}
// prepend git versioning plugin to unCheckedPluginList
unCheckedPluginList.setValue(gitVersioningPlugin.getKey() + ","
+ Objects.requireNonNullElse(unCheckedPluginList.getValue(), "")
);
}
}
}
});
});
}
}


Expand Down Expand Up @@ -739,7 +772,7 @@ private Map<String, Supplier<String>> generateFormatPlaceholderMap(String projec
}));

// deprecated
placeholderMap.put("version.release", Lazy.by(() -> projectVersion.replaceFirst("-.*$", "")));
placeholderMap.put("version.release", Lazy.by(() -> projectVersion.replaceFirst("-.*$", "")));

final Pattern projectVersionPattern = config.projectVersionPattern();
if (projectVersionPattern != null) {
Expand Down Expand Up @@ -842,7 +875,7 @@ private Map<String, Supplier<String>> generateGlobalFormatPlaceholderMap(GitSitu
final Lazy<Integer> descriptionDistance = Lazy.by(() -> description.get().getDistance());
placeholderMap.put("describe.distance", Lazy.by(() -> String.valueOf(descriptionDistance.get())));

placeholderMap.put("describe.tag.version.patch.plus.describe.distance", Lazy.by(() -> increase(placeholderMap.get("describe.tag.version.patch").get(), descriptionDistance.get() )));
placeholderMap.put("describe.tag.version.patch.plus.describe.distance", Lazy.by(() -> increase(placeholderMap.get("describe.tag.version.patch").get(), descriptionDistance.get())));
placeholderMap.put("describe.tag.version.patch.next.plus.describe.distance", Lazy.by(() -> increase(placeholderMap.get("describe.tag.version.patch.next").get(), descriptionDistance.get())));

placeholderMap.put("describe.tag.version.label.plus.describe.distance", Lazy.by(() -> increase(placeholderMap.get("describe.tag.version.version.label").get(), descriptionDistance.get())));
Expand Down
36 changes: 18 additions & 18 deletions src/test/resources/testProjects/standardProject/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@

<groupId>test</groupId>
<artifactId>test-artifact</artifactId>
<version>1.2.6-SNAPSHOT</version>
<version>1.2.7-SNAPSHOT</version>

<properties>
<foo.bar>xxx</foo.bar>
<project.build.outputTimestamp>xxx</project.build.outputTimestamp>
<!-- <project.build.outputTimestamp>xxx</project.build.outputTimestamp>-->
<dep.dependency-management-plugin>0.4</dep.dependency-management-plugin>
<dep.enforce-managed-deps-rule>1.3</dep.enforce-managed-deps-rule>
<dep.extra-enforcer-rules>1.5.1</dep.extra-enforcer-rules>
Expand All @@ -36,22 +36,22 @@

<build>
<plugins>
<!-- <plugin>-->
<!-- <artifactId>maven-enforcer-plugin</artifactId>-->
<!-- <executions>-->
<!-- <execution>-->
<!-- <id>fail</id>-->
<!-- <goals>-->
<!-- <goal>enforce</goal>-->
<!-- </goals>-->
<!-- <configuration>-->
<!-- <rules>-->
<!-- <requirePluginVersions/>-->
<!-- </rules>-->
<!-- </configuration>-->
<!-- </execution>-->
<!-- </executions>-->
<!-- </plugin>-->
<plugin>
<artifactId>maven-enforcer-plugin</artifactId>
<executions>
<execution>
<id>fail</id>
<goals>
<goal>enforce</goal>
</goals>
<configuration>
<rules>
<requirePluginVersions/>
</rules>
</configuration>
</execution>
</executions>
</plugin>
</plugins>

<pluginManagement>
Expand Down

0 comments on commit 83a015e

Please sign in to comment.