本文介绍了Gradle sourceSet依赖于另一个sourceSet的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题类似于



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

  sourceSets {
testenv
}

依赖关系{
testenvCompile sourceSets.main
}

这不起作用,因为您无法直接将sourceSets添加为依赖项。推荐的方法是:

  sourceSets {
testenv
}

依赖关系{
testenvCompile sourceSets.main.output
}

在eclipse中无法正常工作,因为当我清理gradle build文件夹时,eclipse无法编译,因为它取决于gradle构建。
另外,如果我改变主代码,我必须在gradle中重建项目,以使这些更改在eclipse中生效。



如何声明依赖关系正确吗?



编辑:


  sourceSets {
testenv
}

依赖关系{
testenvCompile文件(sourceSets.testenv.java .srcDirs,sourceSets.testenv.resources.srcDirs)
}

适用于主要来源,但是因为我现在引用了.java文件,所以我从注释处理器中缺少生成的类:( p> )这是要走的路:

  sourceSets {
testenv
}

依赖关系{
testenvCompile sourceSets.main.output
}

使用eclipse正常工作,你必须手动排除eclipse classpath中的所有sourceSet输出。
这个i但它适用于我:

  Project proj =项目

eclipse {
classpath {
file {
whenMerged {cp - >
project.logger.lifecycle[eclipse]从项目'$ {project.path}'
cp.entries.grep {it.kind =='lib'}的eclipse依赖关系中排除sourceSet输出。每个{条目 - >
rootProject.allprojects {Project project - >
String buildDirPath = project.buildDir.path.replace('\\\','/')+'/'
String entryPath = entry.path

if( entryPath.startsWith(buildDirPath)){
cp.entries.remove条目

如果(项目!= PROJ){
布尔projectContainsProjectDep =假
为(CFG配置:proj.configurations){
布尔cfgContainsProjectDependency = cfg.allDependencies.withType(ProjectDependency).collect {} it.dependencyProject。载有(项目)
如果(cfgContainsProjectDependency){
projectContainsProjectDep = TRUE
休息; $ {
}

if(!projectContainsProjectDep){
throw new GradleException(项目'$ {proj.path}'依赖于项目'$ { project.path},但不是项目本身,这是不允许的,因为它会导致eclipse中编译的行为与gradle中的行为不同。)
}
}
}





$ b code $ pre

This Question is similar to Make one source set dependent on another

Besides the main SourceSet I also have a testenv SourceSet. The code in the testenv SourceSet references the main code, therefor I need to add the main SourceSet to the testenvCompile configuration.

sourceSets {
  testenv
}

dependencies {
  testenvCompile sourceSets.main
}

This does not work, because you cannot directly add sourceSets as dependencies. The recommended way to do this is:

sourceSets {
  testenv
}

dependencies {
  testenvCompile sourceSets.main.output
}

But this does not work correctly with eclipse, because when I clean the gradle build folder, eclipse can't compile anymore, since it depends on the gradle build.Also if I change main code I'd have to rebuild the project in gradle for the changes to take effect in eclipse.

How do I declare the dependencies correctly?

EDIT:

This

sourceSets {
  testenv
}

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

works for the main source, but because I now reference the .java files I am missing generated classes from the Annotation-Processor :(

解决方案

So after all this is the way to go:

sourceSets {
  testenv
}

dependencies {
  testenvCompile sourceSets.main.output
}

To make it work correctly with eclipse you have to manually exclude all sourceSet outputs from the eclipse classpath.This is ugly, but it works for me:

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.")
                }
              }
            }
          }
        }
      }
    }
  }
}

这篇关于Gradle sourceSet依赖于另一个sourceSet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-30 12:25