简体   繁体   中英

gradle specify classpath via command line arguments

I would really like to do something like ./gradlew build -cp /$HOME/my/custom/testerLibrary.jar but I cannot find a straight answer anywhere. I have a special case where I need to add a jar to the class path because the class that I use for manual testing requires it, but nothing else in my project needs it and I don't want to include it in my build.gradle.

Should I create 2 projects where project A is run manually to debug project B?

You can create two JARs from the same source code:

  1. the production application
  2. the manual-testing application

The manual-testing application (2 in the list above) could be created using a Gradle task that appends the required JAR to the classpath:

task createManualTestingApp(type: JavaExec) {
    classpath += "/$HOME/my/custom/testerLibrary.jar"
    // ...other configuration...
}

If you want the task that creates the production JAR and the manual-testing JAR to be identical, except that the manual testing JAR contains the augmented classpath, this can be done by creating two tasks that extend the same type: How to extend the behavior of a Gradle task for a new task type?


Opinion : From a testing perspective, it is not ideal to have a JAR that is included in the manual-testing application but not in the production application. This results in a test application that is different than the production application. For example, what if that JAR includes something or changes the behavior of the application that causes the manual tests to work but the production application fails because it is missing that JAR? Depending on the nature of the JAR, this may be benign, but it is important to consider when deciding that the test and production applications will be different.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM