diff --git a/src/main/groovy/org/inferred/gradle/ProcessorsPlugin.groovy b/src/main/groovy/org/inferred/gradle/ProcessorsPlugin.groovy index 49c95ee0..8614cfb3 100644 --- a/src/main/groovy/org/inferred/gradle/ProcessorsPlugin.groovy +++ b/src/main/groovy/org/inferred/gradle/ProcessorsPlugin.groovy @@ -111,15 +111,17 @@ class ProcessorsPlugin implements Plugin { }) }) - // If the project uses .idea directory structure, update compiler.xml directly - File ideaCompilerXml = project.file('.idea/compiler.xml') - if (ideaCompilerXml.isFile()) { - Node parsedProjectXml = (new XmlParser()).parse(ideaCompilerXml) - updateIdeaCompilerConfiguration(project, parsedProjectXml) - ideaCompilerXml.withWriter { writer -> - XmlNodePrinter nodePrinter = new XmlNodePrinter(new PrintWriter(writer)) - nodePrinter.setPreserveWhitespace(true) - nodePrinter.print(parsedProjectXml) + project.afterEvaluate { + // If the project uses .idea directory structure, update compiler.xml directly + File ideaCompilerXml = project.file('.idea/compiler.xml') + if (ideaCompilerXml.isFile()) { + Node parsedProjectXml = (new XmlParser()).parse(ideaCompilerXml) + updateIdeaCompilerConfiguration(project, parsedProjectXml) + ideaCompilerXml.withWriter { writer -> + XmlNodePrinter nodePrinter = new XmlNodePrinter(new PrintWriter(writer)) + nodePrinter.setPreserveWhitespace(true) + nodePrinter.print(parsedProjectXml) + } } } @@ -203,11 +205,18 @@ class ProcessorsPlugin implements Plugin { new Node(compilerConfiguration, "annotationProcessing") } + def outputDir = 'generated_src' + def testOutputDir = 'generated_testSrc' + if (project.hasProperty('idea')) { + outputDir = project.idea.processors.outputDir + testOutputDir = project.idea.processors.testOutputDir + } + compilerConfiguration.annotationProcessing.replaceNode{ annotationProcessing() { profile(default: 'true', name: 'Default', enabled: 'true') { - sourceOutputDir(name: project.idea.processors.outputDir) - sourceTestOutputDir(name: project.idea.processors.testOutputDir) + sourceOutputDir(name: outputDir) + sourceTestOutputDir(name: testOutputDir) outputRelativeToContentRoot(value: 'true') processorPath(useClasspath: 'true') } diff --git a/src/test/groovy/org/inferred/gradle/ProcessorsPluginFunctionalTest.groovy b/src/test/groovy/org/inferred/gradle/ProcessorsPluginFunctionalTest.groovy index d3875454..aac01f85 100644 --- a/src/test/groovy/org/inferred/gradle/ProcessorsPluginFunctionalTest.groovy +++ b/src/test/groovy/org/inferred/gradle/ProcessorsPluginFunctionalTest.groovy @@ -657,6 +657,54 @@ public class ProcessorsPluginFunctionalTest { assertEquals(expected, xml) } + @Test + public void testUserSpecifiedDirectoriesUsedInIdeaCompilerXml() throws IOException { + buildFile << """ + apply plugin: 'java' + apply plugin: 'idea' + apply plugin: 'org.inferred.processors' + + idea.processors { + outputDir = 'foo' + testOutputDir = 'bar' + } + """ + + new File(testProjectDir.newFolder('.idea'), 'compiler.xml') << """ + + + + + + + """.trim() + + File testProjectDirRoot = testProjectDir.getRoot() + GradleRunner.create() + .withProjectDir(testProjectDirRoot) + .withArguments("idea", "--stacktrace") + .build() + + def xml = testProjectDirRoot.toPath().resolve(".idea/compiler.xml").toFile().text.trim() + + def expected = """ + + + + + + + + + + + + + """.stripIndent().trim() + + assertEquals(expected, xml) + } + @Test public void testOnlyApplyToSubProject() { testProjectDir.newFolder("projectA") @@ -745,6 +793,98 @@ public class ProcessorsPluginFunctionalTest { assertAutoValueInFile(new File(runner.projectDir, ".factorypath")) } + /** @see https://github.com/palantir/gradle-processors/issues/28 */ + @Test + public void testIdeaCompilerConfigurationUpdatedWithoutNeedToApplyIdeaPlugin() throws IOException { + buildFile << """ + apply plugin: 'java' + apply plugin: 'org.inferred.processors' + """ + + new File(testProjectDir.newFolder('.idea'), 'compiler.xml') << """ + + + + + + + """.trim() + + File testProjectDirRoot = testProjectDir.getRoot() + GradleRunner.create() + .withProjectDir(testProjectDirRoot) + .withArguments("tasks", "--stacktrace") + .build() + + def xml = testProjectDirRoot.toPath().resolve(".idea/compiler.xml").toFile().text.trim() + + def expected = """ + + + + + + + + + + + + + """.stripIndent().trim() + + assertEquals(expected, xml) + } + + /** @see https://github.com/palantir/gradle-processors/issues/53 */ + @Test + public void testCompilerXmlModificationWhenIdeaPluginImportedLast() throws IOException { + buildFile << """ + apply plugin: 'java' + apply plugin: 'org.inferred.processors' + apply plugin: 'idea' + + idea.processors { + outputDir = 'foo' + testOutputDir = 'bar' + } + """ + + new File(testProjectDir.newFolder('.idea'), 'compiler.xml') << """ + + + + + + + """.trim() + + File testProjectDirRoot = testProjectDir.getRoot() + GradleRunner.create() + .withProjectDir(testProjectDirRoot) + .withArguments("idea", "--stacktrace") + .build() + + def xml = testProjectDirRoot.toPath().resolve(".idea/compiler.xml").toFile().text.trim() + + def expected = """ + + + + + + + + + + + + + """.stripIndent().trim() + + assertEquals(expected, xml) + } + private void assertAutoValueInFile(File file) { if (!file.any { it.contains("auto-value-1.0.jar") }) { println "====== $file.name ============================================================"