java实现修改pom.xml中版本信息

问题       

        在java基于流程化的开发过程中,版本管理一直是开发需要直面的问题。在目前基于maven开发下,由pom完成统一的版本管理,但是在进行目标版本升级时,常常会让人感到头痛。

        虽然maven有对应的命令可以直接执行。如:mvn versions:set -DnewVersion=1.0.0 versions:update-child-modules。但是此命令一般只会修改pom中的<version>标签中的版本信息,不会修改定义中的版本信息。同时如果不是此在父pom下的一些工程也不会进行版本信息的变更。

解决方案

        基于上面所描述的问题点,直接通过java代码实现修改xml文件内容,实现目标版本的替换。下面就提供java的一个简单代码实现。基于文件目录递归修改xml文件中的需要替换的版本信息。

/**
 * 替换文件中某个字符串并写入新内容(主要是实现修改pom.xml中的版本号)
 * 原理:根据文件路径,获取文件下的所有pom.xml文件,按行读取,一边读取并修改,同时并写入一个tmp的临时文件。当读取的行中发现有需要被替换和改写的目标内容时候,用新的内容在此行中进行替换。最后删除原文件,重命名tmp文件为原文件名称,也就是pom.xml文件。
 * 注意:代码功能是逐行读取一个字符串,然后检测该行的字符串中是否含有替换的目标内容,如果有则用新的字符内容替换这一行中的目标内容,没有则继续读。
 * 
 * @author joeljx
 *
 */
public class ReplaceFileContent {
	// 起始文件夹,从该文件目录开始。该文件目录下及其所有子目录的文件都将被替换。
	private String path;
	// 目标内容:需要被替换或者改写的字符内容
	private final String targetContent;
	// 替换内容:替换的新字符内容
	private final String newContent;

	public ReplaceFileContent(String path, String targetContent, String newContent) {
		this.path = path;
		this.targetContent = targetContent;
		this.newContent = newContent;
		operationFilePath();
	}

	// 操作文件路径
	public void operationFilePath() {
		File file = new File(path);
		opeationDirectory(file);
	}

	// 递归遍历文件夹
	public void opeationDirectory(File fileDir) {
		File[] files = fileDir.listFiles();
		for (int i = 0; i < files.length; i++) {
			File file = files[i];
			// 文件是目录就进行递归,获取下级文件
			if (file.isDirectory()) {
				opeationDirectory(file);
			}
			// 不是目录就直接操作文件,进行内容替换
			if (file.isFile()) {
				operationFileContent(file);
			}
		}
	}

	// 操作文件内容并进行替换
	public void operationFileContent(File file) {
		// 只操作以.xml结尾的文件,因为要修改pom.xml:判断可以再优化,比如就只有pom.xml文件才可以
		if (file.getName().endsWith(".xml")) {
			System.out.println("修改的文件:" + file.getAbsolutePath());
			try {
				InputStream is = new FileInputStream(file);
				BufferedReader bReader = new BufferedReader(new InputStreamReader(is));
				String filename = file.getName();
				// 新建缓存临时文件tmpfile,代码运行完毕后此临时文件将重命名为原文件名称。
				File tmpfile = new File(file.getParentFile().getAbsolutePath() + "\\" + filename + ".tmp");
				BufferedWriter bWriter = new BufferedWriter(new FileWriter(tmpfile));
				// 判断文件中是否有替换的标识,用于临时文件tmpfile是否直接删除还是重命名
				boolean flag = false;
				String str = null;
				while (true) {
					str = bReader.readLine();
					if (str == null) {
						break;
					}
					// 按行读,读取到的行字符串进行判断是否有目标字符串,有就进行替换;没有就直接写入
					if (str.contains(targetContent)) {
						int beginIndex = str.indexOf(targetContent);
						int endIndex = beginIndex + targetContent.length();
						bWriter.write(str.substring(0, beginIndex) + newContent + str.substring(endIndex) + "\n");
						flag = true;
					} else {
						bWriter.write(str + "\n");
					}
				}
				is.close();
				bWriter.flush();
				bWriter.close();

				if (flag) {
					file.delete();
					tmpfile.renameTo(new File(file.getAbsolutePath()));
				} else {
					tmpfile.delete();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	public static void main(String[] args) {
		// 把在D盘的test目录下以及所有子目录下(如果有)中以“.xml”结尾的文件中替换含有"1.0.1"的字符串行替换成新的"1.0.2"字符串行。
		new ReplaceFileContent("D:\\test", "1.0.1", "1.0.2");
	}
}

注意 

1、文件中的内容基于行读,pom中的版本信息存在换行就无法替换。

2、如果pom中版本在不同的引用下存在相同的版本也会被替换,上面代码还需要再进行按需的优化。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

行走的一只鞋

你的肯定是对我最大赞赏

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值