1

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). 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). Now when I do that to run tests, I get a little yellow balloon:

Tests were not found in module "MyProject.main". Use module "MyProject.test" instead

I can train myself to right-click, Run 'All Tests' from the test-sources-root folder (src/test/java). But it's odd that IDEA would offer to do something that it can't do.

I wonder if I need to specify something differently/better in my build.gradle or possibly settings.gradle files? Or maybe adjust an IntelliJ setting somewhere? 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.

My entire project is checked into Github: TestUtils

If you don't want to click, here's my build.gradle file:

/*
 * 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:

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

rootProject.name = 'TestUtils'
GlenPeterson
  • 4,866
  • 5
  • 41
  • 49

2 Answers2

1

In a Maven project the same module contains both the source roots and the test source roots. 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.

You can disable the Create separate module per source set option and reimport the project:

Create separate module per source set

CrazyCoder
  • 389,263
  • 172
  • 990
  • 904
0

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. Deleting IntelliJ's output directory helped some.

Then one day, upgraded IDEA to:

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! as the Gradle docs instructed and re-imported the project into IDEA. This problem and all my other build problems went away. Which of those things really fixed my problem? I don't know.

GlenPeterson
  • 4,866
  • 5
  • 41
  • 49