This is a sandbox app created to test out Java 9' Project Jigsaw with Maven functionality.
For Project Jigsaw, without maven, check this tutorial: http://openjdk.java.net/projects/jigsaw/quick-start
For reference, I have also added Java 8 app to do the similar thing.
I have used simple Java Reflection API and not the ServiceLoader since I wanted to pass parameters and not use default constructors or Abstract Factories.
- Created an
interface Shape
- Created two implementations
class Circle
andclass Square
ofinterface Shape
. Main App
should havecompile-time
dependency oninterface Shape
, butruntime
dependency on implementations.
- API
- Module which has
interface Shape
and default implementationclass Square
.
- Module which has
- IMPL
- Module has
compile-time
dependency on API. - This module has
class Circle
which has custom implementation ofinterface Shape
.
- Module has
- APP
- This module has the
Main
class. - It will use
class ShapeFactory
in API, which will create objects ofCircle
andSquare
using Java Reflection API (wanted to keep things simple and avoid any dependency injection libs/frameworks). - Try to uncomment the lines which are trying to use
Circle
andSquare
classes at compile-time. It should give a compile-time error.
- This module has the
[APP] ---- (compile-time) ----> [API] <---- (compile-time) ---- [IMPL]
\ ^
\_________________________ (runtime) _______________________/
mvn clean package
Add this in ~/.m2/toolchains.xml
<?xml version="1.0"?>
<toolchains>
<toolchain>
<type>jdk</type>
<provides>
<version>1.9</version>
<vendor>oracle</vendor>
</provides>
<configuration>
<jdkHome>/path/to/java-9/home</jdkHome>
</configuration>
</toolchain>
</toolchains>
Java9's JPMS do not work on IntelliJ 2017.2.*
You need IntelliJ with version 2017.3 or later.
You may need to explicitly change the module SDKs to Java9.