Python打包Java项目脚本(三):打包Servlet项目并上传至服务器

使用Jenkins管理自然是很方便,但是有时候只是一个小项目仍然过于麻烦。于是写了一个Python脚本,实现Servlet项目打包并上传的脚本。

#!/bin/python 
# sys.argv[0] = makefile.py
# sys.argv[1] = project direcotry

import os,sys

#-------------------- Initialization ------------------------------
cwd="."                        # current working directory
srcDir = cwd + "/src"                  # source code directory
outDir = cwd + "/WebContent/WEB-INF/classes"   # output classes
seperator = ":"                        # semi-colon on windows
srcs = ["com.hao.register"]            # namespaces to be compiled
outwar="register.war"                  # output jar file name
# libraries refered
libs = [("/Users/hao/MyApp/tomcat8534/lib", ["servlet-api.jar"])]   

#-------------------- Formatting inputs ----------------------------
classpaths = srcDir
for item in libs:
    for i in range(len(item[1])):
        classpaths += seperator + os.path.join(item[0], item[1][i])

for i in range(len(srcs)):
    srcs[i] = os.path.join(os.path.join(srcDir, srcs[i].replace(".", "/"), "*.java"))
sourcefiles = " ".join(srcs) 

#--------- Generation of and execution compiling command -------------
os.chdir(os.getcwd())
cmdCompile = "javac -d {0} -cp {1} {2}".format(outDir, classpaths, sourcefiles)
print("Executing: " + cmdCompile)
os.system(cmdCompile)

cmdCompress = "jar cf {0} -C ./WebContent .".format(cwd + "/jars/"+outwar)
print("Executing: " + cmdCompress)
os.system(cmdCompress)

# baidu is the shortcut of the remote server defined in /.ssh/config
cmdUpload = "scp {0}/jars/{1} baidu:/var/lib/tomcat8/webapps".format(cwd, outwar)
print("Executing: " + cmdUpload)
os.system(cmdUpload)

print("Compiled.")

猜你喜欢

转载自blog.csdn.net/weixin_43145361/article/details/88988976
今日推荐