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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With