简体   繁体   English

IntelliJ IDEA Gradle 与 Maven 通过右键单击“sources-root”与“test-sources-root”文件夹来“运行所有测试”?

[英]IntelliJ IDEA Gradle vs. Maven "Run all tests" by right-clicking on `sources-root` vs. `test-sources-root` folder?

I just converted a Maven Java/Kotlin project to Gradle and it seems to be working well in general (it's my first time using Gradle).我刚刚将一个 Maven Java/Kotlin 项目转换为 Gradle,它似乎总体上运行良好(这是我第一次使用 Gradle)。 But I have a habit of right-clicking on my sources-root folder ( src/main/java ) to run both Rebuild '<default>' and Run 'All Tests' (at separate times).但我右键点击的习惯,在我的源根文件夹( src/main/java同时运行Rebuild '<default>' ,并Run 'All Tests' (在不同的时间)。 Now when I do that to run tests, I get a little yellow balloon:现在,当我这样做来运行测试时,我得到一个黄色的小气球:

Tests were not found in module "MyProject.main".在模块“MyProject.main”中未找到测试。 Use module "MyProject.test" instead改用模块“MyProject.test”

I can train myself to right-click, Run 'All Tests' from the test-sources-root folder ( src/test/java ).我可以训练自己右键单击,从 test-sources-root 文件夹( src/test/javaRun 'All Tests' But it's odd that IDEA would offer to do something that it can't do.但奇怪的是,IDEA 会提供做它不能做的事情。

I wonder if I need to specify something differently/better in my build.gradle or possibly settings.gradle files?我想知道我是否需要在我的build.gradle或可能的settings.gradle文件中指定不同/更好的东西? Or maybe adjust an IntelliJ setting somewhere?或者也许可以在某处调整 IntelliJ 设置? With a Maven project, I would suspect that I just need to tell IDEA to auto-refresh its project whenever I change the Maven one.对于 Maven 项目,我怀疑我只需要告诉 IDEA 在我更改 Maven 项目时自动刷新它的项目。

My entire project is checked into Github: TestUtils我的整个项目都签入了 Github: TestUtils

If you don't want to click, here's my build.gradle file:如果你不想点击,这里是我的 build.gradle 文件:

/*
 * This file was generated by the Gradle 'init' task.
 */

plugins {
    id 'java'
    id 'maven-publish'
    id 'org.jetbrains.kotlin.jvm' version '1.3.21'
}

repositories {
    mavenLocal()
    maven {
        url = 'http://repo.maven.apache.org/maven2'
    }
}

dependencies {
    compile 'junit:junit:4.12'
    compile 'javax.servlet:javax.servlet-api:4.0.1'
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
}

task sourcesJar(type: Jar) {
    classifier = 'sources'
    from(sourceSets.main.allJava)
}

task javadocJar(type: Jar) {
    classifier = 'javadoc'
    from(javadoc.destinationDir)
}

group = 'org.organicdesign.testUtils'
version = '0.0.8'
description = 'TestUtils'
sourceCompatibility = '1.8'

publishing {
    publications {
        maven(MavenPublication) {
            from(components.java)
            artifact(sourcesJar)
            artifact(javadocJar)
        }
    }
}

Here's settings.gradle:这是settings.gradle:

/*
 * This file was generated by the Gradle 'init' task.
 */

rootProject.name = 'TestUtils'

In a Maven project the same module contains both the source roots and the test source roots.在 Maven 项目中,同一个模块包含源代码根和测试源根。 In the Gradle project IDE is creating the separate modules for the source sets, so sources and test sources will be in the different modules.在 Gradle 项目中,IDE 正在为源集创建单独的模块,因此源和测试源将位于不同的模块中。

You can disable the Create separate module per source set option and reimport the project:您可以禁用为每个源集创建单独的模块选项并重新导入项目:

每个源集创建单独的模块

When I just unzipped Gradle in a folder, set GRADLE_HOME and added it to my path, I had all kinds of problems with IntelliJ not knowing what needed to be rebuilt.当我在文件夹中解压 Gradle,设置 GRADLE_HOME 并将其添加到我的路径时,我遇到了 IntelliJ 的各种问题,不知道需要重建什么。 Deleting IntelliJ's output directory helped some. 删除 IntelliJ 的输出目录有所帮助。

Then one day, upgraded IDEA to:然后有一天,将 IDEA 升级为:

IntelliJ IDEA 2019.1 (Ultimate Edition)
Build #IU-191.6183.87, built on March 27, 2019
JRE: 1.8.0_202-release-1483-b39 amd64
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
Linux 4.15.0-47-generic

Then I installed Gradle using SDKMAN!然后我使用SDKMAN安装了 Gradle as the Gradle docs instructed and re-imported the project into IDEA.按照Gradle 文档的指示,将项目重新导入到 IDEA 中。 This problem and all my other build problems went away.这个问题和我所有的其他构建问题都消失了。 Which of those things really fixed my problem?哪些事情真的解决了我的问题? I don't know.我不知道。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM