简体   繁体   English

gradle 通过命令行指定类路径 arguments

[英]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.我真的很想做./gradlew build -cp /$HOME/my/custom/testerLibrary.jar类的事情,但我在任何地方都找不到直接的答案。 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. 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?我应该创建 2 个项目,其中手动运行项目 A 以调试项目 B?

You can create two JARs from the same source code:您可以从相同的源代码创建两个 JARs:

  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:手动测试应用程序(上面列表中的 2 个)可以使用 Gradle 任务创建,该任务将所需的 JAR 附加到类路径:

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?如果您希望创建生产 JAR 和手动测试 JAR 的任务相同,除了手动测试 JAR 包含扩展相同类型的扩展创建行为如何完成此任务, 新任务类型的 Gradle 任务?


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.意见:从测试的角度来看,将 JAR 包含在手动测试应用程序中但不包含在生产应用程序中并不理想。 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?例如,如果 JAR 包含某些内容或更改了应用程序的行为,导致手动测试工作但生产应用程序由于缺少 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.根据 JAR 的性质,这可能是良性的,但在决定测试和生产应用程序会有所不同时,考虑这一点很重要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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