【问题标题】:Override maven dependency scope in profile覆盖配置文件中的 Maven 依赖范围
【发布时间】:2018-12-10 15:50:24
【问题描述】:

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

我希望将 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>

但它仍然在“无法加载驱动程序类:org.h2.Driver”上失败,因为它仅使用测试范围。

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

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

【问题讨论】:

    标签: java maven spring-boot h2 dependency-management


    【解决方案1】:

    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>
    

    【讨论】:

    • 它看起来只是一个“标准”属性的例外,这就是我找到一些解决方案来覆盖范围的方式。
    • 谢谢,看起来好多了。明天我会试一试,然后告诉你。
    【解决方案2】:

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

    使用这样的选定配置文件运行 maven 构建就足够了:

    mvn clean install -P emb
    

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

    mvn -P emb dependency:analyze
    

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

    当我使用 eclipse/idea 或

    mvn spring-boot:run
    

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

    解决方案是在maven build后用简单的java运行spring-boot app:

    java -jar target/your-app.jar --spring.profiles.active=emb
    

    (在这种情况下,春季简介是另一回事 - 添加只是为了获得完整信息)

    【讨论】:

    • 顺便说一句--spring.profiles.active=emb 这个不错! :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-26
    • 1970-01-01
    • 2011-05-05
    • 1970-01-01
    • 2014-10-26
    • 1970-01-01
    相关资源
    最近更新 更多