105
|
1 Title: Creating a plain Eclipse project using JPMS and JUnit tests
|
|
2 Date: 2020-07-27
|
|
3 Tags: JDK
|
|
4 Lang: en
|
|
5
|
|
6 I use the [Eclipse IDE](https://www.eclipse.org/ide/) as my daily driver for development. For a long time I have ignored the Java Platform Module System (JPMS) which was introduced in Java 9 and I'm still ignoring it today. If you want a proper module system simply [use the better one](https://www.osgi.org) that has existed for many, many years.
|
|
7
|
|
8 One point I've always put forward when arguing against the use of JPMS is that there is no way to create a simple project in Eclipse that uses the JPMS and has unit tests as part of the same project - much like the project setup that [Maven](http://maven.apache.org) uses. Only today I found out that this is indeed possible if you know how to configure your project and hack your way with the module system.
|
|
9
|
|
10 Let's start by creating a plain Java project in Eclipse. Make sure you have a JDK configured that supports JPMS and create separate output folders for sources and class files.
|
|
11 ![Eclipse new project wizard, first step]({static}/images/eclipse-jpms/NewProject1.jpg)
|
|
12
|
|
13 On the next page of the wizard make sure that "Create module-info.java" is checked.
|
|
14
|
|
15 ![Eclipse new project wizard, second step]({static}/images/eclipse-jpms/NewProject2.png)
|
|
16
|
|
17 When you hit *Finish* Eclipse will ask you for the module name of the project. Give your module a friendly name and start hacking away at your sources. Rather sooner than later you'll get to the point where you want to add tests for your code. Add a new source folder that will contain your tests. Configure the source folder for your tests to generate its class files into a different folder and make sure you trigger *Contains tests sources*.
|
|
18
|
|
19 ![Eclipse new project wizard, second step]({static}/images/eclipse-jpms/TestClassFolder.png)
|
|
20
|
|
21 In the *Libraries* tab add the JUnit library to *Classpath* section, not to the *Modulepath* section.
|
|
22 This hack enables the JUnit classes to be found for compiling and running the unit tests. But you're not required to put the JUnit module into `module-info.java`, leaking the JUnit dependency out into the module path. |