Red Hat Training

A Red Hat training course is available for Red Hat JBoss Enterprise Application Platform

4.2. Configure Maven

4.2.1. About Maven

Apache Maven is a distributed build automation tool used in Java application development to create, manage, and build software projects. Maven uses standard configuration files called Project Object Model, or POM, files to define projects and manage the build process. POMs describe the module and component dependencies, build order, and targets for the resulting project packaging and output using an XML file. This ensures that the project is built in a correct and uniform manner.
Maven achieves this by using a repository. A Maven repository stores Java libraries, plug-ins, and other build artifacts. The default public repository is the Maven 2 Central Repository, but repositories can be private and internal within a company with a goal to share common artifacts among development teams. Repositories are also available from third-parties. JBoss EAP includes a Maven repository that contains many of the requirements that Java EE developers typically use to build applications on JBoss EAP.
For more information about Maven, see Welcome to Apache Maven.
For more information about Maven repositories, see Apache Maven Project - Introduction to Repositories.
For more information about how to use Maven in JBoss EAP, see the chapter entitled Maven Guide in the Development Guide for Red Hat JBoss Enterprise Application Platform located on the Customer Portal at https://access.redhat.com/documentation/en-us/red_hat_jboss_enterprise_application_platform/?version=6.4.

4.2.2. Configure the Maven Settings

The artifacts and dependencies needed to build and deploy applications to JBoss EAP 6 are hosted on a public repository. You must direct Maven to use this repository when you build your applications. How you do this depends on whether you plan to use Red Hat JBoss Developer Studio or Maven command line to build and deploy your applications.
Red Hat JBoss Developer Studio includes Maven, so there is no need to download and install it separately. If you plan to use Red Hat JBoss Developer Studio to build and deploy your applications, use the following instructions to configure the Maven settings: Section 4.2.3.1, “Configure Maven for Use with Red Hat JBoss Developer Studio”
If you plan to use the Maven command line to build and deploy your applications, you must first download and install Maven using the instructions here: Section 4.2.4.1, “Download and Install Maven”. Then follow these instructions to configure the Maven settings: Section 4.2.4.2, “Configure the Maven Settings for Use with Command Line”

4.2.3. Configure Maven for Red Hat JBoss Developer Studio

4.2.3.1. Configure Maven for Use with Red Hat JBoss Developer Studio

The artifacts and dependencies needed to build and deploy applications to Red Hat JBoss Enterprise Application Platform are hosted on a public repository. You must direct Maven to use this repository when you build your applications. This topic covers the steps to configure Maven if you plan to build and deploy applications using Red Hat JBoss Developer Studio.
If you plan to use Maven command line to build and deploy applications, you can skip this topic. Instead, follow the instructions here: Section 4.2.4.2, “Configure the Maven Settings for Use with Command Line” .
Maven is distributed with Red Hat JBoss Developer Studio, so it is not necessary to install it separately. However, you must configure Maven for use by the Java EE Web Project wizard for deployments to JBoss EAP. The procedure below demonstrates how to configure Maven for use with JBoss EAP by editing the Maven configuration file from within Red Hat JBoss Developer Studio.

Procedure 4.3. Configure Maven in Red Hat JBoss Developer Studio

  1. Click WindowPreferences, expand JBoss Tools and select JBoss Maven Integration.
    Click Window→Preferences, expand JBoss Tools and select JBoss Maven Integration.

    Figure 4.7. JBoss Maven Integration Pane in the Preferences Window

  2. Click Configure Maven Repositories.
  3. Click Add Repository to configure the JBoss GA Tech Preview Maven repository. Complete the Add Maven Repository dialog as follows:
    1. Set the Profile ID, Repository ID, and Repository Name values to jboss-ga-repository.
    2. Set the Repository URL value to http://maven.repository.redhat.com/techpreview/all.
    3. Click the Active by default checkbox to enable the Maven repository.
    4. Click OK
    Enter Maven profile and repository values.

    Figure 4.8. Add Maven Repository - JBoss Tech Preview

  4. Click Add Repository to configure the JBoss Early Access Maven repository. Complete the Add Maven Repository dialog as follows:
    1. Set the Profile ID, Repository ID, and Repository Name values to jboss-earlyaccess-repository.
    2. Set the Repository URL value to http://maven.repository.redhat.com/earlyaccess/all/.
    3. Click the Active by default checkbox to enable the Maven repository.
    4. Click OK
    Enter Maven profile and repository values.

    Figure 4.9. Add Maven Repository - JBoss Early Access

  5. Review the repositories and click Finish.
    Review Maven profile and repository values.

    Figure 4.10. Review Maven Repositories

  6. You are prompted with the message "Are you sure you want to update the file 'MAVEN_HOME/settings.xml'?". Click Yes to update the settings. Click OK to close the dialog.
    The JBoss EAP Maven repository is now configured for use with Red Hat JBoss Developer Studio.

4.2.4. Configure Maven for Command Line

4.2.4.1. Download and Install Maven

If you plan to use Maven command line to build and deploy your applications to JBoss EAP, you must download and install Maven. If you plan to use Red Hat JBoss Developer Studio to build and deploy your applications, you can skip this procedure as Maven is distributed with Red Hat JBoss Developer Studio.
  1. Go to Apache Maven Project - Download Maven and download the latest distribution for your operating system.
  2. See the Maven documentation for information on how to download and install Apache Maven for your operating system.

4.2.4.2. Configure the Maven Settings for Use with Command Line

