@@ -132,6 +132,29 @@ trait GroovyModule extends JavaModule with GroovyModuleApi { outer =>
132132 groovyCompileTask()()
133133 }
134134
135+ def compileGroovyStubs : T [Result [Unit ]] = Task (persistent = true ){
136+ val groovySourceFiles = allGroovySourceFiles().map(_.path)
137+ val stubDir = compileGeneratedGroovyStubs()
138+ Task .ctx().log.info(s " Generating Java stubs for ${groovySourceFiles.size} Groovy sources to $stubDir ... " )
139+
140+ val compileCp = compileClasspath().map(_.path).filter(os.exists)
141+ val config = GroovyCompilerConfiguration (
142+ enablePreview = groovyCompileEnablePreview(),
143+ targetBytecode = groovyCompileTargetBytecode(),
144+ disabledGlobalAstTransformations = disabledGlobalAstTransformations()
145+ )
146+
147+ GroovyWorkerManager .groovyWorker().withValue(groovyCompilerClasspath()) {
148+ _.compileGroovyStubs(groovySourceFiles, compileCp, stubDir, config)
149+ }
150+ }
151+
152+ /**
153+ * Path to Java stub sources as part of the `compile` step. Stubs are generated
154+ * by the Groovy compiler and later used by the Java compiler.
155+ */
156+ def compileGeneratedGroovyStubs : T [os.Path ] = Task (persistent = true ) { Task .dest }
157+
135158 /**
136159 * The actual Groovy compile task (used by [[compile ]]).
137160 */
@@ -147,11 +170,21 @@ trait GroovyModule extends JavaModule with GroovyModuleApi { outer =>
147170
148171 val isGroovy = groovySourceFiles.nonEmpty
149172 val isJava = javaSourceFiles.nonEmpty
150- val isMixed = isGroovy && isJava
151-
152173 val compileCp = compileClasspath().map(_.path).filter(os.exists)
153174 val updateCompileOutput = upstreamCompileOutput()
154175
176+ sealed trait CompilationStrategy
177+ case object JavaOnly extends CompilationStrategy
178+ case object GroovyOnly extends CompilationStrategy
179+ case object Mixed extends CompilationStrategy
180+
181+ val strategy : CompilationStrategy = (isJava, isGroovy) match {
182+ case (true , false ) => JavaOnly
183+ case (false , true ) => GroovyOnly
184+ case (true , true ) => Mixed
185+ case (false , false ) => JavaOnly // fallback, though this shouldn't happen
186+ }
187+
155188 val config = GroovyCompilerConfiguration (
156189 enablePreview = groovyCompileEnablePreview(),
157190 targetBytecode = groovyCompileTargetBytecode(),
@@ -167,21 +200,14 @@ trait GroovyModule extends JavaModule with GroovyModuleApi { outer =>
167200 worker = jvmWorkerRef().internalWorker(),
168201 upstreamCompileOutput = updateCompileOutput,
169202 javaSourceFiles = javaSourceFiles,
170- compileCp = compileCp,
203+ compileCp = compileCp :+ compileGeneratedGroovyStubs() ,
171204 javaHome = javaHome().map(_.path),
172205 javacOptions = javacOptions(),
173206 compileProblemReporter = ctx.reporter(hashCode),
174207 reportOldProblems = zincReportCachedProblems()
175208 )
176209 }
177210
178- def compileGroovyStubs (): Result [CompilationResult ] = {
179- ctx.log.info(" Compiling Groovy stubs for mixed compilation" )
180- GroovyWorkerManager .groovyWorker().withValue(groovyCompilerClasspath()) {
181- _.compileGroovyStubs(groovySourceFiles, compileCp, classes, config)
182- }
183- }
184-
185211 def compileGroovy (): Result [CompilationResult ] = {
186212 ctx.log.info(
187213 s " Compiling ${groovySourceFiles.size} Groovy sources to $classes ... "
@@ -192,7 +218,7 @@ trait GroovyModule extends JavaModule with GroovyModuleApi { outer =>
192218 }
193219
194220 // TODO figure out if there is a better way to do this
195- val analysisFile = dest / " groovy.analysis.dummy" // needed for mills CompilationResult
221+ val analysisFile = dest / " groovy.analysis.dummy" // needed for Mills CompilationResult
196222 os.write(target = analysisFile, data = " " , createFolders = true )
197223
198224 workerGroovyResult match {
@@ -202,18 +228,19 @@ trait GroovyModule extends JavaModule with GroovyModuleApi { outer =>
202228 }
203229 }
204230
205- val firstAndSecondStage = if (isMixed) {
206- // only compile Java if Stubs are successfully generated
207- compileGroovyStubs().flatMap(_ => compileJava)
208- } else {
209- Result .Success
210- }
231+ strategy match {
232+ case JavaOnly =>
233+ compileJava
211234
212- if (isMixed || isGroovy) {
213- firstAndSecondStage.flatMap(_ => compileGroovy())
214- } else {
215- compileJava
235+ case GroovyOnly =>
236+ compileGroovy()
237+
238+ case Mixed =>
239+ compileGroovyStubs()
240+ .flatMap(_ => compileJava)
241+ .flatMap(_ => compileGroovy())
216242 }
243+
217244 }
218245
219246 private [groovylib] def internalCompileJavaFiles (
0 commit comments