Understand Java projects usually need the classpath to analyze correctly. This article describes how to get the Java classpath from Gradle and Maven Projects and import it into Understand.



Gradle Projects


The Python 3 script attached at the bottom of this article can be used to output the classpaths for a Gradle project.


If the Gradle executable is not on the path, then the path to the Gradle executable should be passed to the script using --gradleexe.


gradle_project_dir$ python3 gradlepaths.py --gradleexe ./gradlew > classpaths.txt

How does it work? The class path can be exported using:


gradlew dependencies --configuration compileClasspath

gradlew dependencies --configuration testCompileClasspath


Or, for a project with sub-directory projects:


gradlew <subdir>:dependencies --configuration compileClasspath

gradlew <subdir>:dependencies --configuration testCompileClasspath


However, the commands only list the jar files and not the complete path. The complete path to the jar file depends on the Gradle cache location. The cache location can be set with an environment variable GRADLE_USER_HOME and defaults to a .gradle folder in the user’s home directory.


The script assumes the Gradle project is in the current working directory and runs the commands above to get the list of jar files from the gradle project (or each sub-directory project). Then it uses the Gradle cache location to find the full paths to the jar files.


Alternatively, users can edit their Gradle configuration to add a command that prints the full paths to the jar files:


tasks.register('list') {

  dependsOn configurations.compileClasspath

  doLast {

    println "classpath = ${configurations.compileClasspath.collect { File file -> file.getAbsolutePath() }}"

  }

}


Maven Projects


For maven projects, the following command will print the class path:


mvn dependency:build-classpath > classpaths.txt


However, if there are submodules with interdependencies, then the project must also be compiled to avoid errors.


mvn compile dependency:build-classpath > classpaths.txt

It’s also possible to get the class path by compiling with -debug.


Importing into Understand


Understand can import the class path from a text file with one path per line. Open the project configuration dialog from Project->Configure Project. Expand the Java section in the lefthand panel and select the “Class Path” page. Use the Import button to select the text file with your class paths.