Ce contenu n'est pas disponible dans la langue sélectionnée.
Chapter 34. Getting Started with OptaPlanner and Quarkus
You can use the https://code.quarkus.redhat.com website to generate a Red Hat build of OptaPlanner Quarkus Maven project and automatically add and configure the extensions that you want to use in your application. You can then download the Quarkus Maven repository or use the online Maven repository with your project.
34.1. Apache Maven and Red Hat build of Quarkus Copier lienLien copié sur presse-papiers!
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 (POM) files to define projects and manage the build process. POM files 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 repositories
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 to share common artifacts among development teams. Repositories are also available from third parties.
You can use the online Maven repository with your Quarkus projects or you can download the Red Hat build of Quarkus Maven repository.
Maven plug-ins
Maven plug-ins are defined parts of a POM file that achieve one or more goals. Quarkus applications use the following Maven plug-ins:
-
Quarkus Maven plug-in (
quarkus-maven-plugin): Enables Maven to create Quarkus projects, supports the generation of uber-JAR files, and provides a development mode. -
Maven Surefire plug-in (
maven-surefire-plugin): Used during the test phase of the build lifecycle to execute unit tests on your application. The plug-in generates text and XML files that contain the test reports.
34.1.1. Configuring the Maven settings.xml file for the online repository Copier lienLien copié sur presse-papiers!
You can use the online Maven repository with your Maven project by configuring your user settings.xml file. 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.
When you configure the repository by modifying the Maven settings.xml file, the changes apply to all of your Maven projects.
Procedure
Open the Maven
~/.m2/settings.xmlfile in a text editor or integrated development environment (IDE).NoteIf there is not a
settings.xmlfile in the~/.m2/directory, copy thesettings.xmlfile from the$MAVEN_HOME/.m2/conf/directory into the~/.m2/directory.Add the following lines to the
<profiles>element of thesettings.xmlfile:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Add the following lines to the
<activeProfiles>element of thesettings.xmlfile and save the file.<activeProfile>red-hat-enterprise-maven-repository</activeProfile>
<activeProfile>red-hat-enterprise-maven-repository</activeProfile>Copy to Clipboard Copied! Toggle word wrap Toggle overflow
34.1.2. Downloading and configuring the Quarkus Maven repository Copier lienLien copié sur presse-papiers!
If you do not want to use the online Maven repository, you can download and configure the Quarkus Maven repository to create a Quarkus application with Maven. The Quarkus Maven repository contains many of the requirements that Java developers typically use to build their applications. This procedure describes how to edit the settings.xml file to configure the Quarkus Maven repository.
When you configure the repository by modifying the Maven settings.xml file, the changes apply to all of your Maven projects.
Procedure
- Download the Red Hat build of Quarkus Maven repository ZIP file from the Software Downloads page of the Red Hat Customer Portal (login required).
- Expand the downloaded archive.
-
Change directory to the
~/.m2/directory and open the Mavensettings.xmlfile in a text editor or integrated development environment (IDE). Add the following lines to the
<profiles>element of thesettings.xmlfile, whereQUARKUS_MAVEN_REPOSITORYis the path of the Quarkus Maven repository that you downloaded. The format ofQUARKUS_MAVEN_REPOSITORYmust befile://$PATH, for examplefile:///home/userX/rh-quarkus-2.13.GA-maven-repository/maven-repository.Copy to Clipboard Copied! Toggle word wrap Toggle overflow Add the following lines to the
<activeProfiles>element of thesettings.xmlfile and save the file.<activeProfile>red-hat-enterprise-maven-repository</activeProfile>
<activeProfile>red-hat-enterprise-maven-repository</activeProfile>Copy to Clipboard Copied! Toggle word wrap Toggle overflow
If your Maven repository contains outdated artifacts, you might encounter one of the following Maven error messages when you build or deploy your project, where ARTIFACT_NAME is the name of a missing artifact and PROJECT_NAME is the name of the project you are trying to build:
-
Missing artifact PROJECT_NAME -
[ERROR] Failed to execute goal on project ARTIFACT_NAME; Could not resolve dependencies for PROJECT_NAME
To resolve the issue, delete the cached version of your local repository located in the ~/.m2/repository directory to force a download of the latest Maven artifacts.
34.2. Creating an OptaPlanner Red Hat build of Quarkus Maven project using the Maven plug-in Copier lienLien copié sur presse-papiers!
You can get up and running with a Red Hat build of OptaPlanner and Quarkus application using Apache Maven and the Quarkus Maven plug-in.
Prerequisites
- OpenJDK 11 or later is installed. Red Hat build of Open JDK is available from the Software Downloads page in the Red Hat Customer Portal (login required).
- Apache Maven 3.6 or higher is installed. Maven is available from the Apache Maven Project website.
Procedure
In a command terminal, enter the following command to verify that Maven is using JDK 11 and that the Maven version is 3.6 or higher:
mvn --version
mvn --versionCopy to Clipboard Copied! Toggle word wrap Toggle overflow - If the preceding command does not return JDK 11, add the path to JDK 11 to the PATH environment variable and enter the preceding command again.
To generate a Quarkus OptaPlanner quickstart project, enter the following command:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow This command create the following elements in the
./optaplanner-quickstartdirectory:- The Maven structure
-
Example
Dockerfilefile insrc/main/docker The application configuration file
Expand Table 34.1. Properties used in the mvn io.quarkus:quarkus-maven-plugin:2.13.Final-redhat-00006:create command Property Description projectGroupIdThe group ID of the project.
projectArtifactIdThe artifact ID of the project.
extensionsA comma-separated list of Quarkus extensions to use with this project. For a full list of Quarkus extensions, enter
mvn quarkus:list-extensionson the command line.noExamplesCreates a project with the project structure but without tests or classes.
The values of the
projectGroupIDand theprojectArtifactIDproperties are used to generate the project version. The default project version is1.0.0-SNAPSHOT.
To view your OptaPlanner project, change directory to the OptaPlanner Quickstarts directory:
cd optaplanner-quickstart
cd optaplanner-quickstartCopy to Clipboard Copied! Toggle word wrap Toggle overflow Review the
pom.xmlfile. The content should be similar to the following example:Copy to Clipboard Copied! Toggle word wrap Toggle overflow
34.3. Creating a Red Hat build of Quarkus Maven project using code.quarkus.redhat.com Copier lienLien copié sur presse-papiers!
You can use the code.quarkus.redhat.com website to generate a Red Hat build of OptaPlanner Quarkus Maven project and automatically add and configure the extensions that you want to use in your application. In addition, code.quarkus.redhat.com automatically manages the configuration parameters required to compile your project into a native executable.
This section walks you through the process of generating an OptaPlanner Maven project and includes the following topics:
- Specifying basic details about your application.
- Choosing the extensions that you want to include in your project.
- Generating a downloadable archive with your project files.
- Using the custom commands for compiling and starting your application.
Prerequisites
- You have a web browser.
Procedure
-
Open
https://code.quarkus.redhat.comin your web browser: - Specify details about your project:
-
Enter a group name for your project. The format of the name follows the Java package naming convention, for example,
com.example. -
Enter a name that you want to use for Maven artifacts generated from your project, for example
code-with-quarkus. Select Build Tool > Maven to specify that you want to create a Maven project. The build tool that you choose determines the items:
- The directory structure of your generated project
- The format of configuration files used in your generated project
The custom build script and command for compiling and starting your application that
code.quarkus.redhat.comdisplays for you after you generate your projectNoteRed Hat provides support for using
code.quarkus.redhat.comto create OptaPlanner Maven projects only. Generating Gradle projects is not supported by Red Hat.
-
Enter a version to be used in artifacts generated from your project. The default value of this field is
1.0.0-SNAPSHOT. Using semantic versioning is recommended, but you can use a different type of versioning if you prefer. Enter the package name of artifacts that the build tool generates when you package your project.
According to the Java package naming conventions the package name should match the group name that you use for your project, but you can specify a different name.
NoteThe
code.quarkus.redhat.comwebsite automatically uses the latest release of OptaPlanner. You can manually change the BOM version in thepom.xmlfile after you generate your project.Select the following extensions to include as dependencies:
- RESTEasy JAX-RS (quarkus-resteasy)
- RESTEasy Jackson (quarkus-resteasy-jackson)
- OptaPlanner AI constraint solver(optaplanner-quarkus)
OptaPlanner Jackson (optaplanner-quarkus-jackson)
Red Hat provides different levels of support for individual extensions on the list, which are indicated by labels next to the name of each extension:
- SUPPORTED extensions are fully supported by Red Hat for use in enterprise applications in production environments.
- TECH-PREVIEW extensions are subject to limited support by Red Hat in production environments under the Technology Preview Features Support Scope.
- DEV-SUPPORT extensions are not supported by Red Hat for use in production environments, but the core functionalities that they provide are supported by Red Hat developers for use in developing new applications.
DEPRECATED extension are planned to be replaced with a newer technology or implementation that provides the same functionality.
Unlabeled extensions are not supported by Red Hat for use in production environments.
- Select Generate your application to confirm your choices and display the overlay screen with the download link for the archive that contains your generated project. The overlay screen also shows the custom command that you can use to compile and start your application.
- Select Download the ZIP to save the archive with the generated project files to your system.
- Extract the contents of the archive.
Navigate to the directory that contains your extracted project files:
cd <directory_name>
cd <directory_name>Copy to Clipboard Copied! Toggle word wrap Toggle overflow Compile and start your application in development mode:
./mvnw compile quarkus:dev
./mvnw compile quarkus:devCopy to Clipboard Copied! Toggle word wrap Toggle overflow
34.4. Creating a Red Hat build of Quarkus Maven project using the Quarkus CLI Copier lienLien copié sur presse-papiers!
You can use the Quarkus command line interface (CLI) to create a Quarkus OptaPlanner project.
Prerequisites
- You have installed the Quarkus CLI. For information, see Building Quarkus Apps with Quarkus Command Line Interface.
Procedure
Create a Quarkus application:
quarkus create app -P io.quarkus:quarkus-bom:2.13.Final-redhat-00006
quarkus create app -P io.quarkus:quarkus-bom:2.13.Final-redhat-00006Copy to Clipboard Copied! Toggle word wrap Toggle overflow To view the available extensions, enter the following command:
quarkus ext -i
quarkus ext -iCopy to Clipboard Copied! Toggle word wrap Toggle overflow This command returns the following extensions:
optaplanner-quarkus optaplanner-quarkus-benchmark optaplanner-quarkus-jackson optaplanner-quarkus-jsonb
optaplanner-quarkus optaplanner-quarkus-benchmark optaplanner-quarkus-jackson optaplanner-quarkus-jsonbCopy to Clipboard Copied! Toggle word wrap Toggle overflow Enter the following command to add extensions to the project’s
pom.xmlfile:quarkus ext add resteasy-jackson quarkus ext add optaplanner-quarkus quarkus ext add optaplanner-quarkus-jackson
quarkus ext add resteasy-jackson quarkus ext add optaplanner-quarkus quarkus ext add optaplanner-quarkus-jacksonCopy to Clipboard Copied! Toggle word wrap Toggle overflow Open the
pom.xmlfile in a text editor. The contents of the file should look similar to the following example:Copy to Clipboard Copied! Toggle word wrap Toggle overflow