我正在使用Perl编写一个脚本,该脚本应该能够生成给定项目的依赖项目列表。
依赖-树Perl脚本
#!/usr/bin/env perl
use strict;
#Read dependency-tree file and split the dependencies
sub readDependencyTreeFile(){
my ($dependencyTreeFile) = @_;
open my $fh, '<', $dependencyTreeFile or die "error opening $dependencyTreeFile: $!";
my $content = do { local $/; <$fh> };
$content= ~/^com.myProject.sample:(.*?):jar:$/;
return $content;
}
my $dependencyTreeFile = "./dependency-tree.txt";
my $content = readDependencyTreeFile($dependencyTreeFile);
print $content;Dependency-tree.txt
INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building myProject_10.5 10.5-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ myProject_10.5 ---
[INFO] com.myProject.sample:myProject_10.5:jar:10.5-SNAPSHOT
[INFO] \- com.myProject.sample:common_0.1:jar:0.1-SNAPSHOT:compile
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.594 s
[INFO] Finished at: 2017-06-13T13:21:29+05:30
[INFO] Final Memory: 18M/309M
[INFO] ------------------------------------------------------------------------预期输出
myProject_10.5
common_0.1但在我的例子中,regex并不像expected.Please那样工作,让我知道它哪里出了问题。$content= ~/^com.myProject.sample:(.*?):jar:$/;,这应该将maven dependency tree字符串过滤到上面的out中。
发布于 2017-06-13 14:06:52
不必在$content中使用整个文件,您可以处理文件中的每一行,并对每一行使用以下regexp:/^\[INFO\]\s.*?com\.myProject\.sample:([^:]+)/来捕获所需的依赖项,并在找到匹配时将它们连接起来。
你的潜艇应该是:
sub readDependencyTreeFile(){
my ($dependencyTreeFile) = @_;
open my $fh, '<', $dependencyTreeFile or die "error opening $dependencyTreeFile: $!";
my $content;
while (my $line = <$fh>) {
if ($line =~ /^\[INFO\]\s.*?com\.myProject\.sample:([^:]+)/) {
$content .= $1 . "\n";
}
}
return $content;
}这假定在任何给定行上只有一个“依赖项”,并且具有此类信息的行具有示例数据中提到的格式,即:
您还应该在use warnings;之后添加一个use strict;。
https://stackoverflow.com/questions/44516947
复制相似问题