4.2. MicroProfile 配置开发

4.2.1. 为 MicroProfile 配置创建 Maven 项目

创建一个 Maven 项目,其中包含必要的依赖项,以及用于创建 MicroProfile 配置应用的目录结构。

先决条件

  • 已安装 Maven。

流程

  1. 设置 Maven 项目。

    $ mvn archetype:generate \
        -DgroupId=com.example \
        -DartifactId=microprofile-config \
        -DinteractiveMode=false \
        -DarchetypeGroupId=org.apache.maven.archetypes \
        -DarchetypeArtifactId=maven-archetype-webapp
    cd microprofile-config

    这将为项目和 pom.xml 配置文件创建目录结构。

  2. 要让 POM 文件自动管理 jboss-eap-xp-microprofile BOM 中的 MicroProfile Config 构件和 MicroProfile REST 客户端构件的版本,请将 BOM 导入到项目 POM 文件的 <dependencyManagement> 部分中

    <dependencyManagement>
      <dependencies>
        <!-- importing the microprofile BOM adds MicroProfile specs -->
        <dependency>
            <groupId>org.jboss.bom</groupId>
            <artifactId>jboss-eap-xp-microprofile</artifactId>
            <version>3.0.0.GA</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
      </dependencies>
    </dependencyManagement>
  3. 将 MicroProfile Config 构件和 MicroProfile REST 客户端构件和其他依赖项(由 BOM 管理)添加到 POM 文件的 <dependency> 部分。以下示例演示了将 MicroProfile Config 和 MicroProfile REST 客户端依赖项添加到该文件中:

    <!-- Add the MicroProfile REST Client API. Set provided for the <scope> tag, as the API is included in the server. -->
    <dependency>
      <groupId>org.eclipse.microprofile.rest.client</groupId>
      <artifactId>microprofile-rest-client-api</artifactId>
      <scope>provided</scope>
    </dependency>
    <!-- Add the MicroProfile Config API. Set provided for the <scope> tag, as the API is included in the server. -->
    <dependency>
      <groupId>org.eclipse.microprofile.config</groupId>
      <artifactId>microprofile-config-api</artifactId>
      <scope>provided</scope>
    </dependency>
    <!-- Add the {JAX-RS} API. Set provided for the <scope> tag, as the API is included in the server. -->
    <dependency>
      <groupId>org.jboss.spec.javax.ws.rs</groupId>
      <artifactId>jboss-jaxrs-api_2.1_spec</artifactId>
      <scope>provided</scope>
    </dependency>
    <!-- Add the CDI API. Set provided for the <scope> tag, as the API is included in the server. -->
    <dependency>
      <groupId>jakarta.enterprise</groupId>
      <artifactId>jakarta.enterprise.cdi-api</artifactId>
      <scope>provided</scope>
    </dependency>