最近前台做的比较多,前台有个问题就是总需要部署项目。但是我们项目编译又慢,部署还老失败。为此,我结合一些朋友的工作,编了一个给自己使用的python小工具,可以一键编译部署整个项目,顺便把activeMq,jboss什么都启动。可惜还是没有能够改变我电脑cpu太垃圾的现实,部署还是容易遇到问题。
主要思路就是先修改参数,然后分别用子进程在新的终端中启动activemq和jboss,最后根据用户输入的参数来决定是否重新编译前后台并部署。这里前后台编译的脚本为了能够获取返回值我重写了一下,前面都一样,去掉了最后的等待。最后结合cmder的快捷键可以执行特定命令做到了真正的一键重新部署。
PS:下次我再也不把东西放到Programma File里面去了!!!中间的空格害死人。
下面贴下主要的代码:
build.py
# coding=UTF-8
import fileinput
import os
import glob
import argparse
import shutil
import subprocess
from multiprocessing import Process
# 这里是需要替换的常量
_TMS_PROJECT_DIR = "C:/company/project/tms"
_WEB_BUILD_BAT = "C:/company/project/build_prs_local_fully_M3_ext.bat"
_AE_BUILD_BAT = "C:/company/project/build_dom_local_fully_M3_ext.bat"
_JBOSS_DEPLOYMENTS_DIR = "C:/jboss-eap-6.4/standalone/deployments"
_SERVER_NAME = "zhoufr3-w11"
_TMS_ACTIVEMQ_XML = "C:/company/project/apache-activemq-5.10.0/conf/activemq.xml" # 只需要跑一次就好
_MODEL_DOM = "dom"
_MODEL_PRS = "prs"
_TMS_DOM_EAR = "WLS_DOM_SPS.ear"
_TMS_PRS_EAR = "WLS_PRS_SPS.ear"
_TMS_AE_SERVER_DIR = os.path.join(_TMS_PROJECT_DIR, "SS_AEServer/Application")
_TMS_WEB_SERVER_DIR = os.path.join(_TMS_PROJECT_DIR, "SS_WebServer/SS_WEB_Application")
_TMS_LOCAL_SERVER_XML = os.path.join(_TMS_PROJECT_DIR,
"SS_AEServer/Application/src/main/config/env_setting/localServer.xml")
def change_properties():
# 修改activemq的配置文件,只需要跑一次就好
read_file = open(_TMS_ACTIVEMQ_XML, "r", encoding='utf-8')
content = ''
for line in read_file:
if 'physicalName="LOCAL.' not in line:
line = line.replace('physicalName="', 'physicalName="LOCAL.')
if 'compositeTopic name="LOCAL.' not in line:
line = line.replace('compositeTopic name="', 'compositeTopic name="LOCAL.')
line = line.replace('10.111.1.233', _SERVER_NAME)
content += line
write_file = open(_TMS_ACTIVEMQ_XML, "w", encoding='utf-8')
write_file.write(content)
for line in fileinput.input(_TMS_LOCAL_SERVER_XML, inplace=True):
line = line.rstrip().replace('spsdemo-w10', _SERVER_NAME)
line = line.rstrip().replace('"%%FAKE_MIF_SENDER%%" value="true"', '"%%FAKE_MIF_SENDER%%" value="false"')
line = line.rstrip().replace('"%%AUTO_START_MESSAGE_RECEIVERS%%" value="false"',
'"%%AUTO_START_MESSAGE_RECEIVERS%%" value="true"')
print(line)
def build_tms_ae_server():
os.chdir(_TMS_AE_SERVER_DIR)
print_build_task("build_tms_ae_server", _AE_BUILD_BAT)
r = os.system(_AE_BUILD_BAT)
if r != 0:
raise Exception("error!")
print("-" * 30, "\n")
def build_tms_web_server():
os.chdir(_TMS_WEB_SERVER_DIR)
print_build_task("build_tms_web_server", _WEB_BUILD_BAT)
r = os.system(_WEB_BUILD_BAT)
if r != 0:
raise Exception("error!")
print("-" * 30)
def print_build_task(task_name, script_name):
print(task_name)
print("\tpath:", os.getcwd())
print("\tscripts:", glob.glob("*.bat"))
print("\tactive script:", script_name)
def copy_dom_to_jboss():
src = os.path.join(_TMS_AE_SERVER_DIR, "dist", _TMS_DOM_EAR)
dest = os.path.join(_JBOSS_DEPLOYMENTS_DIR, _TMS_DOM_EAR)
_delete(dest)
_copy(src, dest)
def copy_prs_to_jboss():
src = os.path.join(_TMS_WEB_SERVER_DIR, "dist", _TMS_PRS_EAR)
dest = os.path.join(_JBOSS_DEPLOYMENTS_DIR, _TMS_PRS_EAR)
_delete(dest)
_copy(src, dest)
def _delete(dest):
items = glob.glob(dest + "*")
for i in items:
print("delete:", i)
os.remove(i)
def _copy(src, dest):
print("copy:", src, " -> ", dest)
shutil.copyfile(src, dest)
def launch_activeMQ():
subprocess.call('python ./launch_activemq.py', creationflags=subprocess.CREATE_NEW_CONSOLE)
def launch_jboss():
subprocess.call('python ./launch_jboss.py', creationflags=subprocess.CREATE_NEW_CONSOLE)
def delete_dest():
if not args.models or _MODEL_PRS in args.models:
dom_dest = os.path.join(_JBOSS_DEPLOYMENTS_DIR, _TMS_DOM_EAR)
_delete(dom_dest)
if not args.models or _MODEL_DOM in args.models:
prs_dest = os.path.join(_JBOSS_DEPLOYMENTS_DIR, _TMS_PRS_EAR)
_delete(prs_dest)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Build And Deploy')
parser.add_argument('models', type=str, nargs='*', help='such as: dom prs')
parser.add_argument('-c', dest="not_copy",
action='store_true', help='not copy; 如果指定将不会删除,拷贝ear文件')
parser.add_argument('-b', dest="build_code",
action='store_false', help='not build code;如果指定将不会重新编译代码')
parser.add_argument('-j', dest="no_launch_jboss",
action='store_true', help='not launch jboss; 如果指定将不会启动jboss和activeMq')
args = parser.parse_args()
print(args)
if not args.no_launch_jboss:
# 如果启动jboss并且重新编译,需要先把老的包都删掉,否则会导致编译的时候jboss在加载类,无法删除旧的包
if not args.not_copy:
delete_dest()
change_properties()
process_activeMQ = Process(target=launch_activeMQ())
process_activeMQ.start()
process_jboss = Process(target=launch_jboss())
process_jboss.start()
if args.build_code:
if not args.models or _MODEL_DOM in args.models:
build_tms_ae_server()
if not args.not_copy:
if not args.models or _MODEL_DOM in args.models:
copy_dom_to_jboss()
if args.build_code:
if not args.models or _MODEL_PRS in args.models:
build_tms_web_server()
if not args.not_copy:
if not args.models or _MODEL_PRS in args.models:
copy_prs_to_jboss()
def test_delete_dest():
delete_dest()
launch_activemq.py
import os
_ACTIVEMQ_BAT = "C:/company/project/apache-activemq-5.10.0/bin/win64/activemq.bat"
if __name__ == '__main__':
os.system(_ACTIVEMQ_BAT)
launch_jboss.py
import subprocess
_JBOSS_BAT = "C:/Program Files/jboss-eap-6.4/bin/standalone.bat"
if __name__ == '__main__':
subprocess.Popen(_JBOSS_BAT)