前面已经说了打jar包,那怎么打成war包呢?
打成war包会把lib中的jar也加进来,虽然导致war文件有点大,但贵在一个文件,比较方便.
打war包命令也是直接用jar即可:
jar -cvf myh.war *
先看工程的结构:
│ build.py
│
├─build
├─conf
│ log4j.xml
│
├─lib
│ jsp-api.jar
│ log4j.jar
│ servlet-api.jar
│
├─src
│ └─com
│ └─yp
│ └─test
│ └─servlet
│ MyServlet.java
│
└─WebContent
│ index.jsp
│
└─WEB-INF
web.xml
只有一个servlet
package com.yp.test.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
/**
* Servlet implementation class MyServlet
*/
public class MyServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
static Logger log = Logger.getLogger(MyServlet.class);
/**
* Default constructor.
*/
public MyServlet() {
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
log.info("hello!!!!!log4j");
response.sendRedirect("index.jsp");
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
}
web.xml增加:
<servlet>
<description></description>
<display-name>MyServlet</display-name>
<servlet-name>MyServlet</servlet-name>
<servlet-class>com.yp.test.servlet.MyServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MyServlet</servlet-name>
<url-pattern>/MyServlet</url-pattern>
</servlet-mapping>
build.py代码:
#!/usr/bin/python
#coding:UTF-8
import paramiko,datetime,os,logging
import os,shutil
import sys
import time
# 遍历文件侠
def getFiles(dir, suffix):
res = []
for root, directory, files in os.walk(dir):
for filename in files:
name, suf = os.path.splitext(filename)
if suf == suffix:
res.append(os.path.join(root, filename))
return res
# 一些变量
jarFileName = "myh.jar"
webFold = "WebContent"
basedir = os.getcwd()
# clean,init some path
logging.error("current workplace:"+os.getcwd()+",clean build")
if os.path.exists(basedir+"\\"+"build"):
shutil.rmtree(basedir+"\\"+"build")
os.mkdir(basedir+"\\"+"build")
os.mkdir(basedir+"\\"+"build\\classes")
# compile
logging.error("begin compile")
# 把文件列表加入到sourcefiles 文件中
file=open('sourcefiles','w')
for tfile in getFiles(basedir+'/src/', '.java'):
file.writelines (tfile+'\n')
file.close()
# 执行编译
os.chdir(basedir)
result = os.system("javac -encoding utf8 -sourcepath src @sourcefiles -d build/classes -Djava.ext.dirs=./lib ")
# 判断编译结果
if result!=0:
logging.error('javac error')
os.system("pause")
sys.exit()
# logging.error("开始copy配置文件")
os.system("cp conf/* build/classes")
# copy lib和classes
os.system('cp -r lib '+basedir+'/'+webFold+'/WEB-INF/')
os.system("cp -r "+basedir+"/build/classes "+basedir+"/"+webFold+"/WEB-INF/")
# war
os.system("jar -cvf myh.war -C "+webFold+"/ .")
# clean
os.remove(basedir+"/sourcefiles")
os.system("pause")
测试:
放到tomcat后,启动tomcat 启动
http://127.0.0.1:8080/myh/MyServlet 就可以看到效果了.
说明:
- WEB-INF是 javaee的标准结构,里面有个web.xml, tocmat等jee容器即认.
- 打war包比打jar包要简单,毕竟不用去管可运行文件和清单文件,只需要把jar扔到 WEB-INF/lib中即可。
- javac命令中,没有用 classpath了,而是用 -Djava.ext.dirs=./lib ,用classpath选项的话,就要生成多个jar包的路径到 -cp后. 用 -Djava.ext.dirs 是有风险的,请参考http://blog.csdn.net/scugxl/article/details/43240991
- 我是先把java文件编译成class文件放到build/classes中,然后把文件夹cp到 WEB-INF下
- 打成war 包下面的清单文件貌似没啥用。
- 需要两个入参:jarFileName,webFold
- 代码:https://github.com/huawumingguo/javacmd/