Skip to content

Commit 9a85dc4

Browse files
committed
feat: update version based on conventional commits
1 parent 52bb4c3 commit 9a85dc4

File tree

10 files changed

+156
-18
lines changed

10 files changed

+156
-18
lines changed

README.md

+23-2
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ This is a Maven plugin for [Git Changelog Lib](https://github.com/tomasbjerre/gi
77

88
## Usage
99

10-
There is a running example [here](https://github.com/tomasbjerre/git-changelog-maven-plugin/tree/master/git-changelog-maven-plugin-example).
10+
There is a running example [here](/git-changelog-maven-plugin-example).
1111

12-
Have a look at the [pom.xml](https://github.com/tomasbjerre/git-changelog-maven-plugin/blob/master/git-changelog-maven-plugin-example/pom.xml) where you will find some more examples.
12+
Have a look at the [pom.xml](/git-changelog-maven-plugin-example/pom.xml) where you will find some more examples.
1313

1414
Here is and example that will generate a CHANGELOG.md when running `mvn generate-resources`.
1515

@@ -176,3 +176,24 @@ Handlebars.registerHelper('firstLetters', function(from, options) {
176176
```
177177

178178
More documentation can be found in the [Git Changelog Lib](https://github.com/tomasbjerre/git-changelog-lib).
179+
180+
### Configuration
181+
182+
Have a look at the [pom.xml](/git-changelog-maven-plugin-example/pom.xml) where you will find some more examples.
183+
184+
#### Update version based on conventional commits
185+
186+
The version in `pom.xml` can be automatically updated based on [conventional commits](https://www.conventionalcommits.org/en/v1.0.0/).
187+
188+
```xml
189+
<configuration>
190+
<!-- Skip changelog generation //-->
191+
<skip>true</skip>
192+
193+
<!-- Update the <version> tag //-->
194+
<updatePomWithNextSemanticVersion>true</updatePomWithNextSemanticVersion>
195+
196+
<!-- suffix version with -SNAPSHOT //-->
197+
<updatePomWithNextSemanticVersionSuffixSnapshot>true</updatePomWithNextSemanticVersionSuffixSnapshot>
198+
</configuration>
199+
```

git-changelog-maven-plugin-example/CHANGELOG_custom.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Changelog of Git Changelog Maven plugin.
44

55
## Unreleased
66
### No issue /
7-
feat: enable updating version in pom based on conventional commits
7+
chore: build script
88

99
chore: updating changelog
1010

git-changelog-maven-plugin-example/CHANGELOG_ignoreCommitsOlderThan.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Changelog of Git Changelog Maven plugin.
44

55
## Unreleased
66
### No issue
7-
feat: enable updating version in pom based on conventional commits *2022-09-25 09:53:19*
7+
chore: build script *2022-09-25 09:55:05*
88

99
chore: updating changelog *2022-09-24 13:38:39*
1010

git-changelog-maven-plugin-example/CHANGELOG_inline.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ Changelog of Git Changelog Maven plugin.
55
## Unreleased
66
### No issue
77

8-
[0f849cb7d7597c6](https://github.com/tomasbjerre/git-changelog-maven-plugin/commit/0f849cb7d7597c6) Tomas Bjerre *2022-09-25 09:53:19*
8+
[52bb4c3f9e02589](https://github.com/tomasbjerre/git-changelog-maven-plugin/commit/52bb4c3f9e02589) Tomas Bjerre *2022-09-25 09:55:05*
99

10-
feat: enable updating version in pom based on conventional commits
10+
chore: build script
1111

1212
[9a6f68a7468860f](https://github.com/tomasbjerre/git-changelog-maven-plugin/commit/9a6f68a7468860f) Tomas Bjerre *2022-09-24 13:38:39*
1313

git-changelog-maven-plugin-example/CHANGELOG_mixed.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Changelog of Git Changelog Maven plugin.
44

55
## These commits are not included in any tag
66
### These commits have not issue in their commit comment
7-
feat: enable updating version in pom based on conventional commits *09:53:19*
7+
chore: build script *09:55:05*
88

99
chore: updating changelog *13:38:39*
1010

git-changelog-maven-plugin-example/CHANGELOG_settings.md

-4
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@ Changelog of git-changelog-maven-plugin.
44

55
## Next release ()
66

7-
### Features
8-
9-
- enable updating version in pom based on conventional commits ([0f849](https://github.com/tomasbjerre/git-changelog-maven-plugin/commit/0f849cb7d7597c6) Tomas Bjerre)
10-
117
## 1.92.2 (2022-09-24)
128

139
### Bug Fixes

git-changelog-maven-plugin-example/pom.xml

+14-6
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
<?xml version="1.0" encoding="UTF-8"?>
2-
<project xmlns="http://maven.apache.org/POM/4.0.0"
3-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
1+
<?xml version="1.0" encoding="UTF-8" standalone="no"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
52
<modelVersion>4.0.0</modelVersion>
63
<groupId>se.bjurr.gitchangelog</groupId>
74
<artifactId>git-changelog-maven-plugin-example</artifactId>
85
<name>git-changelog-maven-plugin-example</name>
9-
<version>1.0-SNAPSHOT</version>
6+
<version>1.92.3-SNAPSHOT</version>
107
<properties>
118
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
129
<maven.compiler.target>1.8</maven.compiler.target>
@@ -453,8 +450,19 @@ Handlebars.registerHelper('firstLetters', function(from, options) {
453450
</templateContent>
454451
</configuration>
455452
</execution>
453+
<execution>
454+
<id>UpdateVersionInPom</id>
455+
<phase>generate-sources</phase>
456+
<goals>
457+
<goal>git-changelog</goal>
458+
</goals>
459+
<configuration>
460+
<skip>true</skip>
461+
<updatePomWithNextSemanticVersion>true</updatePomWithNextSemanticVersion>
462+
</configuration>
463+
</execution>
456464
</executions>
457465
</plugin>
458466
</plugins>
459467
</build>
460-
</project>
468+
</project>

pom.xml

+5
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@
5555
<version>3.6.0</version>
5656
<scope>provided</scope>
5757
</dependency>
58+
<dependency>
59+
<groupId>org.apache.maven</groupId>
60+
<artifactId>maven-core</artifactId>
61+
<version>3.6.0</version>
62+
</dependency>
5863
<dependency>
5964
<groupId>org.apache.maven.plugin-tools</groupId>
6065
<artifactId>maven-plugin-annotations</artifactId>

src/main/java/se/bjurr/gitchangelog/plugin/GitChangelogMojo.java

+43-1
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,22 @@
1111
import java.util.HashMap;
1212
import java.util.List;
1313
import java.util.Map;
14+
import org.apache.maven.model.Model;
1415
import org.apache.maven.plugin.AbstractMojo;
1516
import org.apache.maven.plugin.MojoExecutionException;
17+
import org.apache.maven.plugins.annotations.Component;
1618
import org.apache.maven.plugins.annotations.Mojo;
1719
import org.apache.maven.plugins.annotations.Parameter;
20+
import org.apache.maven.project.MavenProject;
1821
import se.bjurr.gitchangelog.api.GitChangelogApi;
22+
import se.bjurr.gitchangelog.internal.semantic.SemanticVersion;
1923

2024
@Mojo(name = "git-changelog", defaultPhase = PROCESS_SOURCES, threadSafe = true)
2125
public class GitChangelogMojo extends AbstractMojo {
2226
private static final String DEFAULT_FILE = "CHANGELOG.md";
2327

28+
@Component private MavenProject project;
29+
2430
@Parameter(property = "toRef", required = false)
2531
private String toRef;
2632

@@ -42,7 +48,8 @@ public class GitChangelogMojo extends AbstractMojo {
4248
@Parameter(property = "extendedHeaders", required = false)
4349
private Map extendedHeaders;
4450

45-
// map variables cannot be passed through maven cli use this property as a workaround
51+
// map variables cannot be passed through maven cli use this property as a
52+
// workaround
4653
@Parameter(property = "extendedVariablesCli", required = false)
4754
private String[] extendedVariablesCli;
4855

@@ -175,8 +182,43 @@ public class GitChangelogMojo extends AbstractMojo {
175182
@Parameter(property = "prependToFile", required = false)
176183
public Boolean prependToFile;
177184

185+
@Parameter(
186+
property = "updatePomWithNextSemanticVersion",
187+
required = false,
188+
defaultValue = "false")
189+
private boolean updatePomWithNextSemanticVersion;
190+
191+
@Parameter(
192+
property = "updatePomWithNextSemanticVersionSuffixSnapshot",
193+
required = false,
194+
defaultValue = "true")
195+
private boolean updatePomWithNextSemanticVersionSuffixSnapshot;
196+
178197
@Override
179198
public void execute() throws MojoExecutionException {
199+
if (this.updatePomWithNextSemanticVersion) {
200+
try {
201+
final SemanticVersion nextSemanticVersion =
202+
gitChangelogApiBuilder().getNextSemanticVersion();
203+
final String nextVersion =
204+
this.updatePomWithNextSemanticVersionSuffixSnapshot
205+
? nextSemanticVersion.getVersion() + "-SNAPSHOT"
206+
: nextSemanticVersion.getVersion();
207+
208+
final Model model = this.project.getModel();
209+
final String versionOrig = model.getVersion();
210+
final File pomFile = this.project.getFile();
211+
this.getLog()
212+
.info("Setting version to " + nextVersion + " was (" + versionOrig + ") in " + pomFile);
213+
// Change version during build
214+
model.setVersion(nextVersion);
215+
216+
// Change version in file
217+
new XmlModifier(pomFile).setVersion(nextVersion);
218+
} catch (final Exception e) {
219+
throw new MojoExecutionException(e.getMessage(), e);
220+
}
221+
}
180222
if (this.skip != null && this.skip == true) {
181223
this.getLog().info("Skipping changelog generation");
182224
return;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package se.bjurr.gitchangelog.plugin;
2+
3+
import java.io.File;
4+
import java.io.IOException;
5+
import javax.xml.parsers.DocumentBuilder;
6+
import javax.xml.parsers.DocumentBuilderFactory;
7+
import javax.xml.parsers.ParserConfigurationException;
8+
import javax.xml.transform.Transformer;
9+
import javax.xml.transform.TransformerFactory;
10+
import javax.xml.transform.dom.DOMSource;
11+
import javax.xml.transform.stream.StreamResult;
12+
import org.w3c.dom.Document;
13+
import org.w3c.dom.Node;
14+
import org.w3c.dom.NodeList;
15+
import org.xml.sax.SAXException;
16+
17+
public class XmlModifier {
18+
19+
private final File file;
20+
21+
public XmlModifier(final File file) {
22+
this.file = file;
23+
}
24+
25+
public void setVersion(final String value) throws Exception {
26+
final Document document = this.readDocument();
27+
28+
final Node projectNode = this.getNode(document.getChildNodes(), "project");
29+
final Node versionNode = this.getNode(projectNode.getChildNodes(), "version");
30+
versionNode.setTextContent(value);
31+
32+
this.saveDocument(document);
33+
}
34+
35+
private Document readDocument() throws ParserConfigurationException, SAXException, IOException {
36+
final DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
37+
final DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
38+
final Document document = dBuilder.parse(this.file);
39+
return document;
40+
}
41+
42+
private void saveDocument(final Node document) throws Exception {
43+
final TransformerFactory transformerFactory = TransformerFactory.newInstance();
44+
final Transformer transformer = transformerFactory.newTransformer();
45+
final DOMSource source = new DOMSource(document);
46+
final StreamResult result = new StreamResult(this.file);
47+
transformer.transform(source, result);
48+
}
49+
50+
private Node getNode(final NodeList nodes, final String tagName) {
51+
Node found = null;
52+
for (int tagIndex = 0; tagIndex < nodes.getLength(); tagIndex++) {
53+
final Node tag = nodes.item(tagIndex);
54+
if (tag.getNodeName().equals(tagName)) {
55+
if (found != null) {
56+
throw new RuntimeException("Found multiple " + tagName + " in " + nodes);
57+
}
58+
found = tag;
59+
}
60+
}
61+
if (found == null) {
62+
throw new RuntimeException("Cannot find " + tagName + " in " + nodes);
63+
}
64+
return found;
65+
}
66+
}

0 commit comments

Comments
 (0)