Chapter 2. Maven basics in CodeReady Studio

In the context of application development, Maven provides a standardized build system for projects, and facilitates fetching dependencies from one or more repositories.

Root Maven projects can serve as aggregators for multiple Maven modules (sub-projects). For each module that is part of a maven project, a <module> entry is added to the project’s pom.xml file. A pom.xml contains <module> entries is often referred to as an aggregator pom.

When modules are included into a project it is possible to execute Maven goals across all of the modules by a single command issued from the parent project directory.

The following section describes how to:

  • Create a new Maven project.
  • Import existing Maven projects.
  • Create a new Maven module.
  • Add a Maven dependency to the Maven project.
  • Add Maven support to a non-maven project.

Prerequisites

2.1. Creating a new Maven project

The following section describes how to create a new Maven project in CodeReady Studio. The instructions provided ensure that the packaging option is set to pom, which is a requirement for multi-module Maven projects. Alternately, to create a standalone Maven project instead, set the packaging option to an option other than pom.

Procedure

  1. Start CodeReady Studio.
  2. Open Git Perspective.
  3. Click FileNewOther.

    The New window appears.

  4. Enter Maven Projects in the Wizards field.
  5. Select Maven Projects.
  6. Click the Next button.

    The New Maven Project window appears.

  7. Select the Create a simple project check box.

    Note

    By selecting the Create a simple project check box you are skipping the archetype selection and the project type is automatically set to Project Object Model (POM). To create a standalone project, clear the Create a simple project check box and follow the onscreen instructions.

  8. Click the Browse button to select the workspace location.
  9. Click the Next button.
  10. Enter the group ID and the artificial ID.

    Note

    The values cannot include spaces or special characters. The only special characters allowed are periods (.), underscores (_), and dashes (-). An example of a typical group ID or artificial ID is org.company-name_project-name.

    Optionally, you can name your project and give it a description.

  11. Set Packaging to pom.
  12. Click the Finish button.

Verification steps

  1. Click WindowShow ViewOther.

    The Show view window appears.

  2. Enter Project Explorer in the search field.
  3. Select Project Explorer.
  4. Click the Open button.

    The Project Explorer view appears.

Your newly created Maven is now listed in the Project Explorer view.

2.2. Importing existing Maven projects

The following section describes how to import existing Maven projects into CodeReady Studio.

2.2.1. Importing the existing locally stored Maven project

The following section describes how to import the existing locally stored Maven project to CodeReady Studio.

Procedure

  1. Start CodeReady Studio.
  2. Open Git Perspective.
  3. Open Project Explorer.
  4. Click FileImport.

    The Import window appears.

  5. Enter Existing Maven Projects in the Select an import wizards field.
  6. Select Existing Maven Projects.
  7. Click the Next button.
  8. Click the Browse button to locate your Maven project.
  9. Click the Finish button.

Your local Maven project is now listed in the System Explorer view.

2.2.2. Importing the existing remotely stored Maven project

The following section describes how to import the existing remotely stored Maven project to CodeReady Studio.

Procedure

  1. Clone the Clone the source repository with HTTPS or SSH.
  2. Start CodeReady Studio.
  3. Open Git Perspective.
  4. Click the Clone a Git repository and import existing Maven projects icon.

    Figure 2.1. Importing the existing Maven project

    clone a Git repo and import existing maven projects

    The Check out as Maven project from SCM window appears.

  5. Add the address for the source repository you cloned during step one to the SCM URL field.
  6. Click the Next button.
  7. Click the Browse button to select the workspace location.
  8. Click the Finish button.

Your remote Maven project is now listed in the System Explorer view and the Git Perspective view.

2.3. Creating a new Maven module

The following section describes how to create a new Maven module.

Prerequisites

Procedure

  1. Start CodeReady Studio.
  2. Open Git Perspective.
  3. Click FileNewOther.

    The New window appears.

  4. Enter Maven Module in the Wizards field.
  5. Select Maven Module.
  6. Click the Next button.

    The New Maven Module window appears.

  7. Select the Create a simple project check box.

    Note

    By selecting the Create a simple project check box you are skipping the archetype selection and the project type is automatically set to Project Object Model (POM). To create a standalone module, clear the Create a simple project check box and follow the onscreen instructions.

  8. Name your module.
  9. Click the Browse button to select the parent project.
  10. Click the Next button.
  11. Set Packaging to pom.

    Optionally, you can name your module and give it a description.

  12. Click the Finish button.

Verification steps

  1. Click WindowShow ViewOther.

    The Show view window appears.

  2. Enter Project Explorer in the search field.
  3. Select Project Explorer.
  4. Click the Open button.

    The Project Explorer view appears.

Your newly created Maven module is now listed in the System Explorer view.

2.4. Adding a Maven dependency to the Maven project

The following section describes how to add a Maven dependency to the Maven project.

Prerequisites

Procedure

  1. Start CodeReady Studio.
  2. Open Git Perspective.
  3. Open Project Explorer.
  4. Right-click the target projectMavenAdd Dependency.
  5. Enter the group ID or the artificial ID in the Enter groupId, artificialId or sha1 prefix or pattern field.

    The fields above are populated automatically.

  6. Click the OK button.

The dependency is now added to the pom.xml file of your target project.

2.5. Adding Maven support to an existing non-maven project

The following section describes how to add Maven support to an application created without Maven support.

  1. Start CodeReady Studio.
  2. Open Git Perspective.
  3. Open Project Explorer.
  4. Right-click the target projectConfigureConvert to Maven Project.

    The Create a new POM window appears.

    All fields are populated automatically. If you want to change the group ID or the Artificial ID, note that the values cannot include spaces or special characters. The only special characters allowed are periods (.), underscores (_), and dashes (-).

  5. Click the Finish button.

    Your newly generated pom.xml file appears in the System Explorer view.

2.6. Additional resources