我需要采用 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