• 生成骨架时生成archetype.xml脚本

  1. 问题描述:没有方法通过*号批量匹配文件

    [INFO] ----------------------------------------------------------------------------

    [INFO] Using following parameters for creating project from Old (1.x) Archetype: gdpost-framework-archetype-client:1.0-SNAPSHOT

    [INFO] ----------------------------------------------------------------------------

    [INFO] Parameter: basedir, Value: D:\maven-cmd

    [INFO] Parameter: package, Value: mm

    [INFO] Parameter: groupId, Value: mm

    [INFO] Parameter: artifactId, Value: nn

    [INFO] Parameter: packageName, Value: mm

    [INFO] Parameter: version, Value: 1.0-SNAPSHOT

    [ERROR] ResourceManager : unable to find resource 'archetype-resources/src/main/resources/static/js/bsn/example/*.js' in any resource loader.

  2. 解决方法:

  1. python脚本:

    #!usr/bin/python
    # encoding: utf-8
    # -*- coding: utf-8 -*-
      
    import os  
    import sys  
    
    #创建archetype.xml文件
    #project_dir:项目目录 
    #artifactId:项目名称
    def create_archetype(argv):
    	projectDir = argv[0]
    	artifactId = argv[1]
    	real_project_dir = projectDir + "/src/main/resources/archetype-resources"
    	
    	if(os.path.exists(real_project_dir) == False):
    		print("目录:" + real_project_dir + "不存在")
    		return []
    	
    	#判断src\main\java目录是否存在
    	listsubdir = ["src/main/java","src/test/java","src/main/resources","src/test/resources"]
    	#listsubdir = ["src/main/java"]
    	#分配四个嵌套列表
    	listfiles = [list(),list(),list(),list()]
    	
    	for (index,subdir) in enumerate(listsubdir):
    		 #返回一个目录和文件列表
    		curdir = real_project_dir + "/" + subdir
    		if(os.path.exists(curdir) == False):
    			print("======目录:" + real_project_dir + "不存在,继续下一个目录=======")
    			continue
    		#递归获取所有文件
    		getAllfile2list(curdir,index,listfiles)
    	#根据listfiles写入文件
    	savetofile(projectDir,artifactId,listfiles)
    		
    	return listfiles
    
    #递归获取所有文件
    def getAllfile2list(startdir,curindex,listfiles):
    	
    	files = os.listdir(startdir)
    	for curfile in files:
    		curdir = startdir + "/" + curfile
    		#print(curdir)
    		#如果是目录则递归
    		if os.path.isdir(curdir) == True:
    			getAllfile2list(curdir,curindex,listfiles)
    	 #如果是文件则加入列表
    		if os.path.isfile(curdir) == True:
    			listfiles[curindex].append(curdir)
    
    #按段写入文件
    def savesegmenttofile(file,outkey,inkey,sublist):
    	keywork = "archetype-resources/"
    	
    	file.writelines("<" + outkey + ">\n")
    	
    	for sub in sublist:
    		if sub.find(keywork) >=0:
    			sub = sub.split(keywork)[1]
    		file.writelines("<" + inkey + ">" + sub + "</" + inkey + ">\n")
    	  
    	file.writelines("</" + outkey + ">\n")		
    	
    		
    #根据listfiles写入文件
    def savetofile(projectDir,artifactId,listfiles):
    	#文件写入开始
    	filename = projectDir + "/archetype_auto.xml"
    	print("#文件写入开始:" + filename)
    	file = open(filename,'w')
    	file.writelines("<archetype>\n")
    	file.writelines("<id>" + artifactId + "</id>\n")
    	
    	#src/main/java有值
    	outkeylist = ["sources","testSources","resources","testResources"]
    	inkeylist = ["source","resource"]
    	
    	#循环按段写入文件
    	for (index,sublist) in enumerate(listfiles):
    		if len(sublist) >= 0:
    			if index <= 1:
    				savesegmenttofile(file,outkeylist[index],inkeylist[0],sublist)
    			else:
    				savesegmenttofile(file,outkeylist[index],inkeylist[1],sublist)
    
    	file.writelines("</archetype>\n")
    	file.flush()
    	file.close()
    	print("#文件写入结束:" + filename)
    				
    if __name__ == '__main__':  
        listfiles = create_archetype(sys.argv[1:])
        #print(listfiles)


  2. 调用python生成archetype.xml:

    [D:\maven-cmd]$ python create_archetype.py D:/maven-cmd/ yxbj_maven_archetype  yxbj_maven_artifactId  #参数一:项目骨架所在的目录  参数二:artifactId

    ======目录:D:/maven-cmd/yxbj_maven_archetype/src/main/resources/archetype-resources不存在,继续下一个目录=======

    #文件写入开始:D:/maven-cmd/ yxbj_maven_archetype /archetype_auto.xml

    #文件写入结束:D:/maven-cmd/ yxbj_maven_archetype/ etype-client/archetype_auto.xml

  3. 生成的文件放在 archetype_auto.xml 存放在 项目骨架所在的目录


* 打折课程大放送
1. 【图解+笔记】Python从入门到进阶大合集详细讲解(含500条笔记):http://edu.51cto.com/sd/99cf7
2. 【笔记式】Python基础入门--八大数据结构篇(含200条笔记):http://edu.51cto.com/sd/f967d
3. 【笔记式】Python基础入门--语句函数篇(含150条笔记):http://edu.51cto.com/sd/eea85
4. 【图解+笔记】Python进阶-模块、包、面向对象篇(含200条笔记):http://edu.51cto.com/sd/43049
5. 【笔记式】Maven从入门到进阶大合集详细讲解(含200条笔记):http://edu.51cto.com/sd/09299
6. 【笔记式】Maven基础入门--坐标仓库私服(含100条笔记):http://edu.51cto.com/sd/e1387
7. 【笔记式】Maven高级进阶--插件测试属性配置(含100条笔记):http://edu.51cto.com/sd/f3fda