0

我需要采用 maven 依赖树并删除页眉和页脚,以便只留下依赖信息。下面是一个示例树。

[INFO] Scanning for projects...
[INFO] 
[INFO] ----------------------------< brs:libadept >---------------------------
[INFO] Building libadept 0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]--------------------------------
[WARNING] The artifact org.apache.commons:commons-io:jar:1.3.2 has been relocated to commons-io:commons-io:jar:1.3.2
[INFO] 
[INFO] --- maven-dependency-plugin:2.10:tree (default-cli) @ libadept ---
[WARNING] The artifact org.apache.commons:commons-io:jar:1.3.2 has been relocated to commons-io:commons-io:jar:1.3.2
[INFO] brs:libadept:jar:0.1-SNAPSHOT
[INFO] +- org.antlr:antlr4:jar:4.7.1:compile
[INFO] |  +- org.antlr:antlr4-runtime:jar:4.7.1:compile
[INFO] |  \- com.ibm.icu:icu4j:jar:58.2:compile
[INFO] +- brs:libutil8:jar:1.17.3:compile
[INFO] |  \- com.google.code.gson:gson:jar:2.7:compile
[INFO] \- org.slf4j:slf4j-log4j12:jar:1.7.25:compile
[INFO]    \- org.slf4j:slf4j-api:jar:1.7.25:compile
[INFO] -----------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] -----------------------------------------------------------------------
[INFO] Total time:  2.760 s
[INFO] Finished at: 2019-07-31T14:26:14-07:00
[INFO] -----------------------------------------------------------------------

到目前为止,我已经尝试过:

import os
myCmd = 'mvn dependency:tree > new.txt'
os.system(myCmd)

fileObj = open("new.txt", "r")
fileOut = open("bulk.txt", "w")


def getBulk(text):
    for line in text.readlines():
        if line.startswith("[INFO] +- ") or line.startswith("[INFO] | ") or line.startswith("[INFO] \\\- ") or line.startswith("[INFO]    \\\- "):
            fileOut.write(line)
            return fileOut

print(getBulk(fileObj))

我希望能够得到这个信息:

[INFO] +- org.antlr:antlr4:jar:4.7.1:compile
[INFO] |  +- org.antlr:antlr4-runtime:jar:4.7.1:compile
[INFO] |  \- com.ibm.icu:icu4j:jar:58.2:compile
[INFO] +- brs:libutil8:jar:1.17.3:compile
[INFO] |  \- com.google.code.gson:gson:jar:2.7:compile
[INFO] \- org.slf4j:slf4j-log4j12:jar:1.7.25:compile
[INFO]    \- org.slf4j:slf4j-api:jar:1.7.25:compile

但我越来越

<_io.TextIOWrapper name='bulk.txt' mode='w' encoding='cp1252'>

我必须确保考虑到页眉和页脚可能并不总是相同的行数(例如,以“[WARNING]”开头的行可能并不总是存在,所以我不能只编写这样的程序前十行被删除)。提前致谢!

最终答案:

import os
myCmd = 'mvn dependency:tree > new.txt'
os.system(myCmd)
fileObj = open("new.txt", "r").readlines()
fileOut = open("bulk.txt", "w")

def getBulk(text):
    for line in text:
        if line.startswith("[INFO] +- ") or line.startswith("[INFO] | ") or line.startswith("[INFO] \- ") or line.startswith("[INFO]    \- "):
            fileOut.write(line)
            print(line)
    return fileOut
4

1 回答 1

0
import os

mvn_dep_tree_cmd = 'mvn dependency:tree > new.txt'
os.system(mvn_dep_tree_cmd)

file_in = open("new.txt", "r")
file_out = open("bulk.txt", "w")

# flag denoting that current line is inside a tree
in_tree = False

for line in file_in.readlines():
    # this condition denotes the start of a tree
    if line.startswith("[INFO] +- "):
        in_tree = True
    if in_tree:
        # if we are inside a tree the following condition denotes end of the tree
        if line.strip() == '[INFO]':
            in_tree = False
            file_out.write('\n')
            file_out.write('\t<---------------------------------------------->')
            file_out.write('\n\n')
            print(line)
        else:
            # writing to the out file all the contents inside the tree
            file_out.write(line)
            print(line)

file_in.close()
file_out.close()
于 2019-08-01T18:39:08.817 回答