java-test-fixtures插件与IntelliJ IDEA设置与Build和运行不兼容吗? [英] Is the java-test-fixtures plugin incompatible with the Build and run using IntelliJ IDEA setting?

查看:417
本文介绍了java-test-fixtures插件与IntelliJ IDEA设置与Build和运行不兼容吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我在java Gradle项目中使用java-test-fixtures插件,则IntelliJ导入IDE时,它将始终将src/testFixtures目录标记为源文件夹(在模块设置"的源"选项卡中可见).

但是,我在build.gradle中添加到testFixturesApitestFixturesImplementation配置中的所有依赖项都被导入到IDEA项目中,其作用域为Test(在模块设置"的依赖项"选项卡中可见).

因此,当我要构建项目时,请使用Project Build并使用IntelliJ IDEA设置而不是默认的Gradle(在Settings-> Build,Execution,Deployment-> Build Tools-> Gradle中可见)运行),构建器无法从这些testFixtures配置中找到Dep,并且我得到了一堆不存在错误的软件包.

使用gradle init向导设置新的java-library项目并向其中添加'java-test-fixtures'插件,然后将dep添加到测试夹具配置之一中,然后可以很容易地重现此行为.将其导入测试夹具SourceSet中的Java文件中,然后切换构建并运行"设置并单击构建".

我正在使用Gradle 6.0和IntelliJ 2019.3 BETA.

我尝试显式将testFixtures源目录标记为Test Source文件夹:

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

但是IDEA Gradle的导入似乎并不能兑现这一点,而且我仍然怀疑这是错误的解决方案.

是否有任何方法可以使java-test-fixtures插件与IDEA构建器配合使用,而又不会将我的部门从testFixturesApi配置泄漏到api配置中?

更新

因此,我的团队已在IntelliJ注册表中启用了"gradle.settings.showDeprecatedSettings"选项,并且目前使用推荐的每个SourceSet的模块"选项-而是每个项目都有一个模块.此问题仅适用于每个项目中已弃用的模块.请参阅: https://youtrack.jetbrains.com/issue/IDEA-222172

解决方案

所以我找到了一种解决方法:

添加新配置:

configurations {
  ideaTestFixturesApi
}

使现有的testFixturesApi配置扩展您的新配置:

configurations.testFixturesApi.extendsFrom(ideaTestFixturesApi)

testFixturesApi-> ideaTestFixturesApi迁移所有依赖项,此时您的Gradle构建应再次运行,并且IntelliJ构建应保持与以前相同的方式.

应用idea Gralde插件并将新配置的范围切换为测试

plugins {
   id 'idea'
}

// ...

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

然后,当您通过Gradle视图刷新项目时,您会看到每个标记为COMPILE范围的dep,这将使IntelliJ构建成功.您可以为testFixturesImplementation做同样的事情.

不够理想,但是由于似乎没有任何方法可以强制testFixtures sourceSet显示为测试源目录,所以这是我现在要解决的方法.

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

解决方案

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.

这篇关于java-test-fixtures插件与IntelliJ IDEA设置与Build和运行不兼容吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