0

Imagine I have two Maven-based projects with Kotlin code, prjA and prjB.

Test class SomeTest in prjA references a class and function defined in prjB:

class SomeTest {
    @Test
    fun prjACanReferencePrjBStuff() {
        val valRes = ValidationResult()
        val correctValRes = createCorrectValidationResult()
    }
}

When I

  1. run mvn clean install in prjB,
  2. update the dependencies of prjA in IntelliJ Idea and
  3. run mvn clean install in prjA,

I get errors - Maven can't find the classes defined in prjB:

Error in IntelliJ Idea

Why? How can I fix it?

Notes:

  1. Kotlin classes are publicly visible by default. I don't get any errors during mvn install of prjB.
  2. The Maven repository contains prjB artifacts and IntelliJ Idea references the right Maven repository.
  3. When I try to build prjA from the command line, the build succeeds.
  4. Invalidating IntelliJ Idea cache and rebuilding the project doesn't help.

Update 1: I need a solution, which allows me to use prjB not only in tests.

Update 2: Everything works perfectly fine, if I rewrite Kotlin classes in prjB in Java.

Glory to Russia
  • 17,289
  • 56
  • 182
  • 325

1 Answers1

1

If you need regular classes from prjB, add main dependency to prjA. If you need test classes from prjB, add test dependency to prjA.

To add main dependency to another module:

<project>
  ...
  <dependencies>
    <dependency>
      <groupId>com.your.group</groupId>
      <artifactId>prjB</artifactId>
      <version>1.0-SNAPSHOT</version>
    </dependency>
  </dependencies>
  ...
</project>

To add test dependency to another module:

<project>
  ...
  <dependencies>
    <dependency>
      <groupId>com.your.group</groupId>
      <artifactId>prjB</artifactId>
      <version>1.0-SNAPSHOT</version>
      <type>test-jar</type>
      <scope>test</scope>
    </dependency>
  </dependencies>
  ...
</project>

Here is an example on test dependency.

Community
  • 1
  • 1
  • See my update 1. I need to find a way to use `prjB` not only in tests, but in the normal code as well. Will you proposal work in this case? – Glory to Russia Jun 01 '16 at 05:13
  • See the first code snippet of doge, this will set a 'normal' dependency. – morpheus05 Jun 01 '16 at 06:58
  • How is the dependency from your first code snippet different from the dependency on `prjB` in my [example](https://github.com/dpisarenko/2016_06_01_kotlinMavenProblem/blob/master/prjA/pom.xml) ? – Glory to Russia Jun 01 '16 at 07:50
  • @DmitriPisarenko then it's problem in the IDE. Check [this](http://stackoverflow.com/questions/11454822/import-maven-dependencies-in-intellij-idea) – Abylay Sabirgaliyev Jun 01 '16 at 08:24