- public class ChangeVersion {
- /**
- * 修改pom文件的版本号
- * @since 2014-3-20
- * @param file
- * @param version
- * @throws ParserConfigurationException
- * @throws IOException
- * @throws SAXException
- */
- public static void parse(File file, String version) throws Exception {
- if (null == file || !file.exists()) {
- System.err.println(file.getPath());
- return;
- }
- DocumentBuilderFactory builderFactory = DocumentBuilderFactory
- .newInstance();
- Document document = builderFactory.newDocumentBuilder().parse(file);
- NodeList nodeList = document.getElementsByTagName("project").item(0)
- .getChildNodes();
- Node node = null;
- for (int i = 0; i < nodeList.getLength(); i++) {
- if ("version".equals(nodeList.item(i).getNodeName())) {
- node = nodeList.item(i);
- break;
- }
- }
- if (null == node)
- return;
- node.setTextContent(version);
- TransformerFactory transFactory = TransformerFactory.newInstance();
- transFactory.newTransformer().transform(new DOMSource(document),
- new StreamResult(file));
- System.out.println(version+"----"+file.getPath());
- }
- public static List<File> getPom(String basePath) {
- String projects = "base";
- String[] procject = projects.split(",");
- List<File> fileList = new ArrayList<File>();
- for (String str : procject) {
- String path = basePath;
- path = path + "/" + str + "/pom.xml";
- fileList.add(new File(path));
- }
- return fileList;
- }
- /**
- * @since 2014-3-20
- * @param args
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- String basePath="E:\\\\platform2\\\\platform";
- String version="2.0.6-SNAPSHOT";
- List<File> fileList=ChangeVersion.getPom(basePath);
- try {
- for (File file : fileList) {
- ChangeVersion.parse(file, version);
- }
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- //该片段来自于http://www.codesnippet.cn/detail/2708201513557.html
来源: http://www.codesnippet.cn/detail/2708201513557.html