为产品库添加一个新jar包需要下面几个步骤:
-
根据jar包名字,从maven库里得到依赖定义字符串;
-
将该字符串加上配置说明和下载脚本(参见笔记“依赖管理常用vim脚本”);
-
将已下载条目备份文件(installed.txt)另存为old.bpk;
-
将build.xml中的下载条目插入到installed.txt中(按字母顺序排列);
-
用新的下载脚本替换build.xml中原有的下载脚本;
-
运行ant命令将jar包下载到产品库中;
-
在ivy.xml中加入依赖条目,解析之;
以下是上面1~5步的自动化脚本。
#-------------------------------------------------------------------------------
# Purpose: download jars to public repo
# Author: Li Chao
# Created: 28-06-2011
#
# Demonstration:
# install Apache Log4j 1.2
# 1. from mvnrepository.com we got the dep string:
#
# 2. modify dep_str in this file with above dep string:
# 3. run this file, copy modified dep string:
# # 4. open a shell and run ant cmd to download jars of log4j # 5. paste modified dep string to ivy.xml and resolve it #------------------------------------------------------------------------------- #!/usr/bin/env python import os, re dep_str = ' cmd_pattern = ' script = 'build.xml' def conv_str(): """ covert dep_str to custom_ivy_dep string and ivy_install string; """ custom_ivy_dep = dep_str.replace('/>',' conf="compile->master"/>') print 'Copy the following String:\n%s' %custom_ivy_dep keys = re.findall(r'"\S+"', dep_str) if len(keys)!=3: raise Exception('Format wrong of input string!') global ivy_install ivy_install = '<ivy:install organisation=%s module=%s revision=%s '\ def do_bpk(): """ create backup file(filename: installed.txt/old.bpk): """ installed = 'installed.txt' old_bpk ='old.bpk' xml_file = open(script) global xml_str xml_str = xml_file.read() xml_file.close() old_install = re.findall(cmd_pattern, xml_str, re.M)[0] print 'old install string is:\n%s' %old_install if os.path.exists(old_bpk): os.remove(old_bpk) if os.path.exists(installed): os.rename(installed, old_bpk) else: raise Exception('%s does not exist!'%installed) installed_file = open(old_bpk) installed_items = installed_file.read() installed_file.close() all_items = installed_items.split('\n') all_items.append(old_install) all_items.sort() bpk_file = open(installed,'w') bpk_file.write( '\n'.join(all_items) ) bpk_file.close() def build_script(): """ modify install script(build.xml): replace old install string with new one """ xml_file = open(script,'w') new_xml = re.sub(cmd_pattern, ivy_install, xml_str) xml_file.write(new_xml) xml_file.close() def main(): conv_str() do_bpk() build_script() if name == 'main': main() 代码中的技术点包括: 格式化字符串的生成方法:%后面必须接一个tuple; 如何使用正则表达式进行文本的搜索(findall)和替换(sub); 字符串的join方法进行list的组合,也可以用reduce方法: total_str = reduce(lambda x,y:x+y, str_list) 但用join方法更简洁。 'from="${from.resolver}" to="${to.resolver}"/>' %tuple(keys)
1 extract old install string from build.xml;
2 delete old.bpk if it exists;
3 rename installed.txt to old.bpk;
4 read old.bpk file, insert old install string and create installed.txt file;