java Gradle:如何让 JavaExec 任务使用配置类路径?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13683613/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 13:35:29  来源:igfitidea点击:

Gradle: how to make JavaExec task use configuration classpath?

javagradlebuild-process

提问by Sergey Weiss

Here's the problem: I want to execute some java class with some dependencies from, say, runtime configuration. How can this be done?

问题是:我想执行一些 java 类,其中包含来自运行时配置的一些依赖项。如何才能做到这一点?

task runJava(type: JavaExec, dependsOn:[classes]) {
        main = 'mypackage.MyClass'
        classpath = //what should I write here to provide classes from runtime configuration?
}

回答by Benjamin Muschko

You will probably want to use the runtime classpath of your Source setswhich includes the compiled classes of your project as well as all the runtime dependencies.

您可能希望使用源集的运行时类路径,其中包括项目的已编译类以及所有运行时依赖项

task runJava(type: JavaExec, dependsOn:[classes]) {
    main = 'mypackage.MyClass'
    classpath = sourceSets.main.runtimeClasspath
}

In case you want to the get the path of a specific configuration you can do something like this: configurations.getByName('runtime').asPathor shorter configurations.runtime.asPath.

如果您想获取特定配置的路径,您可以执行以下操作:configurations.getByName('runtime').asPath或更短configurations.runtime.asPath