The artifacts and dependencies needed to build and deploy applications to JBoss EAP 6 are hosted on a public repository. If you plan to use Red Hat JBoss Developer Studio to build and deploy applications, you can skip this topic. Instead, follow the instructions here: Section 4.2.3.1, “Configure Maven for Use with Red Hat JBoss Developer Studio”. However, if you plan to use Maven command line, you must direct Maven to use the JBoss EAP 6 Maven Repository across your projects using the Maven settings.
There are 2 ways to configure the settings for use with Maven command line.
  • You can copy the preconfigured settings.xml file that ships with the quickstarts.
  • You can manually edit the Maven settings.xml file.
This topic describes both methods. If you have downloaded the quickstarts, the first procedure is the simplest way to configure the settings.
Prerequisites

You must install Maven before you configure the settings. For more information, see: Section 4.2.4.1, “Download and Install Maven”

Procedure 4.4. Configure Maven Using the Settings Shipped with the Quickstart Examples

The Red Hat JBoss Enterprise Application Platform Quickstarts ship with a settings.xml file that is configured to use the online JBoss EAP Maven repository. If you have downloaded the quickstarts, this is the easiest way to configure the settings.
  1. This procedure overwrites the existing Maven settings file, so you must back up the existing Maven settings.xml file.
    1. Locate the Maven install directory for your operating system. It is usually installed in USER_HOME/.m2/ directory.
      • For Linux or Mac, this is: ~/.m2/
      • For Windows, this is: \Documents and Settings\USER_NAME\.m2\ or \Users\USER_NAME\.m2\
    2. If you have an existing USER_HOME/.m2/settings.xml file, rename it or make a backup copy so you can restore it later.
  2. If have not yet downloaded the quickstarts, follow the instructions here: Section 3.4.1, “Download the Quickstarts”.
  3. Copy the QUICKSTART_HOME/settings.xml file to the USER_HOME/.m2/ directory.
  4. If you modify the settings.xml file while Red Hat JBoss Developer Studio is running, follow the procedure at the end of this topic entitled Refresh the Red Hat JBoss Developer Studio User Settings.

Procedure 4.5. Manually Edit and Configure the Maven Settings To Use the Online JBoss EAP Maven Repository

You can manually add the JBoss EAP profiles to an existing Maven settings file.
  1. Locate the Maven install directory for your operating system. It is usually installed in USER_HOME/.m2/ directory.
    • For Linux or Mac, this is ~/.m2/
    • For Windows, this is \Documents and Settings\USER_NAME\.m2\ or \Users\USER_NAME\.m2\
  2. If you do not find a settings.xml file, copy the settings.xml file from the USER_HOME/.m2/conf/ directory into the USER_HOME/.m2/ directory.
  3. Copy the following XML into the <profiles> element of the file.
    <!-- Configure the JBoss GA Maven repository -->
    <profile>
      <id>jboss-ga-repository</id>
      <repositories>
        <repository>
          <id>jboss-ga-repository</id>
          <url>http://maven.repository.redhat.com/techpreview/all</url>
          <releases>
            <enabled>true</enabled>
          </releases>
          <snapshots>
            <enabled>false</enabled>
          </snapshots>
        </repository>
      </repositories>
      <pluginRepositories>
        <pluginRepository>
          <id>jboss-ga-plugin-repository</id>
          <url>http://maven.repository.redhat.com/techpreview/all</url>
          <releases>
            <enabled>true</enabled>
          </releases>
          <snapshots>
            <enabled>false</enabled>
          </snapshots>
        </pluginRepository>
      </pluginRepositories>
    </profile>
    <!-- Configure the JBoss Early Access Maven repository -->
    <profile>
      <id>jboss-earlyaccess-repository</id>
      <repositories>
        <repository>
          <id>jboss-earlyaccess-repository</id>
          <url>http://maven.repository.redhat.com/earlyaccess/all/</url>
          <releases>
            <enabled>true</enabled>
          </releases>
          <snapshots>
            <enabled>false</enabled>
          </snapshots>
        </repository>
      </repositories>
      <pluginRepositories>
        <pluginRepository>
          <id>jboss-earlyaccess-plugin-repository</id>
          <url>http://maven.repository.redhat.com/earlyaccess/all/</url>
          <releases>
            <enabled>true</enabled>
          </releases>
          <snapshots>
            <enabled>false</enabled>
          </snapshots>
        </pluginRepository>
      </pluginRepositories>
    </profile>
    
    Copy the following XML into the <activeProfiles> element of the settings.xml file.
    <activeProfile>jboss-ga-repository</activeProfile>
    <activeProfile>jboss-earlyaccess-repository</activeProfile>
    
  4. If you modify the settings.xml file while Red Hat JBoss Developer Studio is running, follow the procedure at the end of this topic entitled Refresh the Red Hat JBoss Developer Studio User Settings.

Procedure 4.6. Refresh the Red Hat JBoss Developer Studio User Settings

If you modify the settings.xml file while Red Hat JBoss Developer Studio is running, you must refresh the user settings.
  1. From the menu, choose WindowPreferences.
  2. In the Preferences Window, expand Maven and choose User Settings.
  3. Click the Update Settings button to refresh the Maven user settings in Red Hat JBoss Developer Studio.
    Update Maven User Settings

    Figure 4.11. Update Maven User Settings

Important

If your Maven repository contains outdated artifacts, you may encounter one of the following Maven error messages when you build or deploy your project:
  • Missing artifact ARTIFACT_NAME
  • [ERROR] Failed to execute goal on project PROJECT_NAME; Could not resolve dependencies for PROJECT_NAME
To resolve the issue, delete the cached version of your local repository to force a download of the latest Maven artifacts. The cached repository is located in your ~/.m2/repository/ subdirectory on Linux, or the %SystemDrive%\Users\USERNAME\.m2\repository\ subdirectory on Windows.