Gradle 渐变源集依赖于另一个源集

Gradle 渐变源集依赖于另一个源集,gradle,dependencies,source-sets,Gradle,Dependencies,Source Sets,这个问题类似于 除了主源集之外,我还有一个testenv源集。 testenv源集中的代码引用了主代码,因此我需要将主源集添加到testenvCompile配置中 sourceSets { testenv } dependencies { testenvCompile sourceSets.main } 这不起作用,因为不能直接将源集添加为依赖项。建议的方法是: sourceSets { testenv } dependencies { testenvCompile sou

这个问题类似于

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

sourceSets {
  testenv
}

dependencies {
  testenvCompile sourceSets.main
}
这不起作用,因为不能直接将源集添加为依赖项。建议的方法是:

sourceSets {
  testenv
}

dependencies {
  testenvCompile sourceSets.main.output
}
但是这在eclipse中不能正常工作,因为当我清理gradle构建文件夹时,eclipse不能再编译了,因为它依赖于gradle构建。 此外,如果我更改了主代码,我必须在gradle中重新构建项目,以使更改在eclipse中生效

如何正确声明依赖关系

编辑:

这个


适用于主源代码,但由于我现在引用了.java文件,因此我缺少从注释处理器生成的类:(

因此,毕竟这是一种方法:

sourceSets {
  testenv
}

dependencies {
  testenvCompile sourceSets.main.output
}
要使它在eclipse中正常工作,您必须手动从eclipse类路径中排除所有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.")
                }
              }
            }
          }
        }
      }
    }
  }
}

我偶然发现了这个问题,并在写这篇文章之前进行了几个小时的实验,这篇文章可能会解决这样的问题:
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.")
                }
              }
            }
          }
        }
      }
    }
  }
}