Jack_Lue的博客

编程是一门艺术,带你用新的思维看世界

用python脚本更新NetBeans项目

最近拿到一个NetBeans的示例代码包,是用很老版本的NetBeans创建,在导入NetBeans 8 时出现大量警告,由于项目太多,手动修复太麻烦,于是用python编写了一个简单脚本修复警告,主要修复内容包括以下两个方面:

  • 第一个警告是项目缺少 test-roots 节点,打开项目文件project.xml 可以发现旧版本项目确实没有改节点,只有一个source-roots 节点,修复的方法是在source-roots 节点下面插入如下内容:
<test-roots>
    <root id="src.dir"/>
</test-roots>
  • 第二个警告是在编译运行时产生的,是说没有正确地指定Java源,看来是Java的问题,查看配置文件project.properties 发现有如下两项配置:
javac.source=1.2
javac.target=1.2

显然这是指定了Java编译器的版本,我们只需要把版本号改为自己所用Java版本即可,实际上如果只有一两个项目,那么用文本编辑器直接打开这两个文件按上述方法修改即可,但是如果项目比较多,比如是一个大的项目组,那么就需要用脚本自动化操作了,废话不多说,直接附上python脚本(测试环境 Win10/Python 3.5):

import os
import xml.dom.minidom

'''
This script is used to update the Netbeans project.
Importing the projects created by an older version to a new version NetBeans
will generate some warnings. These warnings can be fixed by running this script
in the root directory of project or project group.

@Date:2018-5-15 10:50:15
@Creator:Jack
@Version:1.0

'''

def detect_walk(dir_path):
    '''
    Traverse all the project files in dir_path,
    and update the Netbeans project
    '''
    for root, dirs, files in os.walk(dir_path):
        for filename in files:        
            if filename == "project.xml":  # update project.xml
                if update_project_xml(root + "\\" + filename):
                    print("Update Project:" + root)
            if filename == "project.properties":  # update project.properties
                update_properties(root + "\\" + filename)
                print("Update Property:" + root)

def update_project_xml(xml_path):
    '''
    Update the file project.xml:
    Add a new xml child node <test-root> for node <data> 
    '''
    DOMTree = xml.dom.minidom.parse(xml_path)
    project = DOMTree.documentElement
    data = project.getElementsByTagName("data")[0]
    if data.getElementsByTagName('test-roots'):   # the node 'test-roots' exists
        return 0
    test_root = DOMTree.createElement("test-roots")
    sub_root = DOMTree.createElement("root")
    sub_root.setAttribute("id", "src.dir")
    test_root.appendChild(sub_root)
    data.appendChild(test_root)

    # Landscaping xml format
    DOMStr = DOMTree.toprettyxml(indent = '', newl = '')
    DOMStr = DOMStr.replace(4*' ', '').replace('\n', '').replace('\t', '')
    DOMTree = xml.dom.minidom.parseString(DOMStr)

    # Update project.xml
    with open(xml_path, 'w') as f:
        f.write(DOMTree.toprettyxml(indent = '\t'))

    return 1

def update_properties(property_path):
    '''
    Update the java properties file project.properties:
    Change the properties javac.source = 1.8, javac.target = 1.8
    Please replace "1.8" with your java version
    '''
    with open(property_path,"r") as f:
        lines = f.readlines()
    with open(property_path,"w") as f:
        for line in lines:
            property = line.split('=',1)
            if property[0] == "javac.source" or property[0] == "javac.target":
                line = property[0] + '=1.8\n' # replace "1.8" with your java version
            f.write(line)



if __name__ == "__main__":
    detect_walk(".")
阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Jack_Lue/article/details/80321039
文章标签: NetBeans Java Python
个人分类: Java
上一篇用户及文件权限管理
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