在配置文件中覆盖Maven依赖范围 - java

我有基于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”上失败,因为它仅使用测试范围。

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

有可能如何基于配置文件重写依赖关系范围吗?

参考方案

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>

休眠映射<键,设置<值>> - java

我有以下表格:@Entity @Table(name = "events") Event --id --name @Entity @Table(name = "state") State --id --name @Entity @Table(name = "action") Action --id …

使用Yaml而不是属性文件的Spring Boot - java

我使用Spring Boot。我想使用YAML而不是属性来编写配置。由于我使用spring-boot-starter,SnakeYAML库已经在类路径中,并且SpringApplication应该自动使用YAML版本。 SpringApplication类将自动支持YAML作为 每当启用SnakeYAML库时,就可以选择属性 您的课程路径。问题是应用程序继续…

无法从ArrayList <String>转换为List <Comparable> - java

当我写下面的代码时,编译器说 无法从ArrayList<String>转换为List<Comparable>private List<Comparable> get(){ return new ArrayList<String>(); } 但是当我用通配符编写返回类型时,代码会编译。private List&l…

合并List <T>和List <Optional <T >> - java

鉴于: List<Integer> integers = new ArrayList<>(Arrays.asList( 10, 12 )); List<Optional<Integer>> optionalIntegers = Arrays.asList( Optional.of(5), Optional.em…

Heroku Spring Boot Web服务始终返回404,但不在本地主机上 - java

我已经开发了一个Web服务,正在尝试将其部署到Heroku。看来成功了,尽管每当我尝试调用我的URL之一时,我都会得到一个404。我知道这意味着资源不可用,但在localhost上运行良好。Heroku部署的输出是标准的Spring引导,但缺少以下内容:2016-05-23 22:29:22.145 INFO 15785 --- [ main] s.w.s.…