Search code examples
eclipsemavenjunitm2eclipse

Eclipse junit tests using wrong version of maven jars


I have two projects that I compile and run together. These projects use different versions of certain jars. so I may have foo.1.15.jar and foo.1.16.jar both in my maven repo. The pom files correctly specify which project should use which jar, and I can run mvn verify to run all my integration tests without issue from the command line.

However, when I try to run a junit test from within eclipse I often run into errors with the wrong version of the jar being used. I believe that eclipse is always using the newest version of a jar that it finds within the maven repo, so if it sees foo.1.15 and foo.1.16 it will use the 16 version. The problem is that I want the 15 version, the tests fail if they run with the 16 version because it's not backwards compatible.

How do I make eclipse recognize that it needs to use an older version? In particular is there a way that I can make the junit tests recognize my pom file and utilize the correct jar versions specified within the pom?

I'm using m2eclipse if that is relevant.

edit: it looks like the problem is not an insistence on using the newest jar each time.

The problem is that my project A uses my project B. A uses foo.16 and B uses foo.15. the junit tests for A tries to use foo.16 even when it's running classes in B that are dependent on 15. However, I can't really change the dependencies, A needs 16 and B needs 15. Can I make eclipse understand this?


Solution

  • Your assumption about Eclipse always using the latest version of a Maven artifact is not correct.

    However, Eclipse only supports one classpath per project and cannot distinguish the Maven dependency scopes, so test scope dependencies are also on the Eclipse compile and runtime classpath. It's hard to tell if this is really related to your problem.

    You can open the POM editor and look at the Dependency Hierarchy tab to find out the (transitive) dependency path and the version used for each artifact. This may given you a clue for fixing your setup.