Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dependency on a module on a multimodule gradle project

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?

like image 360
user_mda Avatar asked Sep 06 '19 19:09

user_mda


1 Answers

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.

like image 57
Chriki Avatar answered Sep 16 '22 17:09

Chriki