覆盖配置文件中的 Maven 依赖范围

Override maven dependency scope in profile

我有 spring-基于 maven 的启动应用程序。

我只想将 h2 数据库作为测试的依赖项,所以我将其设置如下:

<dependency>
      <groupId>com.h2database</groupId>
      <artifactId>h2</artifactId>
      <scope>test</scope>
</dependency>

然后我需要一个用于开发的 Maven 配置文件,它需要 h2 作为编译或运行时依赖项:

    <profile>
      <id>emb</id>
      <activation>
        <activeByDefault>false</activeByDefault>
      </activation>
      <dependencies>
        <!-- Using embedded database in development -->
        <dependency>
          <groupId>com.h2database</groupId>
          <artifactId>h2</artifactId>
          <scope>compile</scope>
        </dependency>
      </dependencies>
    </profile>

但它仍然在 "Cannot load driver class: org.h2.Driver" 上失败,因为它仅使用测试范围。

当我从依赖项中删除测试范围规范时,它起作用了,但它不是我想要的,因为我不想在生产中使用它。

有没有可能如何根据配置文件重写依赖范围?

profile标签可以在你pom的任意层级,设置2组属性即可。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.greg</groupId>
    <artifactId>profile-boot-example</artifactId>
    <version>0.1.0</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.10.RELEASE</version>
    </parent>

    <profiles>
        <profile>
            <id>prod</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <h2.scope>test</h2.scope>
            </properties>
        </profile>
        <profile>
            <id>emb</id>
            <activation>
                <activeByDefault>false</activeByDefault>
            </activation>
            <properties>
                <h2.scope>compile</h2.scope>
            </properties>
        </profile>
    </profiles>

    <dependencies>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>${h2.scope}</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <artifactId>maven-failsafe-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>integration-test</goal>
                            <goal>verify</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

最后我发现依赖范围覆盖工作正常。

足以 运行 使用选定的配置文件构建 maven,如下所示:

mvn clean install -P emb

您还可以使用以下方法检查依赖范围:

mvn -P emb dependency:analyze

问题 与 运行ning spring-boot 应用程序有关:

当我使用 eclipse/idea 或

mvn spring-boot:run

它因缺少 h2 驱动程序而失败 - 可能是因为 运行 另一个构建没有正确的配置文件。

解决方案 是 运行 spring-在使用简单 java:

构建 maven 后启动应用程序
java -jar target/your-app.jar --spring.profiles.active=emb

(spring 个人资料在这种情况下是另一回事 - 添加只是为了获得完整信息)