开源框架那些事儿
——tiny框架 成员QQ群:228977971
BlogJava
首页
新随笔
联系
聚合
管理
posts - 27, comments - 0, trackbacks - 0
《开源框架那些事儿2》:对整个目录中的POM树进行批量处理
下面是一个具体的案例,即对整个目录中的POM树进行批量处理 。
原来我的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
}
实现方案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
}
下面的xml格式化并输出到文件中,也可以写成:
1
formater.format(doc,fileObject.getOutputStream();
当然,上面只是临时进行一下处理,中间的Xml处理写得稍微丑陋一点。
欢迎访问框架论坛:
http://web.j2ee.top
。本例涉及的代码和框架资料,将会在论坛分享。《自己动手写框架》成员QQ群:228977971,让我们一起动手,了解框架的奥秘!
posted on 2015-06-08 23:12
柏然
阅读(72)
评论(0)
编辑
收藏
新用户注册
刷新评论列表
只有注册用户
登录
后才能发表评论。
网站导航:
博客园
IT新闻
Chat2DB
C++博客
博问
管理
<
2015年6月
>
日
一
二
三
四
五
六
31
1
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
2
3
4
5
6
7
8
9
10
11
常用链接
我的随笔
我的评论
我的参与
留言簿
给我留言
查看公开留言
查看私人留言
随笔档案
2015年8月 (2)
2015年7月 (8)
2015年6月 (17)
搜索
最新评论
阅读排行榜
1. 《开源框架那些事儿3》:高屋建瓴,理念先行 (2542)
2. 《开源框架那些事儿4》:关于框架体系与战术的思考(460)
3. 《开源框架那些事儿26》:“最好的模板引擎”Beetl剖析及与Tiny模板引擎(441)
4. 《自己动手写开源框架10》:Tiny快速入门之Web界面快速开发实践(347)
5. 《开源框架那点事儿20》:发布TinyUI前端框架 (156)
评论排行榜
1. 《开源框架那些事儿27》一段SQL引发的性能危机及其背后隐藏的设计缺陷 (0)
2. 《开源框架那些事儿26》:“最好的模板引擎”Beetl剖析及与Tiny模板引擎(0)
3. 《开源框架那点事儿25》:对框架模板引擎实现方式的改造实录 (0)
4. 《开源框架那点事儿24》:开着跑车换轮胎(0)
5. 《开源分享1》:前端开发必备《Html-CSS中文手册》 (0)