Home > Software engineering >  Gradle task to get list of jar dependecies
Gradle task to get list of jar dependecies

Time:12-29

Is there any gradle task to obtain list of runtime dependencies. Preferably built-in task or task from the 'application' plugin. Or if not possible, using another gradle plugin.

I simply need to manually run the main class of my app from the command line.

java -cp STRING_OF_ALL_JAR_DEPENDENCIES my.package.Main

STRING_OF_ALL_JAR_DEPENDENCIES should also include my project jar.

The task should produce STRING_OF_ALL_JAR_DEPENDENCIES in suitable formatting for use in --classpath/-cp switch BUT may also produce some other format that need some trivial processing before use.

CodePudding user response:

try

task runCP() {
    doLast {
        def runList = configurations.runtimeClasspath.asList()
        println(runList.join(File.pathSeparator))
    }
}

CodePudding user response:

Consider the startScripts task in the application plugin, as described here.

For example, in this project, we have the following dependencies:

dependencies {
    implementation 'com.codepoetics:protonpack:1.16'
    implementation 'com.google.guava:guava:24.1-jre'
    implementation 'org.apache.commons:commons-lang3:3.10'
    implementation 'org.springframework:spring-context:5.2.5.RELEASE'

    testImplementation 'junit:junit:4.12'
}

If we type ./gradlew startScripts then the file ./build/scripts/WarO_Java_17 contains this (newlines added):

CLASSPATH=$APP_HOME/lib/WarO_Java_17.jar
:$APP_HOME/lib/protonpack-1.16.jar
:$APP_HOME/lib/guava-24.1-jre.jar
:$APP_HOME/lib/commons-lang3-3.10.jar
:$APP_HOME/lib/spring-context-5.2.5.RELEASE.jar
:$APP_HOME/lib/jsr305-1.3.9.jar
:$APP_HOME/lib/checker-compat-qual-2.0.0.jar
:$APP_HOME/lib/error_prone_annotations-2.1.3.jar
:$APP_HOME/lib/j2objc-annotations-1.1.jar
:$APP_HOME/lib/animal-sniffer-annotations-1.14.jar
:$APP_HOME/lib/spring-aop-5.2.5.RELEASE.jar
:$APP_HOME/lib/spring-beans-5.2.5.RELEASE.jar
:$APP_HOME/lib/spring-expression-5.2.5.RELEASE.jar
:$APP_HOME/lib/spring-core-5.2.5.RELEASE.jar
:$APP_HOME/lib/spring-jcl-5.2.5.RELEASE.jar

There is a corresponding file for Windows in ./build/scripts/WarO_Java_17.bat

  • Related