Skip to content

Commit 3ce854d

Browse files
committed
feat(maven): upgrade to version 0.0.8 with automated migration
- Update Maven plugin version from 0.0.7 to 0.0.8 - Create updateNxMavenPluginVersion utility function with proper XML parsing - Add migration to automatically update user pom.xml files on upgrade - Register migration in migrations.json - Add comprehensive unit tests (9 test cases, all passing) The utility function uses DOM parsing (@xmldom/xmldom) to safely update only the dev.nx.maven:nx-maven-plugin version element, leaving other versions (dependencies, parent, etc.) untouched.
1 parent 29a6142 commit 3ce854d

File tree

9 files changed

+413
-4
lines changed

9 files changed

+413
-4
lines changed

packages/maven/maven-plugin/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<groupId>dev.nx</groupId>
77
<artifactId>nx-parent</artifactId>
8-
<version>0.0.7</version>
8+
<version>0.0.8</version>
99
<relativePath>../../../pom.xml</relativePath>
1010
</parent>
1111

packages/maven/migrations.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"$schema": "../../node_modules/nx/schemas/generators-schema.json",
3+
"generators": {
4+
"update-0.0.8": {
5+
"cli": "nx",
6+
"version": "0.0.8-beta.0",
7+
"description": "Update Maven plugin version from 0.0.7 to 0.0.8 in pom.xml files",
8+
"factory": "./dist/migrations/0-0-8/update-pom-xml-version"
9+
}
10+
}
11+
}

