Mic*_*čík 3 java h2 dependency-management maven spring-boot
我有基于Maven的spring-boot应用程序。
我想将h2数据库仅作为测试的依赖项,因此它具有以下内容:
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>test</scope>
</dependency>
Run Code Online (Sandbox Code Playgroud)
那么我需要一个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>
Run Code Online (Sandbox Code Playgroud)
但是由于仅使用测试范围,它在“无法加载驱动程序类: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>
Run Code Online (Sandbox Code Playgroud)
最后我发现依赖范围覆盖工作正常。
使用选定的配置文件运行 Maven 构建就足够了,如下所示:
mvn clean install -P emb
Run Code Online (Sandbox Code Playgroud)
您还可以使用以下方法检查依赖范围:
mvn -P emb dependency:analyze
Run Code Online (Sandbox Code Playgroud)
问题是运行spring-boot应用程序:
当我使用 eclipse/idea 或
mvn spring-boot:run
Run Code Online (Sandbox Code Playgroud)
它因缺少 h2 驱动程序而失败 - 可能是因为在没有正确配置文件的情况下运行另一个版本。
解决方案是在使用简单的 java 构建 Maven 后运行 spring-boot 应用程序:
java -jar target/your-app.jar --spring.profiles.active=emb
Run Code Online (Sandbox Code Playgroud)
(本例中的 spring 配置文件是另一个故事 - 添加只是为了获取完整信息)
| 归档时间: |
|
| 查看次数: |
1951 次 |
| 最近记录: |