Dieser Inhalt ist in der von Ihnen ausgewählten Sprache nicht verfügbar.
Chapter 2. Using Maven with JBoss EAP
2.1. Learn about Maven
2.1.1. About the Maven Repository
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. To configure your project to use this repository, see Configure the JBoss EAP Maven Repository.
For more information about Maven, see Welcome to Apache Maven.
For more information about Maven repositories, see Apache Maven Project - Introduction to Repositories.
2.1.2. About the Maven POM File
The Project Object Model, or POM, file is a configuration file used by Maven to build projects. It is an XML file that contains information about the project and how to build it, including the location of the source, test, and target directories, the project dependencies, plug-in repositories, and goals it can execute. It can also include additional details about the project including the version, description, developers, mailing list, license, and more. A pom.xml
file requires some configuration options and will default all others.
The schema for the pom.xml
file can be found at http://maven.apache.org/maven-v4_0_0.xsd.
For more information about POM files, see the Apache Maven Project POM Reference.
Minimum Requirements of a Maven POM File
The minimum requirements of a pom.xml
file are as follows:
- project root
- modelVersion
- groupId - the ID of the project’s group
- artifactId - the ID of the artifact (project)
- version - the version of the artifact under the specified group
Example: Basic pom.xml
File
A basic pom.xml
file might look like this:
<project> <modelVersion>4.0.0</modelVersion> <groupId>com.jboss.app</groupId> <artifactId>my-app</artifactId> <version>1</version> </project>
2.1.3. About the Maven Settings File
The Maven settings.xml
file contains user-specific configuration information for Maven. It contains information that must not be distributed with the pom.xml
file, such as developer identity, proxy information, local repository location, and other settings specific to a user.
There are two locations where the settings.xml
can be found:
-
In the Maven installation: The settings file can be found in the
$M2_HOME/conf/
directory. These settings are referred to asglobal
settings. The default Maven settings file is a template that can be copied and used as a starting point for the user settings file. -
In the user’s installation: The settings file can be found in the
${user.home}/.m2/
directory. If both the Maven and usersettings.xml
files exist, the contents are merged. Where there are overlaps, the user’ssettings.xml
file takes precedence.
Example: Maven Settings File
<?xml version="1.0" encoding="UTF-8"?> <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd"> <profiles> <!-- Configure the JBoss EAP Maven repository --> <profile> <id>jboss-eap-maven-repository</id> <repositories> <repository> <id>jboss-eap</id> <url>file:///path/to/repo/jboss-eap-7.1.0.GA-maven-repository/maven-repository</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>jboss-eap-maven-plugin-repository</id> <url>file:///path/to/repo/jboss-eap-7.1.0.GA-maven-repository/maven-repository</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>false</enabled> </snapshots> </pluginRepository> </pluginRepositories> </profile> </profiles> <activeProfiles> <!-- Optionally, make the repository active by default --> <activeProfile>jboss-eap-maven-repository</activeProfile> </activeProfiles> </settings>
The schema for the settings.xml
file can be found at http://maven.apache.org/xsd/settings-1.0.0.xsd.
2.1.4. About Maven Repository Managers
A repository manager is a tool that allows you to easily manage Maven repositories. Repository managers are useful in multiple ways:
- They provide the ability to configure proxies between your organization and remote Maven repositories. This provides a number of benefits, including faster and more efficient deployments and a better level of control over what is downloaded by Maven.
- They provide deployment destinations for your own generated artifacts, allowing collaboration between different development teams across an organization.
For more information about Maven repository managers, see Best Practice - Using a Repository Manager.
Commonly used Maven repository managers
- Sonatype Nexus
- See Sonatype Nexus documentation for more information about Nexus.
- Artifactory
- See JFrog Artifactory documentation for more information about Artifactory.
- Apache Archiva
- See Apache Archiva: The Build Artifact Repository Manager for more information about Apache Archiva.
In an enterprise environment, where a repository manager is usually used, Maven should query all artifacts for all projects using this manager. Because Maven uses all declared repositories to find missing artifacts, if it can not find what it is looking for, it will try and look for it in the repository central
(defined in the built-in parent POM). To override this central
location, you can add a definition with central
so that the default repository central
is now your repository manager as well. This works well for established projects, but for clean or 'new' projects it causes a problem as it creates a cyclic
dependency.
2.2. Install Maven and the JBoss EAP Maven Repository
2.2.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.
- Go to Apache Maven Project - Download Maven and download the latest distribution for your operating system.
- See the Maven documentation for information on how to download and install Apache Maven for your operating system.
2.2.2. Download the JBoss EAP Maven Repository
There are two ways to download the JBoss EAP Maven repository:
2.2.2.1. Download the JBoss EAP Maven Repository ZIP File from the Customer Portal
Follow these steps to download the JBoss EAP Maven repository.
- Open a web browser and access this URL: https://access.redhat.com/jbossnetwork/restricted/listSoftware.html?product=appplatform.
- Find Red Hat JBoss Enterprise Application Platform 7.1 Maven Repository in the list.
-
Click the Download button to download a
.zip
file containing the repository.
2.2.2.2. Download the JBoss EAP Maven Repository Using the Offliner Application
An Offliner application is now available as an alternate option to download the Maven artifacts from the Red Hat Maven repository for developing JBoss EAP 7.1 applications.
The process of downloading the JBoss EAP Maven repository using the Offliner application is provided as Technology Preview only. Technology Preview features are not supported with Red Hat production service level agreements (SLAs), might not be functionally complete, and Red Hat does not recommend to use them for production. These features provide early access to upcoming product features, enabling customers to test functionality and provide feedback during the development process.
See Technology Preview Features Support Scope on the Red Hat Customer Portal for information about the support scope for Technology Preview features.
Download the Maven Repository Offliner Content List text file,
jboss-eap-7.1.0-maven-repository-content-with-sha256-checksums.txt
, from the JBoss EAP Software Downloads page. This serves as the input to the Offliner application.NoteThis file does not contain license information. The artifacts downloaded by the Offliner application have the same licenses as specified in the Maven repository ZIP file that is distributed with JBoss EAP.
- Download the Offliner application from the Maven Central Repository.
Run the Offliner application using the following command:
$ java -jar offliner.jar -r http://repository.redhat.com/ga/ -d FOLDER_TO_DOWNLOAD_TO jboss-eap-7.1.0-maven-repository-content-with-sha256-checksums.txt
The artifacts from the JBoss EAP Maven repository will be downloaded in the
FOLDER_TO_DOWNLOAD_TO
directory.
See the Offliner documentation for more information on running the Offliner application.
The generated JBoss EAP Maven repository will have the same content that is currently available in the JBoss EAP Maven repository ZIP file. It will not contain artifacts available in Maven Central repository.
2.2.3. Install the JBoss EAP Maven Repository
There are three ways to install the JBoss EAP Maven repository.
- You can install the JBoss EAP Maven repository on your local file system. For detailed instructions, see Install the JBoss EAP Maven Repository Locally.
- You can install the JBoss EAP Maven repository on the Apache Web Server. For more information, see Install the JBoss EAP Maven Repository for Use with Apache httpd.
- You can install the JBoss EAP Maven repository using the Nexus Maven Repository Manager. For more information, see Repository Management Using Nexus Maven Repository Manager.
You can use the JBoss EAP Maven repository available online, or download and install it locally using any one of the three listed methods.
2.2.3.1. Install the JBoss EAP Maven Repository Locally
This section covers the steps to install the JBoss EAP Maven Repository to the local file system. This option is easy to configure and allows you to get up and running quickly on your local machine. It can help you become familiar with using Maven for development but is not recommended for team production environments.
Follow these steps to install the JBoss EAP Maven repository to the local file system.
- Make sure you have Download the JBoss EAP Maven Repository ZIP File from the Customer Portal to your local file system.
Unzip the file on the local file system into a directory of your choosing.
This creates a new
jboss-eap-7.1.0.GA-maven-repository/
directory, which contains the Maven repository in a subdirectory namedmaven-repository/
.
If you want to continue to use an older local repository, you must configure it separately in the Maven settings.xml
configuration file. Each local repository must be configured within its own <repository>
tag.
When downloading a new Maven repository, remove the cached repository/
subdirectory located under the .m2/
directory before attempting to use it.
2.2.3.2. Install the JBoss EAP Maven Repository for Use with Apache httpd
This section covers the steps to install the JBoss EAP Maven Repository for use with Apache httpd. This option is good for multi-user and cross-team development environments because any developer that can access the web server can also access the Maven repository.
You must first configure Apache httpd. See Apache HTTP Server Project documentation for instructions.
- Make sure you have Download the JBoss EAP Maven Repository ZIP File from the Customer Portal to your local file system.
- Unzip the file in a directory that is web accessible on the Apache server.
Configure Apache to allow read access and directory browsing in the created directory.
This configuration allows a multi-user environment to access the Maven repository on Apache httpd.
2.3. Use the Maven Repository
2.3.1. Configure the JBoss EAP Maven Repository
- Overview
There are two approaches to direct Maven to use the JBoss EAP Maven Repository in your project:
Configure the JBoss EAP Maven Repository Using the Maven Settings
This is the recommended approach. Maven settings used with a repository manager or repository on a shared server provide better control and manageability of projects. Settings also provide the ability to use an alternative mirror to redirect all lookup requests for a specific repository to your repository manager without changing the project files. For more information about mirrors, see http://maven.apache.org/guides/mini/guide-mirror-settings.html.
This method of configuration applies across all Maven projects, as long as the project POM file does not contain repository configuration.
This section describes how to configure the Maven settings. You can configure the Maven install global settings or the user’s install settings.
Configure the Maven Settings File
Locate the Maven
settings.xml
file for your operating system. It is usually located in the${user.home}/.m2/
directory.-
For Linux or Mac, this is
~/.m2/
-
For Windows, this is
\Documents and Settings\.m2\
or\Users\.m2\
-
For Linux or Mac, this is
-
If you do not find a
settings.xml
file, copy thesettings.xml
file from the${user.home}/.m2/conf/
directory into the${user.home}/.m2/
directory. Copy the following XML into the
<profiles>
element of thesettings.xml
file. Determine the URL of the JBoss EAP repository and replace JBOSS_EAP_REPOSITORY_URL with it.<!-- Configure the JBoss Enterprise Maven repository --> <profile> <id>jboss-enterprise-maven-repository</id> <repositories> <repository> <id>jboss-enterprise-maven-repository</id> <url>JBOSS_EAP_REPOSITORY_URL</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>jboss-enterprise-maven-repository</id> <url>JBOSS_EAP_REPOSITORY_URL</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>false</enabled> </snapshots> </pluginRepository> </pluginRepositories> </profile>
The following is an example configuration that accesses the online JBoss EAP Maven repository.
<!-- Configure the JBoss Enterprise Maven repository --> <profile> <id>jboss-enterprise-maven-repository</id> <repositories> <repository> <id>jboss-enterprise-maven-repository</id> <url>https://maven.repository.redhat.com/ga/</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>jboss-enterprise-maven-repository</id> <url>https://maven.repository.redhat.com/ga/</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>false</enabled> </snapshots> </pluginRepository> </pluginRepositories> </profile>
Copy the following XML into the
<activeProfiles>
element of thesettings.xml
file.<activeProfile>jboss-enterprise-maven-repository</activeProfile>
If you modify the
settings.xml
file while Red Hat JBoss Developer Studio is running, you must refresh the user settings.-
From the menu, choose
Window
.Preferences -
In the
Preferences
window, expandMaven
and chooseUser Settings
. Click the
Update Settings
button to refresh the Maven user settings in Red Hat JBoss Developer Studio.The Update Maven User Settings screen shot
-
From the menu, choose
If your Maven repository contains outdated artifacts, you might 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 here: ${user.home}/.m2/repository/
Configure the JBoss EAP Maven Repository Using the Project POM
You should avoid this method of configuration as it overrides the global and user Maven settings for the configured project.
You must plan carefully if you decide to configure repositories using project POM file. Transitively included POMs are an issue with this type of configuration since Maven has to query the external repositories for missing artifacts and this slows the build process. It can also cause you to lose control over where your artifacts are coming from.
The URL of the repository will depend on where the repository is located: on the file system, or web server. For information on how to install the repository, see: Install the JBoss EAP Maven Repository. The following are examples for each of the installation options:
- File System
- file:///path/to/repo/jboss-eap-maven-repository
- Apache Web Server
- http://intranet.acme.com/jboss-eap-maven-repository/
- Nexus Repository Manager
- https://intranet.acme.com/nexus/content/repositories/jboss-eap-maven-repository
Configuring the Project’s POM File
-
Open your project’s
pom.xml
file in a text editor. Add the following repository configuration. If there is already a
<repositories>
configuration in the file, then add the<repository>
element to it. Be sure to change the<url>
to the actual repository location.<repositories> <repository> <id>jboss-eap-repository-group</id> <name>JBoss EAP Maven Repository</name> <url>JBOSS_EAP_REPOSITORY_URL</url> <layout>default</layout> <releases> <enabled>true</enabled> <updatePolicy>never</updatePolicy> </releases> <snapshots> <enabled>true</enabled> <updatePolicy>never</updatePolicy> </snapshots> </repository> </repositories>
Add the following plug-in repository configuration. If there is already a
<pluginRepositories>
configuration in the file, then add the<pluginRepository>
element to it.<pluginRepositories> <pluginRepository> <id>jboss-eap-repository-group</id> <name>JBoss EAP Maven Repository</name> <url>JBOSS_EAP_REPOSITORY_URL</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>true</enabled> </snapshots> </pluginRepository> </pluginRepositories>
Determine the URL of the JBoss EAP Repository
The repository URL depends on where the repository is located. You can configure Maven to use any of the following repository locations.
- To use the online JBoss EAP Maven repository, specify the following URL: https://maven.repository.redhat.com/ga/
- To use a JBoss EAP Maven repository installed on the local file system, you must download the repository and then use the local file path for the URL. For example: file:///path/to/repo/jboss-eap-7.1.0.GA-maven-repository/maven-repository/
- If you install the repository on an Apache Web Server, the repository URL will be similar to the following: http://intranet.acme.com/jboss-eap-7.1.0.GA-maven-repository/maven-repository/
- If you install the JBoss EAP Maven repository using the Nexus Repository Manager, the URL will look something like the following: https://intranet.acme.com/nexus/content/repositories/jboss-eap-7.1.0.GA-maven-repository/maven-repository/
Remote repositories are accessed using common protocols such as http://
for a repository on an HTTP server or file://
for a repository on a file server.
2.3.2. 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 section covers the steps to configure Maven if you plan to build and deploy applications using Red Hat JBoss Developer Studio.
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.
Configure Maven in Red Hat JBoss Developer Studio
Click
Window
, expand JBoss Tools and select JBoss Maven Integration.Preferences JBoss Maven Integration Pane in the Preferences Window
- Click Configure Maven Repositories.
Click Add Repository to configure the JBoss Enterprise Maven repository. Complete the
Add Maven Repository
dialog as follows:-
Set the Profile ID, Repository ID, and Repository Name values to
jboss-ga-repository
. -
Set the Repository URL value to
http://maven.repository.redhat.com/ga
. - Click the Active by default checkbox to enable the Maven repository.
Click OK.
Add Maven Repository
-
Set the Profile ID, Repository ID, and Repository Name values to
Review the repositories and click Finish.
Review Maven Repositories
-
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.
2.3.3. Manage Project Dependencies
This section describes the usage of Bill of Materials (BOM) POMs for Red Hat JBoss Enterprise Application Platform.
A BOM is a Maven pom.xml
(POM) file that specifies the versions of all runtime dependencies for a given module. Version dependencies are listed in the dependency management section of the file.
A project uses a BOM by adding its groupId:artifactId:version
(GAV) to the dependency management section of the project pom.xml
file and specifying the <scope>import</scope>
and <type>pom</type>
element values.
In many cases, dependencies in project POM files use the provided
scope. This is because these classes are provided by the application server at runtime and it is not necessary to package them with the user application.
Supported Maven Artifacts
As part of the product build process, all runtime components of JBoss EAP are built from source in a controlled environment. This helps to ensure that the binary artifacts do not contain any malicious code, and that they can be supported for the life of the product. These artifacts can be easily identified by the -redhat
version qualifier, for example 1.0.0-redhat-1
.
Adding a supported artifact to the build configuration pom.xml
file ensures that the build is using the correct binary artifact for local building and testing. Note that an artifact with a -redhat
version is not necessarily part of the supported public API, and might change in future revisions. For information about the public supported API, see the Javadoc documentation included in the release.
For example, to use the supported version of Hibernate, add something similar to the following to your build configuration.
<dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>5.1.6.Final-redhat-1</version> <scope>provided</scope> </dependency>
Notice that the above example includes a value for the <version/>
field. However, it is recommended to use Maven dependency management for configuring dependency versions.
Dependency Management
Maven includes a mechanism for managing the versions of direct and transitive dependencies throughout the build. For general information about using dependency management, see the Apache Maven Project: Introduction to the Dependency Mechanism.
Using one or more supported Red Hat dependencies directly in your build does not guarantee that all transitive dependencies of the build will be fully supported Red Hat artifacts. It is common for Maven builds to use a mix of artifact sources from the Maven central repository and other Maven repositories.
There is a dependency management BOM included in the JBoss EAP Maven repository, which specifies all the supported JBoss EAP binary artifacts. This BOM can be used in a build to ensure that Maven will prioritize supported JBoss EAP dependencies for all direct and transitive dependencies in the build. In other words, transitive dependencies will be managed to the correct supported dependency version where applicable. The version of this BOM matches the version of the JBoss EAP release.
<dependencyManagement> <dependencies> ... <dependency> <groupId>org.jboss.bom</groupId> <artifactId>eap-runtime-artifacts</artifactId> <version>7.1.0.GA</version> <type>pom</type> <scope>import</scope> </dependency> ... </dependencies> </dependencyManagement>
In JBoss EAP 7 the name of this BOM was changed from eap6-supported-artifacts to eap-runtime-artifacts. The purpose of this change is to make it more clear that the artifacts in this POM are part of the JBoss EAP runtime, but are not necessarily part of the supported public API. Some of the jars contain internal API and functionality, which might change between releases.
JBoss EAP Java EE Specs BOM
The jboss-eap-javaee7
BOM contains the Java EE Specification API JARs used by JBoss EAP.
To use this BOM in a project, add a dependency for the GAV that contains the version of the JSP and Servlet API JARs needed to build and deploy the application.
The following example uses the 1.1.0.Final-redhat-1
version of the jboss-eap-javaee7
BOM.
<dependencyManagement> <dependencies> <dependency> <groupId>org.jboss.spec</groupId> <artifactId>jboss-javaee-7.0</artifactId> <version>1.1.0.Final-redhat-1</version> <type>pom</type> <scope>import</scope> </dependency> ... </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.jboss.spec.javax.servlet</groupId> <artifactId>jboss-servlet-api_3.1_spec</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>org.jboss.spec.javax.servlet.jsp</groupId> <artifactId>jboss-jsp-api_2.3_spec</artifactId> <scope>provided</scope> </dependency> ... </dependencies>
JBoss EAP BOMs Available for Application Development
The following table lists the Maven BOMs that are available for application development.
BOM Artifact ID | Use Case |
---|---|
jboss-eap-javaee7 | Supported JBoss EAP JavaEE 7 APIs plus additional JBoss EAP API JARs. |
jboss-eap-javaee7-with-spring4 |
|
jboss-eap-javaee7-with-tools |
|
These BOMs from JBoss EAP 6 have been consolidated into fewer BOMs to make usage simpler for most use cases. The Hibernate, logging, transactions, messaging, and other public API jars are now included in jboss-javaee7-eap
instead of a requiring a separate BOM for each case.
The following example uses the 7.1.0.GA
version of the jboss-eap-javaee7
BOM.
<dependencyManagement> <dependencies> <dependency> <groupId>org.jboss.bom</groupId> <artifactId>jboss-eap-javaee7</artifactId> <version>7.1.0.GA</version> <type>pom</type> <scope>import</scope> </dependency> ... </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <scope>provided</scope> </dependency> ... </dependencies>
JBoss EAP Client BOMs
The client BOMs do not create a dependency management section or define dependencies. Instead, they are an aggregate of other BOMs and are used to package the set of dependencies necessary for a remote client use case.
The wildfly-ejb-client-bom
and wildfly-jms-client-bom
BOMs are managed by the jboss-eap-javaee7
BOM, so there is no need to manage the versions in your project dependencies.
The following is an example of how to add the wildfly-ejb-client-bom
and wildfly-jms-client-bom
client BOM dependencies to your project.
<dependencyManagement> <dependencies> <!-- jboss-eap-javaee7: JBoss stack of the Java EE APIs and related components. --> <dependency> <groupId>org.jboss.bom</groupId> <artifactId>jboss-eap-javaee7</artifactId> <version>7.1.0.GA</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> ... </dependencyManagement> <dependencies> <dependency> <groupId>org.jboss.eap</groupId> <artifactId>wildfly-ejb-client-bom</artifactId> <type>pom</type> </dependency> <dependency> <groupId>org.jboss.eap</groupId> <artifactId>wildfly-jms-client-bom</artifactId> <type>pom</type> </dependency> ... </dependencies>
For more information about Maven Dependencies and BOM POM files, see Apache Maven Project - Introduction to the Dependency Mechanism.