packages/maven/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@nx/maven",
3-
"version": "0.0.1",
3+
"version": "0.0.8",
44
"private": false,
55
"description": "Nx plugin for Maven integration",
66
"repository": {
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { Tree } from '@nx/devkit';
2+
import { updateNxMavenPluginVersion } from '../../utils/pom-xml-updater';
3+
4+
/**
5+
* Migration for @nx/maven v0.0.8
6+
* Updates the Maven plugin version from 0.0.7 to 0.0.8 in user pom.xml files
7+
*/
8+
export default async function update(tree: Tree) {
9+
updateNxMavenPluginVersion(tree, '0.0.8');
10+
}
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing
2+
3+
exports[`pom-xml-updater updateNxMavenPluginVersion should correctly identify nx-maven-plugin with whitespace 1`] = `
4+
"<?xml version="1.0" encoding="UTF-8"?>
5+
<project xmlns="http://maven.apache.org/POM/4.0.0">
6+
<build>
7+
<plugins>
8+
<plugin>
9+
<groupId>
10+
dev.nx.maven
11+
</groupId>
12+
<artifactId>
13+
nx-maven-plugin
14+
</artifactId>
15+
<version>0.0.8</version>
16+
</plugin>
17+
</plugins>
18+
</build>
19+
</project>"
20+
`;
21+
22+
exports[`pom-xml-updater updateNxMavenPluginVersion should handle pom.xml with dependencies and other version elements 1`] = `
23+
"<?xml version="1.0" encoding="UTF-8"?>
24+
<project xmlns="http://maven.apache.org/POM/4.0.0">
25+
<version>1.0.0</version>
26+
<build>
27+
<plugins>
28+
<plugin>
29+
<groupId>dev.nx.maven</groupId>
30+
<artifactId>nx-maven-plugin</artifactId>
31+
<version>0.0.8</version>
32+
</plugin>
33+
</plugins>
34+
</build>
35+
<dependencies>
36+
<dependency>
37+
<groupId>junit</groupId>
38+
<artifactId>junit</artifactId>
39+
<version>4.13.2</version>
40+
</dependency>
41+
</dependencies>
42+
</project>"
43+
`;
44+
45+
exports[`pom-xml-updater updateNxMavenPluginVersion should not update other plugin versions 1`] = `
46+
"<?xml version="1.0" encoding="UTF-8"?>
47+
<project xmlns="http://maven.apache.org/POM/4.0.0">
48+
<build>
49+
<plugins>
50+
<plugin>
51+
<groupId>dev.nx.maven</groupId>
52+
<artifactId>nx-maven-plugin</artifactId>
53+
<version>0.0.8</version>
54+
</plugin>
55+
<plugin>
56+
<groupId>org.apache.maven.plugins</groupId>
57+
<artifactId>maven-compiler-plugin</artifactId>
58+
<version>0.0.7</version>
59+
</plugin>
60+
</plugins>
61+
</build>
62+
</project>"
63+
`;
64+
65+
exports[`pom-xml-updater updateNxMavenPluginVersion should only update plugins, not parent version 1`] = `
66+
"<?xml version="1.0" encoding="UTF-8"?>
67+
<project xmlns="http://maven.apache.org/POM/4.0.0">
68+
<parent>
69+
<groupId>dev.nx</groupId>
70+
<artifactId>nx-parent</artifactId>
71+
<version>0.0.7</version>
72+
</parent>
73+
<build>
74+
<plugins>
75+
<plugin>
76+
<groupId>dev.nx.maven</groupId>
77+
<artifactId>nx-maven-plugin</artifactId>
78+
<version>0.0.8</version>
79+
</plugin>
80+
</plugins>
81+
</build>
82+
</project>"
83+
`;
84+
85+
exports[`pom-xml-updater updateNxMavenPluginVersion should preserve XML formatting and structure 1`] = `
86+
"<?xml version="1.0" encoding="UTF-8"?>
87+
<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/xsd/maven-4.0.0.xsd">
88+
<build>
89+
<plugins>
90+
<plugin>
91+
<groupId>dev.nx.maven</groupId>
92+
<artifactId>nx-maven-plugin</artifactId>
93+
<version>0.0.8</version>
94+
</plugin>
95+
</plugins>
96+
</build>
97+
</project>"
98+
`;
99+
100+
exports[`pom-xml-updater updateNxMavenPluginVersion should update the nx-maven-plugin version only 1`] = `
101+
"<?xml version="1.0" encoding="UTF-8"?>
102+
<project xmlns="http://maven.apache.org/POM/4.0.0">
103+
<build>
104+
<plugins>
105+
<plugin>
106+
<groupId>dev.nx.maven</groupId>
107+
<artifactId>nx-maven-plugin</artifactId>
108+
<version>0.0.8</version>
109+
</plugin>
110+
</plugins>
111+
</build>
112+
</project>"
113+
`;
Lines changed: 221 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,221 @@
1+
import { Tree } from '@nx/devkit';
2+
import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing';
3+
import { updateNxMavenPluginVersion } from './pom-xml-updater';
4+
5+
describe('pom-xml-updater', () => {
6+
let tree: Tree;
7+
8+
beforeEach(() => {
9+
tree = createTreeWithEmptyWorkspace({});
10+
});
11+
12+
describe('updateNxMavenPluginVersion', () => {
13+
it('should update the nx-maven-plugin version only', () => {
14+
// Arrange
15+
const pomContent = `<?xml version="1.0" encoding="UTF-8"?>
16+
<project xmlns="http://maven.apache.org/POM/4.0.0">
17+
<build>
18+
<plugins>
19+
<plugin>
20+
<groupId>dev.nx.maven</groupId>
21+
<artifactId>nx-maven-plugin</artifactId>
22+
<version>0.0.7</version>
23+
</plugin>
24+
</plugins>
25+
</build>
26+
</project>`;
27+
28+
tree.write('pom.xml', pomContent);
29+
30+
// Act
31+
updateNxMavenPluginVersion(tree, '0.0.8');
32+
33+
// Assert
34+
const updatedContent = tree.read('pom.xml', 'utf-8');
35+
expect(updatedContent).toMatchSnapshot();
36+
});
37+
38+
it('should not update other plugin versions', () => {
39+
// Arrange
40+
const pomContent = `<?xml version="1.0" encoding="UTF-8"?>
41+
<project xmlns="http://maven.apache.org/POM/4.0.0">
42+
<build>
43+
<plugins>
44+
<plugin>
45+
<groupId>dev.nx.maven</groupId>
46+
<artifactId>nx-maven-plugin</artifactId>
47+
<version>0.0.7</version>
48+
</plugin>
49+
<plugin>
50+
<groupId>org.apache.maven.plugins</groupId>
51+
<artifactId>maven-compiler-plugin</artifactId>
52+
<version>0.0.7</version>
53+
</plugin>
54+
</plugins>
55+
</build>
56+
</project>`;
57+
58+
tree.write('pom.xml', pomContent);
59+
60+
// Act
61+
updateNxMavenPluginVersion(tree, '0.0.8');
62+
63+
// Assert
64+
const updatedContent = tree.read('pom.xml', 'utf-8');
65+
expect(updatedContent).toMatchSnapshot();
66+
});
67+
68+
it('should not modify pom.xml if file does not exist', () => {
69+
// Act & Assert - should not throw
70+
expect(() => {
71+
updateNxMavenPluginVersion(tree, '0.0.8');
72+
}).not.toThrow();
73+
});
74+
75+
it('should preserve XML formatting and structure', () => {
76+
// Arrange
77+
const pomContent = `<?xml version="1.0" encoding="UTF-8"?>
78+
<project xmlns="http://maven.apache.org/POM/4.0.0"
79+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
80+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
81+
<build>
82+
<plugins>
83+
<plugin>
84+
<groupId>dev.nx.maven</groupId>
85+
<artifactId>nx-maven-plugin</artifactId>
86+
<version>0.0.7</version>
87+
</plugin>
88+
</plugins>
89+
</build>
90+
</project>`;
91+
92+
tree.write('pom.xml', pomContent);
93+
94+
// Act
95+
updateNxMavenPluginVersion(tree, '0.0.8');
96+
97+
// Assert
98+
const updatedContent = tree.read('pom.xml', 'utf-8');
99+
expect(updatedContent).toMatchSnapshot();
100+
});
101+
102+
it('should not update if version is already the target version', () => {
103+
// Arrange
104+
const pomContent = `<?xml version="1.0" encoding="UTF-8"?>
105+
<project xmlns="http://maven.apache.org/POM/4.0.0">
106+
<build>
107+
<plugins>
108+
<plugin>
109+
<groupId>dev.nx.maven</groupId>
110+
<artifactId>nx-maven-plugin</artifactId>
111+
<version>0.0.8</version>
112+
</plugin>
113+
</plugins>
114+
</build>
115+
</project>`;
116+
117+
tree.write('pom.xml', pomContent);
118+
119+
// Act
120+
updateNxMavenPluginVersion(tree, '0.0.8');
121+
122+
// Assert
123+
const updatedContent = tree.read('pom.xml', 'utf-8');
124+
expect(updatedContent).toEqual(pomContent);
125+
});
126+
127+
it('should handle pom.xml with dependencies and other version elements', () => {
128+
// Arrange
129+
const pomContent = `<?xml version="1.0" encoding="UTF-8"?>
130+
<project xmlns="http://maven.apache.org/POM/4.0.0">
131+
<version>1.0.0</version>
132+
<build>
133+
<plugins>
134+
<plugin>
135+
<groupId>dev.nx.maven</groupId>
136+
<artifactId>nx-maven-plugin</artifactId>
137+
<version>0.0.7</version>
138+
</plugin>
139+
</plugins>
140+
</build>
141+
<dependencies>
142+
<dependency>
143+
<groupId>junit</groupId>
144+
<artifactId>junit</artifactId>
145+
<version>4.13.2</version>
146+
</dependency>
147+
</dependencies>
148+
</project>`;
149+
150+
tree.write('pom.xml', pomContent);
151+
152+
// Act
153+
updateNxMavenPluginVersion(tree, '0.0.8');
154+
155+
// Assert
156+
const updatedContent = tree.read('pom.xml', 'utf-8');
157+
expect(updatedContent).toMatchSnapshot();
158+
});
159+
160+
it('should correctly identify nx-maven-plugin with whitespace', () => {
161+
// Arrange
162+
const pomContent = `<?xml version="1.0" encoding="UTF-8"?>
163+
<project xmlns="http://maven.apache.org/POM/4.0.0">
164+
<build>
165+
<plugins>
166+
<plugin>
167+
<groupId>
168+
dev.nx.maven
169+
</groupId>
170+
<artifactId>
171+
nx-maven-plugin
172+
</artifactId>
173+
<version>
174+
0.0.7
175+
</version>
176+
</plugin>
177+
</plugins>
178+
</build>
179+
</project>`;
180+
181+
tree.write('pom.xml', pomContent);
182+
183+
// Act
184+
updateNxMavenPluginVersion(tree, '0.0.8');
185+
186+
// Assert
187+
const updatedContent = tree.read('pom.xml', 'utf-8');
188+
expect(updatedContent).toMatchSnapshot();
189+
});
190+
191+
it('should only update plugins, not parent version', () => {
192+
// Arrange
193+
const pomContent = `<?xml version="1.0" encoding="UTF-8"?>
194+
<project xmlns="http://maven.apache.org/POM/4.0.0">
195+
<parent>
196+
<groupId>dev.nx</groupId>
197+
<artifactId>nx-parent</artifactId>
198+
<version>0.0.7</version>
199+
</parent>
200+
<build>
201+
<plugins>
202+
<plugin>
203+
<groupId>dev.nx.maven</groupId>
204+
<artifactId>nx-maven-plugin</artifactId>
205+
<version>0.0.7</version>
206+
</plugin>
207+
</plugins>
208+
</build>
209+
</project>`;
210+
211+
tree.write('pom.xml', pomContent);
212+
213+
// Act
214+
updateNxMavenPluginVersion(tree, '0.0.8');
215+
216+
// Assert
217+
const updatedContent = tree.read('pom.xml', 'utf-8');
218+
expect(updatedContent).toMatchSnapshot();
219+
});
220+
});
221+
});

0 commit comments

Comments
 (0)