Gradle sourceSet depends on another source.

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

Besides the main SourceSet, I also have testenv SourceSet. The code in testenv SourceSet refers to the main code, so 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 source elements as dependencies. Recommended way to do this:

 sourceSets { testenv } dependencies { testenvCompile sourceSets.main.output } 

But this does not work correctly with eclipse, because when I clean the gradle build folder, eclipse no longer compiles, since it depends on the gradle build. Also, if I change the main code, I will have to rebuild the project in gradle for the changes to take effect in eclipse.

How do I declare dependencies correctly?

EDIT:

it

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

works for the main source, but since I am now linking to .java files, I miss the generated classes from Annotation-Processor :(

+4
source share
1 answer

So, after all this, the way:

 sourceSets { testenv } dependencies { testenvCompile sourceSets.main.output } 

To ensure proper operation with eclipse, you must manually exclude all outputs from sourceSet from the class path of eclipse. 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.") } } } } } } } } } 
+3
source

All Articles