📄 makewar.java
字号:
package com.bluegrid.installanywhere;
/**
* -----------------------------------------------------
* Created by Blue using tools <IntelliJ IDEA> at Hust CGCL.
* Author: Blue (elychaser@hotmail.com)
* Date: 2004-8-22
* Time: 20:20:10
* -----------------------------------------------------
* Class Function: 专门用来打包,只要给出war 包路径
* -----------------------
* How to call:
* -----------------------
* Modify History:
* -----------------------
* Ideal GOAL: 给出一个配置war包,并完成封装的功能
* -----------------------------------------------------
*/
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.*;
import java.util.jar.JarEntry;
import java.util.jar.JarOutputStream;
import java.util.jar.Manifest;
public class MakeWAR extends JFrame implements ActionListener {
public String PKGNAME = "";
public JTextField warName;
public static void main(String args[]) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch (Exception e) {
e.printStackTrace();
}
new MakeWAR();
}
public String getPKGNAME() {
if (PKGNAME.trim().equals("")) {
PKGNAME = "";
}
return PKGNAME;
}
public void setPKGNAME(String PKGNAME) {
this.PKGNAME = PKGNAME;
}
public MakeWAR() {
super("WAR Maker");
warTextField = new JTextField(20);
warFileChooser = new JFileChooser();
warFileChooser.setFileSelectionMode(1);
//File warDir = new File("jive3");
File warDir = new File(PKGNAME);
if (warDir.exists()) {
try {
//返回代表 文件或者目录的 规范的( Canonical ) 的路径字串
warTextField.setText(warDir.getCanonicalPath());
//System.out.println("构建中定义的warDir = warDir.getCanonicalPath() = " + warDir.getCanonicalPath());
}
catch (Exception e) {
e.printStackTrace();
}
warFileChooser.setCurrentDirectory(warDir.getAbsoluteFile().getParentFile());
}
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
JPanel mainPanel = new JPanel();
mainPanel.setBorder(new EmptyBorder(3, 3, 3, 3));
mainPanel.setLayout(new BoxLayout(mainPanel, 1));
JPanel dirPanel = new JPanel();
dirPanel.setBorder(BorderFactory.createTitledBorder("Choose a Directory to Make Into a WAR"));
dirPanel.add(warTextField);
JLabel warTip = new JLabel("请输入war包名:");
warName = new JTextField(10);
JButton button = new JButton("Browse");
button.addActionListener(this);
dirPanel.add(button);
dirPanel.add(warTip);
dirPanel.add(warName);
JPanel panel3 = new JPanel();
panel3.setBorder(new EmptyBorder(5, 5, 5, 5));
panel3.setLayout(new BorderLayout());
JButton startButton = new JButton("Create WAR");
startButton.addActionListener(this);
panel3.add(startButton, "East");
mainPanel.add(dirPanel);
mainPanel.add(panel3);
getContentPane().add(mainPanel);
pack();
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
Dimension size = getSize();
setLocation((screen.width - size.width) / 2, (screen.height - size.height) / 2);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("Browse")) {
warFileChooser.setCurrentDirectory(new File(warTextField.getText()));
int returnVal = warFileChooser.showDialog(this, "Choose Directory");
if (returnVal == 0)
try {
warTextField.setText(warFileChooser.getSelectedFile().getCanonicalFile().getPath());
System.out.println("得到的war包路径 = " + warTextField.getText());
}
catch (Exception ex) {
ex.printStackTrace();
}
}
else if (e.getActionCommand().equals("Create WAR")) {
final File warDir = new File(warTextField.getText());
/*
try {
System.out.println("触发事件中定义的 warDir=" + warDir.getCanonicalPath());
}
catch (Exception e1) {
}
*/
if (!(new File(warDir, "WEB-INF")).exists()) {
String msg = "The directory you chose does not appear to be a valid\nweb application directory. Please choose another.";
JOptionPane.showMessageDialog(this, msg, "Error!", 0);
}
else {
String inputstr = this.warName.getText().trim();
System.out.println("inputstr = " + inputstr);
if (inputstr.equals("")) { //采用系统默认给出的包名
//get the war package name
String tmpStr = warTextField.getText().trim();
int p = tmpStr.lastIndexOf("\\"); //Windows 下面运行
if (p != -1) {
PKGNAME = tmpStr.substring(p + 1);
this.setPKGNAME(PKGNAME);
}
else {
this.setPKGNAME("WarPkg");
System.out.println("Error in get war pkg" + ",因此系统自动给出一个包名=" + "WarPkg.war");
}
System.out.println("默认的 WAR 包名:" + this.getPKGNAME() + " 结束");
}
else { // 采用用户输入的包名
//String str1 = this.warName.getText().trim();
this.setPKGNAME(inputstr);
System.out.println("用户自定义的 WAR 包名:" + this.getPKGNAME() + " 结束");
}
//-------------------------------------------------------
countFiles(warDir);
//设置进度条
progressBar = new JProgressBar(0, totalFileCount);
progressBar.setValue(0);
progressBar.setStringPainted(true);
setVisible(false);
getContentPane().removeAll();
JPanel panel = new JPanel();
panel.add(progressBar);
getContentPane().add(panel);
pack();
setVisible(true);
//JFrame frame = this;
Thread t = new Thread() {
public void run() {
try {
//File warFile = new File("jive3.war");
File warFile = new File(PKGNAME + ".war");
MakeWAR.makeZip(warDir, warFile);
String msg = "The WAR file was successfully written to: " + warFile.getCanonicalPath();
//JOptionPane.showMessageDialog(frame, msg, "WAR Completed", -1);
JOptionPane.showMessageDialog(null, msg, "WAR Completed", -1);
}
catch (Exception e) {
System.err.println(e);
}
System.exit(0);
}
};
t.start();
}
}
}
public static void makeZip(File directory, File zipFile)
throws IOException, FileNotFoundException {
jos = new JarOutputStream(new FileOutputStream(zipFile), new Manifest());
String fileNames[] = directory.list();
if (fileNames != null) {
for (int i = 0; i < fileNames.length; i++)
recurseFiles(new File(directory, fileNames[i]), "");
}
jos.close();
}
/**
* Func: 采用递归方法得到 制定文件目录下的 文件数目
* 实际返回数据存在变量 totalFileCount 里面
*
* @param file
*/
private static void countFiles(File file) {
if (file.isDirectory()) {
String fileNames[] = file.list();
if (fileNames != null) {
for (int i = 0; i < fileNames.length; i++)
countFiles(new File(file, fileNames[i]));
}
}
else {
totalFileCount++;
}
}
/**
* Func : 采用递归方法得到所有文件,
* 实际返回结果存在变量 JarOutputStream jos里面
*
* @param file
* @param pathName
* @throws IOException
* @throws FileNotFoundException
*/
private static void recurseFiles(File file, String pathName)
throws IOException, FileNotFoundException {
if (file.isDirectory()) {
pathName = pathName + file.getName() + "/";
System.out.println("PathName="+pathName);
jos.putNextEntry(new JarEntry(pathName));
String fileNames[] = file.list();
if (fileNames != null) {
for (int i = 0; i < fileNames.length; i++) {
recurseFiles(new File(file, fileNames[i]), pathName);
}
}
}
else {
JarEntry jarEntry = new JarEntry(pathName + file.getName());
//这里输出所有文件
System.out.println("FileName="+pathName + "" + file.getName());
FileInputStream fin = new FileInputStream(file);
BufferedInputStream in = new BufferedInputStream(fin);
jos.putNextEntry(jarEntry);
while ((len = in.read(buf)) >= 0)
jos.write(buf, 0, len);
in.close();
jos.closeEntry();
doneFileCount++;
//根据文件数目来设置进程条的进度
progressBar.setValue(doneFileCount);
}
}
private static JarOutputStream jos;
private static JProgressBar progressBar;
private JFileChooser warFileChooser;
private JTextField warTextField;
private static byte buf[] = new byte[1024];
private static int len;
private static int totalFileCount;
private static int doneFileCount;
//==========================================================================================
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -