原來我的POM中groupId是org.tinygroup,artifactId是對應的工程名,如parser。
后出有一個處理上的原因,而且便于進行分隔,還可以避免與別人的沖突,想把所有的artifactId前面增加個“org.tinygroup.”前綴,比如parser就變成org.tinygroup.parser。但是這樣一來,所有的依賴信息也全都對不上了,也就是要對工程的artifactId及依賴中的artifactId都進行修改才行。
由于工程數比較多,一個一個手工改總是麻煩的,因此就想著寫程序進行處理。
1
public class ChangePom {
2
public static void main(String[] args) throws Throwable {
3
File file1 = new File("D:\\SVN\\tinyorg-code\\trunk\\Sources\\");
4
processFolder(file1);
5
}
6
7
private static void processFolder(File file1) throws Exception {
8
File[] files = file1.listFiles();
9
for (File file : files) {
10
if (file.isDirectory()) {
11
processFolder(file);
12
}
13
if (file.getName().equals("pom.xml")) {
14
processPomFile(file);
15
}
16
}
17
}
18
19
private static void processPomFile(File file) throws Exception {
20
System.out.println("processing:" + file.getAbsolutePath());
21
XmlStringParser parser = new XmlStringParser();
22
XmlDocument doc = parser.parse(IOUtils.readFromInputStream(new FileInputStream(file), "utf-8"));
23
XmlNode dependencies = doc.getRoot().getSubNode("dependencies");
24
XmlNode projectArtifactId = doc.getRoot().getSubNode("artifactId");
25
projectArtifactId.setContent("org.tinygroup" + projectArtifactId.getContent().trim());
26
if (dependencies != null) {
27
List<XmlNode> dependencyList = dependencies.getSubNodes("dependency");
28
if (dependencyList != null) {
29
for (XmlNode node : dependencyList) {
30
XmlNode groupId = node.getSubNode("groupId");
31
if (groupId.getContent().trim().equals("org.tinygroup")) {
32
XmlNode artifactId = node.getSubNode("artifactId");
33
artifactId.setContent("org.tinygroup." + artifactId.getContent().trim());
34
}
35
}
36
}
37
}
38
39
XmlFormater formater = new XmlFormater();
40
IOUtils.writeToOutputStream(new FileOutputStream(file), formater.format(doc), "UTF-8");
41
}
42
}

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

實現方案1:
-
呵呵,程序運行一會,馬上搞定了。
當然,這個時候,可能感覺還是有點麻煩,OK,再換一種寫法:
1
public class ChangePom1 {
2
public static void main(String[] args) throws Throwable {
3
FileObject fileObject= VFS.resolveFile("D:\\SVN\\tinyorg-code\\trunk\\Sources\\");
4
fileObject.foreach(new FileNameFileObjectFilter("pom\\.xml"),new FileObjectProcessor() {
5
public void process(FileObject fileObject) throws Exception {
6
System.out.println("processing:" + fileObject.getAbsolutePath());
7
XmlStringParser parser = new XmlStringParser();
8
XmlDocument doc = parser.parse(IOUtils.readFromInputStream(fileObject.getInputStream(), "utf-8"));
9
XmlNode dependencies = doc.getRoot().getSubNode("dependencies");
10
XmlNode projectArtifactId = doc.getRoot().getSubNode("artifactId");
11
projectArtifactId.setContent("org.tinygroup" + projectArtifactId.getContent().trim());
12
if (dependencies != null) {
13
List<XmlNode> dependencyList = dependencies.getSubNodes("dependency");
14
if (dependencyList != null) {
15
for (XmlNode node : dependencyList) {
16
XmlNode groupId = node.getSubNode("groupId");
17
if (groupId.getContent().trim().equals("org.tinygroup")) {
18
XmlNode artifactId = node.getSubNode("artifactId");
19
artifactId.setContent("org.tinygroup." + artifactId.getContent().trim());
20
}
21
}
22
}
23
}
24
25
XmlFormater formater = new XmlFormater();
26
IOUtils.writeToOutputStream(fileObject.getOutputStream(), formater.format(doc), "UTF-8");
27
}
28
});
29
}
30
}

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

1
formater.format(doc,fileObject.getOutputStream();

歡迎訪問框架論壇:http://web.j2ee.top。本例涉及的代碼和框架資料,將會在論壇分享。《自己動手寫框架》成員QQ群:228977971,讓我們一起動手,了解框架的奧秘!