2015-08-20 135 views
4

这个问题是类似于Make one source set dependent on another摇篮sourceSet依赖于另一个sourceSet

除了主要SourceSet我也有一个testenv SourceSet。 testenv SourceSet中的代码引用了主代码,因此我需要将主SourceSet添加到testenvCompile配置中。

sourceSets { 
    testenv 
} 

dependencies { 
    testenvCompile sourceSets.main 
} 

这不起作用,因为您不能直接添加sourceSets作为依赖项。推荐的方式做到这一点的是:

sourceSets { 
    testenv 
} 

dependencies { 
    testenvCompile sourceSets.main.output 
} 

但是,这并不与Eclipse正常工作,因为当我清理gradle这个build文件夹,日食不能再编译,因为它依赖于构建的gradle。 另外,如果我改变主代码,我不得不在gradle中重建项目,以使变更在eclipse中生效。

如何正确声明依赖关系?

编辑:

sourceSets { 
    testenv 
} 

dependencies { 
    testenvCompile files(sourceSets.testenv.java.srcDirs, sourceSets.testenv.resources.srcDirs) 
} 

作品为主要来源,而是因为我现在引用.java文件,我从注释处理器:(

回答

3
缺少生成的类

因此毕竟这是要走的路:

sourceSets { 
    testenv 
} 

dependencies { 
    testenvCompile sourceSets.main.output 
} 

为了使它在eclipse中正常工作,您必须手动排除eclipse classpath中的所有sourceSet输出。 这是丑陋的,但它适用于我:

Project proj = project 

eclipse { 
    classpath { 
    file { 
     whenMerged { cp -> 
     project.logger.lifecycle "[eclipse] Excluding sourceSet outputs from eclipse dependencies for project '${project.path}'" 
     cp.entries.grep { it.kind == 'lib' }.each { entry -> 
      rootProject.allprojects { Project project -> 
      String buildDirPath = project.buildDir.path.replace('\\', '/') + '/' 
      String entryPath = entry.path 

      if (entryPath.startsWith(buildDirPath)) { 
       cp.entries.remove entry 

       if (project != proj) { 
       boolean projectContainsProjectDep = false 
       for (Configuration cfg : proj.configurations) { 
        boolean cfgContainsProjectDependency = cfg.allDependencies.withType(ProjectDependency).collect { it.dependencyProject }.contains(project) 
        if(cfgContainsProjectDependency) { 
        projectContainsProjectDep = true 
        break; 
        } 
       } 
       if (!projectContainsProjectDep) { 
        throw new GradleException("The project '${proj.path}' has a dependency to the outputs of project '${project.path}', but not to the project itself. This is not allowed because it will cause compilation in eclipse to behave differently than in gradle.") 
       } 
       } 
      } 
      } 
     } 
     } 
    } 
    } 
}