代码生成MANIFEST.MF文件

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/nayi_224/article/details/83989268

我在查询怎么把java项目打包成jar包时,很多文章都提到了MANIFEST.MF这个文件,但是对于怎么生成这个文件,都只是说了手写,手写,手写。真好意思啊。

于是我就写了一个简易版的自动生成代码,只生成了比较关键的部分。

lib信息取自.classpath文件,如果由于ide的原因没有这个文件或者格式不一样,本文方法不适用(我用的是myeclipse)。

package test;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Test {

	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		
		String mainClass = Test.class.getName();	//要启动的类
		
		System.out.println(new File(".classpath").exists());
		
		List<String> list = readFile(".classpath");
		
		Pattern pattern = Pattern.compile("path=\"(.*?)\"");
		Pattern patternCheck = Pattern.compile("kind=\"lib\"");
		Matcher matcher;
		
		String libStr = "";
		
		for(String str : list){
			
			matcher = patternCheck.matcher(str);
			if(!matcher.find())
				continue;
			
			matcher = pattern.matcher(str);
			if(matcher.find())
				libStr += matcher.group(1) + " ";
		}
		
		if(libStr == null || libStr.length() == 0)
			libStr += " ";
		
		File file = new File("MANIFEST.MF");
		file.delete();
		
		file.createNewFile();
		
		writeTxtFile(file, "Manifest-Version: 1.0");
		writeTxtFile(file, "Main-Class: " + mainClass);
		writeTxtFile(file, "Class-Path: " + libStr);
	}
	
    public static List<String> readFile(String path) throws IOException {
        List<String> list = new ArrayList<String>();
        FileInputStream fis = new FileInputStream(path);
        InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
        BufferedReader br = new BufferedReader(isr);
        String line = "";
        while ((line = br.readLine()) != null) {
            if (line.lastIndexOf("---") < 0) {
                list.add(line);
            }
        }
        br.close();
        isr.close();
        fis.close();
        return list;
    }
	
	public static boolean writeTxtFile(File file, String newStr) throws IOException {
		boolean flag = false;
		String filein = newStr + "\r\n";
		String temp = "";
 
		FileInputStream fis = null;
		InputStreamReader isr = null;
		BufferedReader br = null;
 
		FileOutputStream fos = null;
		PrintWriter pw = null;
		try {
			fis = new FileInputStream(file);
			isr = new InputStreamReader(fis);
			br = new BufferedReader(isr);
			StringBuffer buf = new StringBuffer();
 
			for (int j = 1; (temp = br.readLine()) != null; j++) {
				buf = buf.append(temp);
				buf = buf.append(System.getProperty("line.separator"));
			}
			buf.append(filein);
 
			fos = new FileOutputStream(file);
			pw = new PrintWriter(fos);
			pw.write(buf.toString().toCharArray());
			pw.flush();
			flag = true;
		} catch (IOException e) {
			throw e;
		} finally {
			if (pw != null) {
				pw.close();
			}
			if (fos != null) {
				fos.close();
			}
			if (br != null) {
				br.close();
			}
			if (isr != null) {
				isr.close();
			}
			if (fis != null) {
				fis.close();
			}
		}
		return flag;
	}

}

猜你喜欢

转载自blog.csdn.net/nayi_224/article/details/83989268