简体   繁体   English

如何将 gradle 子项目源目录添加到配置/类路径

[英]How to add gradle subproject source dir to a configuration/classpath

I need to add a source path of a subproject to a Gradle configuration used to run the GWT code server via JavaExec task.我需要将子项目的源路径添加到用于通过 JavaExec 任务运行 GWT 代码服务器的 Gradle 配置。

All my tries fail whether nothing is added or the build fails.无论没有添加任何内容或构建失败,我所有的尝试都会失败。 I am quite new to Gradle and it seems I miss some crucial step.我对 Gradle 很陌生,似乎我错过了一些关键步骤。

Here's how a configuration is created and used:以下是配置的创建和使用方式:

configurations {
    gwtCodeServer
}

task runCodeServer(type: JavaExec) {
    classpath = configurations.gwtCodeServer
    doFirst { 
      classpath.each { println it} 

      // prints correct path in brackets: [C:/fullpath_to_subproject/src/main/java]
      println files( project(':subproject.name').sourceSets.main.java.srcDirs ).files
    }
}

and here's how I populate the configuration:这是我填充配置的方式:

dependencies {
    gwtCodeServer files(project.sourceSets.main.java.srcDirs) // OK
    gwtCodeServer files(project.sourceSets.main.resources.srcDirs) // OK
    gwtCodeServer files('C:/opt/gwt-2.9.0/gwt-codeserver.jar') // OK

    // OK (according to println classpath) but only for hardcoded path
    gwtCodeServer files('C:/fullpath_to_subproject/src/main/java') 

    // KO - adds nothing according to println classpath above
    gwtCodeServer files(project(':subproject.name').sourceSets.main.java.srcDirs) 

    // KO FAILS with 'Cannot convert the provided notation to an object of type Dependency'
    // even when println shows correct path (probably via .toString())
    gwtCodeServer files(project(':subproject.name').sourceSets.main.java.srcDirs).files 

}

I found the reason:我找到了原因:

In my project configuration the subprojects are initialized after computing of the dependencies of the main project, on next steps.在我的项目配置中,子项目在计算主项目的依赖关系后初始化,在接下来的步骤中。

As a result, in dependencies {} block, project(':subproject.name').sourceSets.main.java.srcDirs returns same path as of main project, project.sourceSets.main.java.srcDirs因此,在dependencies {}块中, project(':subproject.name').sourceSets.main.java.srcDirs返回与主项目project.sourceSets.main.java.srcDirs相同的路径

in runCodeServer.doFirst the subproject is already initialized so its source path was printed correctly there.runCodeServer.doFirst中,子项目已经初始化,因此其源路径已正确打印在那里。

Thus I will have to add all subproject source dirs in runCodeServer.doFirst closure instead of dependency {}因此,我将不得不在runCodeServer.doFirst闭包中添加所有子项目源目录,而不是dependency {}

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

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