Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to exclude a library from the eclipse default test classpath?

Tags:

java

eclipse

I have an interesting problem. I have two projects, which both depend on different versions of a library. The individual projects cannot change their dependencies, due to version constraints.

So:

Project A depends on lib version 1

Project B depends on lib version 2

lib version 2 is binary compatible with version 1, but has additional functionality so we don't want to update project A to depend on version2.

When running tests that only involve project A, then the class path is perfect, it only contains version 1 of lib.

When running tests that only involve project B, then the class path is perfect, it only contains version 2 of lib.

When running tests that involve both projects A and B, then the class path is broken, as it contains both version 1 and 2 of lib.

Is there any way to say "use this library for compiling only, and don't export for the runtime class path"? This is in some ways related (but opposite) to the exported flag on a JAR file, since that exports it to projects that you depend on as a compile dependency. I want to not export it as a runtime dependency.

When running from the command line this all works fine for us, it is only within Eclipse that we are seeing this problem.

like image 984
Paul Wagland Avatar asked Aug 04 '26 21:08

Paul Wagland


1 Answers

It is possible to specify a classpath for a junit launch configuration via the junit run configuration dialog. See here section "Customizing a Test Configuration": https://help.eclipse.org/neon/index.jsp?topic=%2Forg.eclipse.jdt.doc.user%2FgettingStarted%2Fqs-junit.htm

junit run configuration dialog (image from https://help.eclipse.org/neon/index.jsp?topic=%2Forg.eclipse.jdt.doc.user%2FgettingStarted%2Fqs-junit.htm)

The new version of the library needs to be added to the classpath before the default element so that during class loading the new library version will be seen first.

After you have configured the correct classpath export the launch configuration to the workspace via Export ... -> Launch Configurations. Then you will be able to run it with right clicking on it and selecting Run as -> <Your Lauch Config name>

like image 121
SpaceTrucker Avatar answered Aug 06 '26 10:08

SpaceTrucker



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!