Multiple File read from multiple places and update in multiple bat file

 

Multiple File read from multiple places and update in multiple bat file as java jar version.



I am creating a Java Application to replace or a java version to an bat file. I have a folder of bat files that I want to change the java jar file version for and I don't want to manually do every bat file. I want to read the bat, find the jar file name tag (REGEX = "([^\\s]+(\\.(?i)(txt|doc|csv|pdf|jar))$)"), and replace it with a new jar version (i.e Some New jar version Here) in each bat file in the folder




package com.kartik.jar.version;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URISyntaxException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class JarFileVersion {

	public static void main(String[] args) throws IOException, URISyntaxException {
				
		JarFileVersion jfv = new JarFileVersion();
		Map<String, String> mapInput = new LinkedHashMap<String, String>();
		mapInput.put("CONFIG_SERVICE", "C:/Users/SG0308108/FileDetails/configuration/configuration-impl/target");
		mapInput.put("CONFIG_PROVIDER_SERVICE", "C:/Users/SG0308108/FileDetails/configuration-provider/configuration-provider-impl/target");
		mapInput.put("SESSION_SERVICE", "C:/Users/SG0308108/FileDetails/session/session-service/target");
		mapInput.put("LOGIN_SERVICE", "C:/Users/SG0308108/FileDetails/login-profile/login-profile-impl/target");
		mapInput.put("AIR_SERVICE", "C:/Users/SG0308108/FileDetails/air/air-impl/service/target");
		mapInput.put("ANCILLARIES_SERVICE", "C:/Users/SG0308108/FileDetails/ancillaries/ancillaries-impl/target");
		mapInput.put("BAGAGE_SERVICE", "C:/Users/SG0308108/FileDetails/baggage/baggage-impl/target");		
		mapInput.put("FAIR_RULE_SERVICE", "C:/Users/SG0308108/FileDetails/farerules/farerules-impl/target");
		mapInput.put("FLIGHT_STATUS_SERVICE", "C:/Users/SG0308108/FileDetails/flight-status/flight-status-impl/target");
		mapInput.put("INSURANCE_SERVICE", "C:/Users/SG0308108/FileDetails/insurance/insurance-impl/target");
		mapInput.put("PASSENGER_SERVICE", "C:/Users/SG0308108/FileDetails/passenger/passenger-impl/target");
		mapInput.put("PAYMENT_OPTION_SERVICE", "C:/Users/SG0308108/FileDetails/payment-options/payment-options-impl/target");
		mapInput.put("PRODUCT_SERVICE", "C:/Users/SG0308108/FileDetails/products/products-impl/service/target");
		mapInput.put("PURCHASE_SERVICE_1", "C:/Users/SG0308108/FileDetails/purchase/purchase-impl/payment-service/target");
		mapInput.put("PURCHASE_SERVICE_2", "C:/Users/SG0308108/FileDetails/purchase/booking/booking-impl/target");
		mapInput.put("PURCHASE_SERVICE_3", "C:/Users/SG0308108/FileDetails/purchase/purchase-impl/purchase-orchestrator/target");
		mapInput.put("PURCHASE_SERVICE_4", "C:/Users/SG0308108/FileDetails/purchase/purchase-impl/poller-service/target");
		mapInput.put("PURCHASE_SERVICE_5", "C:/Users/SG0308108/FileDetails/purchase/purchase-impl/fulfillment-service/target");
		mapInput.put("PURCHASE_SERVICE_6", "C:/Users/SG0308108/FileDetails/purchase/purchase-impl/fraud-check-service/target");
		mapInput.put("SEAT_SERVICE", "C:/Users/SG0308108/FileDetails/seats/seats-impl/target");
		mapInput.put("PNR_SERVICE", "C:/Users/SG0308108/FileDetails/pnr/pnr-impl/target");
		
		Map<String, String> mapOutPut = new LinkedHashMap<String, String>();
		for (Map.Entry<String, String> entry : mapInput.entrySet()) {
			System.out.println(entry.getKey() + ":" + entry.getValue());
			mapOutPut.put(entry.getKey(),
					jfv.getResourceListing(entry.getValue()));
		}
		for (Map.Entry<String, String> entry : mapOutPut.entrySet()) {
			jfv.searchAndUpdate(entry.getKey(), entry.getValue());
			// System.out.println("kkk "+entry.getValue());
			// jfv.runBatFile(entry.getKey()+".bat");
		}

	}

	String getResourceListing(String path) throws URISyntaxException,
			UnsupportedEncodingException, IOException {
		File folder = new File(path);
		File[] listOfFiles = folder.listFiles();

		for (int i = 0; i < listOfFiles.length; i++) {
			if (listOfFiles[i].isFile()) {
				String fName = listOfFiles[i].getName();
				if (fName.endsWith(".jar")) {
					System.out.println("File " + fName);
					return fName;
				}
			} else if (listOfFiles[i].isDirectory()) {
				// System.out.println("Directory " + listOfFiles[i].getName());
			}
		}
		return null;
	}

	void searchAndUpdate(String fileName, String newFileName)
			throws IOException {
		String REGEX = "([^\\s]+(\\.(?i)(txt|doc|csv|pdf|jar))$)";
		String file = "C:/DC/KartikRequired/Project/Common/FileDetails_bat/batch/"
				+ fileName + ".bat";
		Path path = Paths.get(file);
		Charset charset = StandardCharsets.UTF_8;
		String content = new String(Files.readAllBytes(path), charset);
		Pattern p = Pattern.compile(REGEX);
		Matcher m = p.matcher(content);
		content = m.replaceAll(newFileName);
		Files.write(path, content.getBytes(charset));
	}

	void runBatFile(String fileName) {
		try {
			Runtime.getRuntime()
					.exec("cmd /c " + fileName,
							null,
							new File(
									"C:/DC/KartikRequired/Project/Common/FileDetails_bat/batch/"));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	String getVersion(String pathWithFile) {
		File file = new File(pathWithFile);
		String versionNumber = "";
		String fileName = file.getName().substring(0,
				file.getName().lastIndexOf("."));
		if (fileName.contains(".")) {
			String majorVersion = fileName.substring(0, fileName.indexOf("."));
			String minorVersion = fileName.substring(fileName.indexOf("."));
			int delimiter = majorVersion.lastIndexOf("-");
			if (majorVersion.indexOf("_") > delimiter)
				delimiter = majorVersion.indexOf("_");
			majorVersion = majorVersion.substring(delimiter + 1,
					fileName.indexOf("."));
			versionNumber = majorVersion + minorVersion;
		}
		System.out.println("Version: " + versionNumber);
		return versionNumber;
	}

	String getVersionDetails(String pathWithFile) throws IOException {
		/*java.io.File file = new java.io.File(
				"C:/Users/SG0308108/FileDetails/air/air-impl/service/target/air-service-2.0.14-SNAPSHOT.jar");*/
		java.io.File file = new java.io.File(pathWithFile);
		java.util.jar.JarFile jar = new java.util.jar.JarFile(file);
		java.util.jar.Manifest manifest = jar.getManifest();
		String versionNumber = "";
		java.util.jar.Attributes attributes = manifest.getMainAttributes();
		if (attributes != null) {
			java.util.Iterator it = attributes.keySet().iterator();
			while (it.hasNext()) {
				java.util.jar.Attributes.Name key = (java.util.jar.Attributes.Name) it
						.next();
				String keyword = key.toString();
				if (keyword.equals("Implementation-Version")
						|| keyword.equals("Bundle-Version")) {
					versionNumber = (String) attributes.get(key);
					break;
				}
			}
		}
		jar.close();

		System.out.println("Version: " + versionNumber);
		return versionNumber;
	}
}








Previous
Next Post »