The folder reservation-system
contains a Maven project consisting of just one module.
(it's similar to the one on first exercise). You'll separate this app in three different modules.
Maven modules (managed by pom.xml
files) and modules descriptor (module-info.java) complement each other. But, they
play different roles. Maven brings the functionality of automatic, centralized dependency management. And the
module descriptors handle the modularity at Java platform level. Let's see how they work together.
Running mvn clean package
generates just one jar that can be run with
java -jar target/reservation-system-1.0-SNAPSHOT.jar
-
On the
pom.xml
change the Java version from 1.8 to 11. -
Change the packaging from
jar
topom
to allow maven submodules. -
Under the
reservation-system
folder we need to add three different folders, each of them will contain a moduel.- flightmodule
- hotelmodule
- packagemodule
-
For each of the modules we need to declare a
pom.xml
referencing the parent module. Here's what's needed for flightmodule.<parent> <groupId>com.udacity.flight</groupId> <artifactId>reservation-system</artifactId> <version>1.0-SNAPSHOT</version> </parent> <artifactId>flightmodule</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <name>flightmodule</name>
Add similar
pom.xml
s for the other modules -
Add a
modules
section to the parentpom.xml
<modules> <module>flightmodule</module> <module>hotelmodule</module> <module>packagemodule</module> </modules>
-
Move the classes from parent module to each submodule (from package
com.udacity.fligth
toflightmodule
,com.udacity.hotel
tohotelmodule
, andcom.udacity.packagesearch
topackagemodule
). Each submodule should have the structure of a Maven project (with the directorysrc/main/java
). -
After doing this, the classes on
packagemodule
will lose some visibility on classes, so let's add the required dependencies to itspom.xml
to recover that visibility.<dependencies> <dependency> <groupId>com.udacity.flight</groupId> <artifactId>hotelmodule</artifactId> <version>1.0-SNAPSHOT</version> <scope>compile</scope> </dependency> <dependency> <groupId>com.udacity.flight</groupId> <artifactId>flightmodule</artifactId> <version>1.0-SNAPSHOT</version> <scope>compile</scope> </dependency> </dependencies>
-
Move the declaration of
maven-jar-plugin
topackagemodule
since it's the one that contains themainClass
. -
Now the project is modularized at Maven level, to convert it in a Java modularized project we need to add the
module-info.java
, they will be in thesrc/main/java
directory of each of them. These files are similar to what we did on exercise one. -
The project should build with
mvn clean package
but now it will create three different jars, to run them you need specify the dependencies like this:java -cp flightmodule/target/flightmodule-1.0-SNAPSHOT.jar:hotelmodule/target/hotelmodule-1.0-SNAPSHOT.jar:packagemodule/target/packagemodule-1.0-SNAPSHOT.jar com.udacity.packagesearch.search.Main