Chapter 2. Maven basics in CodeReady Studio

Maven provides a standardized build system for application development, 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 and is often referred to as an aggregator pom.

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

2.1. Creating a new Maven project

The following section describes how to create a new Maven project in CodeReady Studio.

Procedure

  1. Start CodeReady Studio.
  2. Click WindowShow ViewOther.

    The Show View window appears.

    crs pe explorer view
  3. Enter Project Explorer in the search field.
  4. Select Project Explorer.
  5. Click Open.

    The Project Explorer view appears.

    crs maven project in project explorer
  6. Press Ctrl+N.

    The Select a wizard window appears.

    crs wizard maven project
  7. Enter Maven in the Wizards field.
  8. Select Maven Project.
  9. Click Next.

    The New Maven Project window appears.

    crs creation of maven project
  10. 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), which is a requirement for multi-module Maven projects.

    To create a standalone Maven project instead, clear the Create a simple project check box and follow the onscreen instructions to set the packaging option to jar or war.

  11. Click Browse to select the workspace location.
  12. Click Next.

    crs maven project creation2
  13. Enter the group ID and the artifact ID.

    Note

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

    Optionally, you can name your project and add a description.

  14. Set Packaging to pom, jar or war.
  15. Click Finish.

Your newly created Maven project 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 an existing locally stored Maven project

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

Procedure

  1. Start CodeReady Studio.
  2. Click FileImport.

    crs file import

    The Import window appears.

    crs importing maven project
  3. Enter Maven in the Select an import wizard field.
  4. Select Existing Maven Projects.
  5. Click Next.

    The Import Maven Project window appears.

    crs importing local maven project
  6. Click Browse to locate your Maven project.
  7. Select the Add project(s) to working set check box.
  8. Click Finish.

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

2.2.2. Importing an existing remotely stored Maven project

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

Procedure

  1. Start CodeReady Studio.
  2. Open Git Perspective.
  3. Click the Clone a Git repository and import existing Maven projects icon.

    crs clone a git repository and import existing maven projects

    The Check out as Maven project from SCM window appears.

    crs importing remote maven project
  4. Add the address for the source repository to the SCM URL field.
  5. Click Next.

    The Select Project Location window appears.

    crs remote maven project location
  6. Click Browse to select the workspace location.
  7. Click Finish.

Your remote Maven project is now listed in the Git Repositories 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. Press Ctrl+N.

    The Select a wizard window appears.

    crs wizard maven module
  3. Enter Maven in the Wizards field.
  4. Select Maven Module.
  5. Click Next.

    The New Maven Module window appears.

    crs creating a new maven module
  6. 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), which is a requirement for multi-module Maven projects.

    To create a standalone Maven project instead, clear the Create a simple project check box and follow the onscreen instructions to set the packaging option to jar or war.

  7. Name your module.
  8. Click Browse to select the parent project.
  9. Click Next.

    The Configure Project window appears.

    crs configure new maven module
  10. Set Packaging to pom, jar or war.

    Optionally, you can name your module and add a description.

  11. Click Finish.

Your newly created Maven module is now listed below your Maven project.

2.4. Adding a Maven dependency to a Maven project

The following section describes how to add a Maven dependency to a Maven project in CodeReady Studio.

Prerequisites

Procedure

  1. Start CodeReady Studio.
  2. Open Project Explorer.
  3. Right-click your Maven projectMavenAdd Dependency.

    crs maven add dependency

    The Add Dependency window appears.

    crs maven dependency enter groupid
  4. Enter the group ID or the artifact ID in the Enter groupId, artifactId or sha1 prefix or pattern field.

    The fields above are populated automatically.

  5. Click OK.

The dependency is now added to the pom.xml file of your 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 Project Explorer.
  3. Right-click your projectConfigureConvert to Maven Project.

    crs convert to maven project

    The Create a new POM window appears.

    crs java to maven conversion

    All fields are populated automatically. If you want to change the group ID or the artifact ID, note that 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 artifact ID is org.company-name_project-name.

  4. Click Finish.

    Your newly generated pom.xml file appears under your Java project.

2.6. Additional resources

  • For more information on how to use the Maven software project management and comprehension tool, see the JBoss Community Archive.