Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to parse a build.gradle file in java?

I need to parse the gradle build.gradle file in Java and list out all its dependencies(as List of Group:Artifact:Version) mentioned under dependencies block.

What would be the best possible solution for this?

build.gradle like below,

apply plugin: 'java-library'

repositories {
    jcenter()
}

dependencies {
    compileOnly group: 'org.slf4j', name: 'slf4j-log4j12', version: '1.7.25'
    compile group: 'com.google.code.findbugs', name: 'annotations' , version: '3.0.0'
    compile "com.google.code.gson:gson:2.8.5"
    compile "org.owasp:dependency-check-gradle:4.0.1"
    compile 'org.apache.httpcomponents:httpclient:4.5.7'
    api 'org.apache.commons:commons-math3:3.6.1'
    implementation 'com.google.guava:guava:21.0'
    testImplementation 'junit:junit:4.12'
}
like image 226
Sharanya M. Avatar asked Mar 27 '19 09:03

Sharanya M.


People also ask

How Gradle build works in Java?

The building process includes compiling, linking, and packaging the code. The process becomes more consistent with the help of build automation tools. It is popular for its ability to build automation in languages like Java, Scala, Android, C/C++, and Groovy.

What is Gradle build file?

Android Studio uses Gradle, an advanced build toolkit, to automate and manage the build process, while allowing you to define flexible custom build configurations. Each build configuration can define its own set of code and resources, while reusing the parts common to all versions of your app.

What is gradle tooling API?

Gradle provides a programmatic API called the Tooling API, which you can use for embedding Gradle into your own software. This API allows you to execute and monitor builds and to query Gradle about the details of a build.

How many types of gradle files are there?

Gradle Build Files Android Studio projects consists of one or more modules, which are components that you can build, test, and debug independently. Each module has its own build file, so every Android Studio project contains two kinds of Gradle build files.


1 Answers

  1. Create a custom gradle task to list all dependencies into a file.
  2. Hook this task to the very beginning of build process.
  3. Read that file from java
like image 198
Tamás Ozsváth Avatar answered Dec 10 '22 23:12

Tamás Ozsváth