Skip to content

Commit 0521fe7

Browse files
authored
Do not use compileClasspath as source of proto files (#631)
* Do not use compileClasspath as source of proto files * Add failing test case for #624 * Fix codenarc problems
1 parent e20df5b commit 0521fe7

File tree

2 files changed

+40
-5
lines changed

2 files changed

+40
-5
lines changed

src/main/groovy/com/google/protobuf/gradle/ProtobufPlugin.groovy

+17-5
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ import org.gradle.api.file.SourceDirectorySet
5252
import org.gradle.api.plugins.AppliedPlugin
5353
import org.gradle.api.provider.Provider
5454
import org.gradle.api.tasks.SourceSet
55+
import org.gradle.api.tasks.SourceSetContainer
56+
import org.gradle.api.tasks.TaskProvider
5557
import org.gradle.language.jvm.tasks.ProcessResources
5658
import org.gradle.util.GradleVersion
5759

@@ -237,12 +239,22 @@ class ProtobufPlugin implements Plugin<Project> {
237239
Configuration compileProtoPath, Collection<Closure> postConfigure) {
238240
Provider<ProtobufExtract> extractProtosTask =
239241
setupExtractProtosTask(sourceSet.name, protobufConfig)
240-
// In Java projects, the compileClasspath of the 'test' sourceSet includes all the
241-
// 'resources' of the output of 'main', in which the source protos are placed. This is
242-
// nicer than the ad-hoc solution that Android has, because it works for any extended
243-
// configuration, not just 'testCompile'.
242+
243+
// Pass include proto files from main to test.
244+
// Process resource task contains all source proto files from a proto source set.
245+
FileCollection testClassPathConfig = project.objects.fileCollection()
246+
if (Utils.isTest(sourceSet.name)) {
247+
TaskProvider<ProcessResources> mainProcessResources = project.tasks.named(
248+
project.extensions.getByType(SourceSetContainer)
249+
.getByName(SourceSet.MAIN_SOURCE_SET_NAME)
250+
.processResourcesTaskName,
251+
ProcessResources
252+
)
253+
testClassPathConfig.from(mainProcessResources)
254+
}
255+
244256
Provider<ProtobufExtract> extractIncludeProtosTask = setupExtractIncludeProtosTask(
245-
sourceSet.name, compileProtoPath, sourceSet.compileClasspath)
257+
sourceSet.name, compileProtoPath, testClassPathConfig)
246258
Provider<GenerateProtoTask> generateProtoTask = addGenerateProtoTask(
247259
sourceSet.name, protoSrcDirSet, project.files(extractProtosTask),
248260
extractIncludeProtosTask) {

src/test/groovy/com/google/protobuf/gradle/ProtobufJavaPluginTest.groovy

+23
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,29 @@ class ProtobufJavaPluginTest extends Specification {
9191
gradleVersion << GRADLE_VERSIONS
9292
}
9393
94+
@Unroll
95+
void "test generateTestProto should not execute :compileJava task (java-only project) [gradle #gradleVersion]"() {
96+
given: "project from testProject"
97+
File projectDir = ProtobufPluginTestHelper.projectBuilder('testProject')
98+
.copyDirs('testProjectBase', 'testProject')
99+
.build()
100+
101+
when: "build is invoked"
102+
BuildResult result = ProtobufPluginTestHelper.getGradleRunner(
103+
projectDir,
104+
gradleVersion,
105+
"generateTestProto"
106+
).build()
107+
108+
then: "it succeed"
109+
result.task(":generateTestProto").outcome == TaskOutcome.SUCCESS
110+
assert !result.output.contains("Task :classes")
111+
assert !result.output.contains("Task :compileJava")
112+
113+
where:
114+
gradleVersion << GRADLE_VERSIONS
115+
}
116+
94117
@Unroll
95118
void "testProject should be successfully executed (configuration cache) [gradle #gradleVersion]"() {
96119
given: "project from testProject"

0 commit comments

Comments
 (0)