我有基于 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>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)