Dependency on a module on a multimodule gradle project
Asked Answered
M

2

7

I have a multi-module project in Gradle.

I refactored the common functionality into a module named common.

I have tests in a different module (lets say module A) of the multi-module project that consume the classes under src/main/java of the common module.

I am able to import these classes from common module in test classes of module A, but when I run the tests, I get the following error:

error: package 'common.bla...' does not exist.

This is the build.gradle file for module A that depends on common module for its tests (I have tried all these options):

 dependencies  {
     compile project(':common')
     testCompile project(':common')
     testRuntime project(':common')
     runtime project(':common')
     implementation project(":common")
     testCompile 'junit:junit:4.12'
     testImplementation 'junit:junit:4.12'
     implementation 'junit:junit:4.12'
     testCompileOnly project(':common')
     testRuntimeOnly project(':common')
     testImplementation project(':common')
     runtimeOnly project(':common')
     testCompile project(":common").sourceSets.test.output
     compile project(":common").sourceSets.test.output
     testRuntime fileTree(dir: 'libs', include: ['*.jar'])
}

I have also verified that a jar is created in common/build/libs.
What else can I try?

Maurits answered 6/9, 2019 at 19:35 Comment(1)
could you add some screen how does your project structure look in IDE?Maceio
A
1

It’s a bit difficult to answer your question with so little context but let me try to do it anyway. From what I understand, you have a directory structure similar to the following (excluding the Gradle Wrapper files):

.
├── common
│   ├── build.gradle
│   └── src
│       └── main
│           └── java
│               └── common
│                   └── Foobar.java
├── moduleA
│   ├── build.gradle
│   └── src
│       └── test
│           └── java
│               └── FoobarTest.java
└── settings.gradle

I can successfully run ./gradlew :moduleA:test (Gradle 5.6.2) from the root directory with the following file contents:

./common/build.gradle

plugins {
    id 'java'
}

./common/src/main/java/common/Foobar.java

package common;

public class Foobar {
    public static void main(String... args) {
        System.err.println("Foobar");
    }
}

./moduleA/build.gradle

plugins {
    id 'java'
}

repositories {
    jcenter()
}

dependencies {
    testImplementation project(':common')
    testImplementation 'junit:junit:4.12'
}

./moduleA/src/test/java/FoobarTest.java

import common.Foobar;

public class FoobarTest {

    @org.junit.Test
    public void myTest() {
        org.junit.Assert.assertNotNull(Foobar.class);
    }
}

./settings.gradle

include 'common', 'moduleA'

As said, it’s hard to say where exactly your error comes from. If you can’t use my minimal setup to get your own code fixed, then maybe try to update your question with a minimal, reproducible example for your non-working setup.

Aylsworth answered 16/9, 2019 at 20:20 Comment(0)
S
0

The dependencies are convoluted right now. Having both compile and testCompile and testCompileOnly is confusing, if not downright incorrect. testCompileOnly may be the reason it’s not working at runtime.

Having compile implies runtime and testCompile as well. You can check that using gradlew common:dependencies.

I think cleaning up the dependencies will help. If not please check the contents of the jar to contain the expected class.

Straley answered 14/9, 2019 at 10:39 Comment(1)
Thanks, I did add just the testImplementation dependency but the jar does not contain the required classMaurits

© 2022 - 2024 — McMap. All rights reserved.