How to filter out a certain portion of ./gradlew project:dependencies command? (version 3)

Solution for How to filter out a certain portion of ./gradlew project:dependencies command? (version 3)
is Given Below:

This is an updated question for How to filter out a certain portion of ./gradlew project:dependencies command? (version 2)

I’m writing a routine that parses the dependencies{} portion of a build.gradle file, which may look like this

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"
}

Now I run a Jenkins pipeline (Groovy) script that does the following. The awk takes all the lines that start with “+” character in column 1, and has “com.groupId” string. What I’m calling “other data” are lines that neither have “+” in column 1, nor the “com.groupId” string, the awk script ignores those.

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")

My deps.txt shows

+ --- 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

and my List depList looks like

com.groupId:artifact0:1.0.0
com.groupId:artifact1:2.+
com.groupId:artifact2:3.+

which is exactly what I asked awk to do correct? Note the 4th dependency is not there, again because of my awk statement.

But how can I modify the awk script to get the actual version values, so my depList shows this? In other words, I want it to get the resolved versions obtained by the dependencies Gradle task.

com.groupId:artifact0:1.0.0
com.groupId:artifact1:2.1.0
com.groupId:artifact2:3.0.0

You may use this awk:

awk '!/^+.*com.groupId/ {next} {dep = $2}
NF >= 4 && $3 == "->" {sub(/:[^:]+$/, ":" $4, dep)} {print dep}' deps.txt

com.groupId:artifact0:1.0.0
com.groupId:artifact1:2.1.0
com.groupId:artifact2:3.0.0

Details:

  • we skip all lines that don’t start with + and contains com.groupId since we only want com.groupId dependencies
  • Store $2 in variable dep
  • If there are >= 4 fields and $3 is -> then substitute last after : with value of $4
  • Finally we print dep

This might be what you want:

$ awk -F'[ :]+' -v OFS=':' '$1=="+"{ sub(/ (.*/,""); print $3, $4, $(NF) }' deps.txt
com.groupId:artifact0:1.0.0
com.groupId:artifact1:2.1.0
com.groupId:artifact2:3.0.0

if you were just asking how to turn deps.txt from your question into the above output using awk.