[英]Maven project runs sql-maven-plugin executions from dependency
How can I configure my Maven pom.xml
and/or sql-maven-plugin
so that the unit test related DB executions I've specified in my dependency, lib.example.jdbc
, don't get executed when building the pom.xml
of my project com.example.project
? 如何配置Maven pom.xml
和/或sql-maven-plugin
以便在构建pom.xml
时不执行我在依赖项lib.example.jdbc
指定的与单元测试相关的数据库执行我的项目com.example.project
? Currently when I run mvn clean install
on the pom.xml
for com.example.project
I find that my SQL database has tables that were created in lib.example.jdbc
. 当前,当我在com.example.project
的pom.xml
上运行mvn clean install
时,我发现我的SQL数据库具有在lib.example.jdbc
中创建的lib.example.jdbc
。 One obvious workaround is to use different properties for mysql.database
in each pom.xml
file but i feel the bigger problem is that the library's (dependency) db executions are being run in the test phase of the main project. 一个明显的解决方法是在每个pom.xml
文件中为mysql.database
使用不同的属性,但是我觉得更大的问题是,库(依赖)的db执行正在主项目的测试阶段中运行。 I cant imagine this being correct since one can have hundreds of pom.xml
dependencies with their own tests and db executions that shouldn't be run when building the final project. 我无法想象这是正确的,因为在构建最终项目时,可能有数百个pom.xml
依赖项及其自己的测试和数据库执行不应该运行。
UPDATE: A Spring annotation was the culprit as discovered in my answer so I updated the question tags to reflect that. 更新:Spring注释是我在答案中发现的罪魁祸首,所以我更新了问题标签以反映这一点。
Here is the relevant sample from the pom.xml
of lib.example.jdbc
: 这是lib.example.jdbc
的pom.xml
中的相关示例:
...
<groupId>lib.example</groupId>
<artifactId>jdbc</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
...
<properties>
...
<mysql.port>3306</mysql.port>
<mysql.database>testDb</mysql.database>
<mysql.user>root</mysql.user>
<mysql.pass>password</mysql.pass>
</properties>
...
<build>
...
<plugins>
...
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>sql-maven-plugin</artifactId>
<version>1.5</version>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.44</version>
</dependency>
</dependencies>
<configuration>
<driver>com.mysql.jdbc.Driver</driver>
<url>jdbc:mysql://${mysql.host}:${mysql.port}/${mysql.database}?useSSL=false</url>
<username>${mysql.user}</username>
<password>${mysql.pass}</password>
<settingsKey>sensibleKey</settingsKey>
<!--all executions are ignored if -Dmaven.test.skip=true-->
<skip>${maven.test.skip}</skip>
</configuration>
<executions>
<execution>
<id>create-db</id>
<phase>process-test-resources</phase>
<goals>
<goal>execute</goal>
</goals>
<configuration>
<url>jdbc:mysql://${mysql.host}:${mysql.port}?useSSL=false</url>
<autocommit>true</autocommit>
<sqlCommand>create database if not exists `${mysql.database}`</sqlCommand>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
And here is the relevant sample from the pom.xml
of com.example.project
: 这是com.example.project
的pom.xml
中的相关示例:
<groupId>com.example</groupId>
<artifactId>project</artifactId>
<version>0.0.1</version>
...
<properties>
...
<mysql.port>3306</mysql.port>
<mysql.database>testDb</mysql.database>
<mysql.user>root</mysql.user>
<mysql.pass>password</mysql.pass>
</properties>
...
<dependencies>
...
<dependency>
<groupId>lib.example</groupId>
<artifactId>jdbc</artifactId>
<version>1.0.0</version>
</dependency>
...
</dependencies>
...
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
...
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>sql-maven-plugin</artifactId>
<version>1.5</version>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.44</version>
</dependency>
</dependencies>
<configuration>
<driver>com.mysql.jdbc.Driver</driver>
<url>jdbc:mysql://${mysql.host}:${mysql.port}?useSSL=false</url>
<username>${mysql.user}</username>
<password>${mysql.pass}</password>
<settingsKey>sensibleKey</settingsKey>
<!--all executions are ignored if -Dmaven.test.skip=true-->
<skip>${maven.test.skip}</skip>
</configuration>
<executions>
<execution>
<id>drop-db-before-test-if-any</id>
<phase>process-test-resources</phase>
<goals>
<goal>execute</goal>
</goals>
<configuration>
<url>jdbc:mysql://${mysql.host}:${mysql.port}?useSSL=false</url>
<autocommit>true</autocommit>
<sqlCommand>drop database if exists `${mysql.database}`</sqlCommand>
<sqlCommand>create database `${mysql.database}`</sqlCommand>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
After further investigation, it seems the problem is not with the sql-maven-plugin
but instead with the way a Spring annotation (@Sql) gets executed when running com.example.project
tests from a test scoped configuration in lib.example.jdbc
. 经过进一步调查,似乎问题不是出在sql-maven-plugin
而是问题在于从lib.example.jdbc
的测试范围配置运行com.example.project
测试时执行Spring批注(@Sql)的方式。 Here is the class level annotation in lib.example.jdbc
that was causing problems for me. 这是lib.example.jdbc
中的类级别注释,这对我造成了问题。 I moved the code from my schema.sql
and data.sql
into my pom.xml
<sqlCommand>
block and it was no longer executed when testing com.example.project
. 从我的移动的代码schema.sql
和data.sql
到我pom.xml
<sqlCommand>
块和测试时,它不再执行com.example.project
。
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {TestConfig.class})
@Sql(executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD, scripts = {"classpath:schema.sql","classpath:data.sql"})
public class DataDaoTest {
...
@Test
public void testRetrieval() throws Exception {
...
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.