Knowledge Base > Cover CLI 3.0 > Passing the classpath
How to pass the classpath from Maven or Gradle to the CLI
- Generating the classpath from Maven
- Generating the classpath from Gradle
- Adding a JUnit dependency
- Passing the classpath to the CLI
Generating the classpath from Maven
Diffblue Cover will automatically detect and use the classpath for a Maven project if one is not provided by the user. If you wish to supply your own classpath to dcover
, follow these instructions.
While there is more than one way to generate the project’s full classpath (including dependencies) from Maven, one example is as follows:
When compiling the project, the output from Maven
should have indicated where the project’s class files were written to. This is typically at target/classes
within the directory containing the project’s pom.xml
file.
Next, run the mvn dependency:build-classpath
command from the directory containing the pom.xml
file for the module you wish to build.
This will print out a classpath, prefixed by Dependencies classpath
.
Append the target/classes
folder (or equivalent) as the first entry to the dependencies classpath: /home/myUser/myProject/target/classes:...
(where ...
represents the contents of the above dependencies classpath)
Generating the classpath from Gradle
While there is more than one way to generate a project’s full classpath (including dependencies) from Gradle, one example is as follows:
Append the following task at the end of your project’s (or module’s) build.gradle
file:
Gradle 4.0+:
task printClasspath {
doLast {
def cp = "${project.buildDir}/classes/java/main";
configurations.testRuntime.each { cp += (':'+it.toString()) };
println "Dependencies classpath: " + cp;
}
}
Gradle versions prior to 4.0:
task printClasspath {
doLast {
def cp = "${project.buildDir}/classes/main";
configurations.testRuntime.each { cp += (':'+it.toString()) };
println "Dependencies classpath: " + cp;
}
}
Execute the task by running gradle printClasspath
(or ./gradlew printClasspath
/ gradlew.bat printClasspath
if your project uses a Gradle Wrapper).
This will print out a classpath, prefixed by Dependencies classpath:
, containing the location of the project’s class files as the first entry.
For example:
$ gradle printClasspath
> Configure project :micro-core
Dependencies classpath: /home/myUser/projects/gradle/sample-boot-micro/micro-core/build/classes/java/main:/home/myUser/.gradle/caches/modules-2/files-2.1/com.h2database/h2/1.4.199/7bf08152984ed8859740ae3f97fae6c72771ae45/h2-1.4.199.jar
BUILD SUCCESSFUL in 752ms
Adding a JUnit dependency
Diffblue Cover requires a JUnit dependency to be passed in before it can generate and verify (compile and run) unit tests. Add this dependency to the end of your classpath.
This can be found by searching the following directories in the user’s home folder for a JUnit JAR file:
.m2/repository
(Maven)
.gradle/caches
(Gradle)
Passing the classpath to the CLI
Once the full classpath has been generated, it can be passed to the CLI via the -cp
argument:
-cp "/home/myUser/myProject/target/classes:..."
.