In this lab, you will build a multi-module Maven project that simulates processing order data stored in JSON format. You will use the Gson library to convert JSON data into Java objects, then apply business logic to process these orders. The goal is to practice advanced Maven configurations—including creating a parent POM and setting up child modules—while enhancing your skills in JSON manipulation and modular project design.
-
Repository Setup:
- Fork this repo.
- Clone this repo.
- Add your instructor and the class graders as collaborators to your repository. If you are unsure who your class graders are, ask your instructor or refer to the day 1 slide deck.
- In the repository, create a Java project and add the code for the following prompts.
-
Project Structure:
- Create a Maven project with a parent POM.
- Create at least two child modules:
- Order Data Module: This module is responsible for handling JSON order data.
- Order Logic Module: This module implements business logic (e.g., calculating order totals or filtering orders).
Once you finish the assignment, submit a URL link to your repository or your pull request in the field below.
- Create a New Parent Project:
- Start by creating a new directory for your project. The main project should be named solution-lab-2.02-maven.
- Configure the Parent POM:
- Create a
pom.xml
in your parent directory. - Include project metadata (groupId, artifactId, version) and set the packaging to
pom
. - Define a properties section (e.g.,
java.version
andproject.build.sourceEncoding
). - Set up a dependency management section with at least one common dependency (for example, SLF4J).
- Configure repository and pluginRepository sections to point to Maven Central.
- List your child modules using a
<modules>
section (e.g.,order-data
andorder-logic
).
- Create a
-
Module Setup:
- Within the parent project directory, create a new subdirectory named
order-data
. - Create a
pom.xml
for the module. Make sure this POM references the parent POM by including a<parent>
section.
- Within the parent project directory, create a new subdirectory named
-
Implement JSON Processing:
- Develop a Java class (for example,
OrderProcessor.java
) that will:- Read a sample JSON string or file containing order details. Think about the fields you need, such as orderId, customer, items, and total.
- Use the Gson library to convert the JSON data into Java objects. You should define classes like
Order
andOrderItem
that map to the JSON structure. - Print the parsed order data to the console so you can verify that it’s being read correctly.
- Develop a Java class (for example,
-
Module Setup:
- In the parent project directory, create another subdirectory called
order-logic
. - Create a
pom.xml
for this module and reference the parent POM in its configuration.
- In the parent project directory, create another subdirectory called
-
Implement Business Logic:
- Develop a Java class (for example,
OrderCalculator.java
) that will:- Import the order model classes from the
order-data
module (remember to set up inter-module dependencies so that the classes are available). - Process the order data by applying business logic—such as calculating aggregate values (summing order totals, filtering orders based on certain criteria, etc.).
- Print the results of the processing to the console.
- Import the order model classes from the
- Develop a Java class (for example,
- Compile and Package:
- From your parent project directory, run the command:
mvn clean install
- This command should build the parent project and all child modules. Verify that the build completes successfully.
- From your parent project directory, run the command:
- Run and Verify:
- Run the main method in your Order Data Module (e.g., in
OrderProcessor.java
) to check that the JSON data is being parsed correctly. - Run the main method in your Order Logic Module (e.g., in
OrderCalculator.java
) to ensure that your business logic processes the order data correctly. - Make sure the console outputs are clear and provide informative results.
- Run the main method in your Order Data Module (e.g., in
I am stuck and don't know how to solve the problem or where to start. What should I do?
If you are stuck in your code and don't know how to solve the problem or where to start, you should take a step back and try to form a clear, straight forward question about the specific issue you are facing. The process you will go through while trying to define this question, will help you narrow down the problem and come up with potential solutions.
For example, are you facing a problem because you don't understand the concept or are you receiving an error message that you don't know how to fix? It is usually helpful to try to state the problem as clearly as possible, including any error messages you are receiving. This can help you communicate the issue to others and potentially get help from classmates or online resources.
Once you have a clear understanding of the problem, you should be able to start working toward the solution.
How do I create a Maven project in IntelliJ?
To create a Maven project in IntelliJ, you can follow these steps:
- Open IntelliJ IDEA and click the "Create New Project" button.
- In the "New Project" dialog, select "Maven" as the build system.
- Specify the name of the project.
- In the "Project Location" section, specify a location where you want to save your project.
- Select the "Create Git repository" checkbox in order to initialize the git repository upon creation of the project.
- Click the "Create" button to create the Maven project.
What is a parent POM and why is it important?
A parent POM centralizes configurations (dependencies, plugins, properties) so that all child modules inherit them. This ensures consistency and simplifies maintenance across multi-module projects.
How do I build a multi-module project using Maven?
Navigate to the parent project directory in your terminal and run `mvn clean install` to build the entire project. Maven will compile and package all modules as defined in the "modules" section of the parent POM.
How do I set up inter-module dependencies in a multi-module project?
If one module needs to use classes from another module, you must add an inter-module dependency. In the dependent module's `pom.xml`, add a dependency with the groupId, artifactId, and version that match the module it depends on. For example, if your `order-logic` module needs to access classes from the `order-data` module, include the `order-data` module as a dependency. Make sure that the parent POM lists both modules and that you build the project using `mvn clean install` so that all modules are correctly compiled and packaged.
I am unable to push changes to my repository. What should I do?
If you are unable to push changes to your repository, here are a few steps that you can follow:
- Check your internet connection: Ensure that your internet connection is stable and working.
- Verify your repository URL: Make sure that you are using the correct repository URL to push your changes.
- Check Git credentials: Ensure that your Git credentials are up-to-date and correct. You can check your credentials using the following command:
git config --list
- Update your local repository: Before pushing changes, make sure that your local repository is up-to-date with the remote repository. You can update your local repository using the following command:
git fetch origin
- Check for conflicts: If there are any conflicts between your local repository and the remote repository, resolve them before pushing changes.
- Push changes: Once you have resolved any conflicts and updated your local repository, you can try pushing changes again using the following command:
git push origin <branch_name>