如何过滤掉./gradlew project:dependencies命令的某一部分? (版本 3)
How to filter out a certain portion of ./gradlew project:dependencies command? (version 3)
这是
的更新问题
我正在编写一个例程来解析 build.gradle
文件的 dependencies{}
部分,它可能看起来像这样
dependencies {
compile "com.groupId:artifact0:1.0.0"
compile "com.groupId:artifact1:2.+"
compile "com.groupId:artifact3:3.+"
compile "org.otherGroupId:artifact:1.0.0"
}
现在我 运行 一个执行以下操作的 Jenkins 管道 (Groovy) 脚本。 awk
获取第 1 列中以“+”字符开头的所有行,并具有“com.groupId”字符串。我所说的“其他数据”是第 1 列中既没有“+”也没有“com.groupId”字符串的行,awk
脚本会忽略这些。
sh "./gradlew project:dependencies | tee deps.txt"
List depList = sh(script: " awk -F '[: ]+' -v OFS=: '/^\+.*com\.groupId/ && !seen[$2,$3]++{print $2, $3, $4}' deps.txt",
returnStdout: true).split("\n")
我的deps.txt
显示
+ --- com.groupId:artifact0:1.0.0
... other data
+ --- com.groupId:artifact1:2.+ -> 2.1.0
... other data
+ --- com.groupId:artifact2:3.+ -> 3.0.0 (*)
... other data
+ --- org.otherGroupId:artifact:1.0.0
... other data
我的List depList
看起来像
com.groupId:artifact0:1.0.0
com.groupId:artifact1:2.+
com.groupId:artifact2:3.+
这正是我要求 awk
做的正确吗?请注意,第 4 个依赖项不存在,同样是因为我的 awk
语句。
但是我如何修改 awk
脚本来获取实际的版本值,所以我的 depList
显示了这个?换句话说,我希望它获得 dependencies
Gradle 任务获得的解析版本。
com.groupId:artifact0:1.0.0
com.groupId:artifact1:2.1.0
com.groupId:artifact2:3.0.0
您可以使用这个 awk
:
awk '!/^\+.*com\.groupId/ {next} {dep = }
NF >= 4 && == "->" {sub(/:[^:]+$/, ":" , dep)} {print dep}' deps.txt
com.groupId:artifact0:1.0.0
com.groupId:artifact1:2.1.0
com.groupId:artifact2:3.0.0
详情:
- 我们跳过所有不以
+
开头且包含 com.groupId
的行,因为我们只需要 com.groupId
依赖项
- 将
</code>存储在变量<code>dep
中
- 如果有 >= 4 个字段并且
</code> 是 <code>->
则将 :
之后的最后一个替换为 </code></li> 的值
<li>最后我们打印 <code>dep
这可能是您想要的:
$ awk -F'[ :]+' -v OFS=':' '=="+"{ sub(/ \(.*/,""); print , , $(NF) }' deps.txt
com.groupId:artifact0:1.0.0
com.groupId:artifact1:2.1.0
com.groupId:artifact2:3.0.0
如果您只是问如何使用 awk 将 deps.txt 从您的问题转换为上述输出。
这是
我正在编写一个例程来解析 build.gradle
文件的 dependencies{}
部分,它可能看起来像这样
dependencies {
compile "com.groupId:artifact0:1.0.0"
compile "com.groupId:artifact1:2.+"
compile "com.groupId:artifact3:3.+"
compile "org.otherGroupId:artifact:1.0.0"
}
现在我 运行 一个执行以下操作的 Jenkins 管道 (Groovy) 脚本。 awk
获取第 1 列中以“+”字符开头的所有行,并具有“com.groupId”字符串。我所说的“其他数据”是第 1 列中既没有“+”也没有“com.groupId”字符串的行,awk
脚本会忽略这些。
sh "./gradlew project:dependencies | tee deps.txt"
List depList = sh(script: " awk -F '[: ]+' -v OFS=: '/^\+.*com\.groupId/ && !seen[$2,$3]++{print $2, $3, $4}' deps.txt",
returnStdout: true).split("\n")
我的deps.txt
显示
+ --- com.groupId:artifact0:1.0.0
... other data
+ --- com.groupId:artifact1:2.+ -> 2.1.0
... other data
+ --- com.groupId:artifact2:3.+ -> 3.0.0 (*)
... other data
+ --- org.otherGroupId:artifact:1.0.0
... other data
我的List depList
看起来像
com.groupId:artifact0:1.0.0
com.groupId:artifact1:2.+
com.groupId:artifact2:3.+
这正是我要求 awk
做的正确吗?请注意,第 4 个依赖项不存在,同样是因为我的 awk
语句。
但是我如何修改 awk
脚本来获取实际的版本值,所以我的 depList
显示了这个?换句话说,我希望它获得 dependencies
Gradle 任务获得的解析版本。
com.groupId:artifact0:1.0.0
com.groupId:artifact1:2.1.0
com.groupId:artifact2:3.0.0
您可以使用这个 awk
:
awk '!/^\+.*com\.groupId/ {next} {dep = }
NF >= 4 && == "->" {sub(/:[^:]+$/, ":" , dep)} {print dep}' deps.txt
com.groupId:artifact0:1.0.0
com.groupId:artifact1:2.1.0
com.groupId:artifact2:3.0.0
详情:
- 我们跳过所有不以
+
开头且包含com.groupId
的行,因为我们只需要com.groupId
依赖项 - 将
</code>存储在变量<code>dep
中
- 如果有 >= 4 个字段并且
</code> 是 <code>->
则将:
之后的最后一个替换为</code></li> 的值 <li>最后我们打印 <code>dep
这可能是您想要的:
$ awk -F'[ :]+' -v OFS=':' '=="+"{ sub(/ \(.*/,""); print , , $(NF) }' deps.txt
com.groupId:artifact0:1.0.0
com.groupId:artifact1:2.1.0
com.groupId:artifact2:3.0.0
如果您只是问如何使用 awk 将 deps.txt 从您的问题转换为上述输出。