Is the java-test-fixtures plugin incompatible with the Build and run using IntelliJ IDEA setting?

谁说胖子不能爱 提交于 2020-01-25 03:11:06

问题


It looks like if I use the java-test-fixtures plugin in my java Gradle project, when IntelliJ imports it the IDE will always mark the src/testFixtures dir as a Source Folder (visible in Module Settings, Sources tab).

Yet any dependencies I've added in my build.gradle to the testFixturesApi or testFixturesImplementation configurations are imported into the IDEA project with a Scope of Test (visible in Module Settings, Dependencies tab).

So when I go to build my Project, using the Project Build and run using setting of IntelliJ IDEA instead of the default of Gradle (visible in Settings -> Build, Execution, Deployment -> Build Tools -> Gradle), the builder can't find the deps from those testFixtures configurations and I get a bunch of package does not exist errors.

This behaviour can easily be reproduced after setting up a new java-library project using the gradle init wizard and adding the 'java-test-fixtures' plugin to it, then adding a dep to one of the test fixtures configuration and importing it in a java file in the test fixtures SourceSet, then switching the Build and run setting and clicking build.

I'm using Gradle 6.0 and IntelliJ 2019.3 BETA.

I've tried explicitly marking the testFixtures source dir as a Test Source folder:

idea {
    module {
        testSourceDirs += project.sourceSets.testFixtures.allJava.srcDirs
    }
}

But the IDEA Gradle import doesn't seem to honor this, and I suspect it's the wrong solution anyway.

Is there any way to get the java-test-fixtures plugin to play nice with the IDEA builder without leaking my deps from testFixturesApi configuration into the api configuration?

UPDATE

So my team has enabled the "gradle.settings.showDeprecatedSettings" option in the IntelliJ registry and is currently NOT using the recommended "Module per SourceSet" option - instead we have a module per project. This issue only applies with module per project, which is deprecated. See: https://youtrack.jetbrains.com/issue/IDEA-222172


回答1:


So I've found a workaround:

Add a new configuration:

configurations {
  ideaTestFixturesApi
}

Make the existing testFixturesApi configuration extend your new configuration:

configurations.testFixturesApi.extendsFrom(ideaTestFixturesApi)

Migrate all your dependencies from testFixturesApi -> ideaTestFixturesApi, at this point your Gradle build should once again run, and your IntelliJ build should remain broken in the same manner as before.

Apply the idea Gralde plugin and switch the scope of the new configuration to test

plugins {
   id 'idea'
}

// ...

idea {
    module {
       scopes.COMPILE.plus += [configurations.ideaTestFixturesApi]
    }
}

And then when you refresh your project via the Gradle view, you'll see every dep marked as COMPILE scope, which should allow the IntelliJ build to succeed. You can do the same thing for testFixturesImplementation.

Less than ideal, but as there doesn't seem to be any way to force the testFixtures sourceSet to show up as a test source directory, this is the workaround I'm going with for now.



来源:https://stackoverflow.com/questions/58963413/is-the-java-test-fixtures-plugin-incompatible-with-the-build-and-run-using-intel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!