首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Maven项目从依赖项运行sql-maven-plugin执行。

Maven项目从依赖项运行sql-maven-plugin执行。
EN

Stack Overflow用户
提问于 2017-11-29 03:35:43
回答 1查看 2.8K关注 0票数 0

如何配置Maven pom.xml 和/或sql-maven-plugin ,以便当我在pom.xml上运行com.example.projectcom.example.project时,在构建项目 com.example.project**?**的** pom.xml 时,不要执行在依赖项中指定的与单元测试相关的DB执行,在lib.example.jdbc中创建的表。一个明显的解决方法是在每个mysql.database文件中对pom.xml使用不同的属性,但我觉得更大的问题是库的(依赖) db执行正在主项目的测试阶段运行。我无法想象这是正确的,因为在构建最终项目时不应该运行的有数百个pom.xml依赖项和它们自己的测试和db执行。

更新:正如我在回答中发现的那样,Spring注释是罪魁祸首,因此我更新了问题标签以反映这一点。

以下是来自pom.xml of lib.example.jdbc的相关示例

代码语言:javascript
复制
...
<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>

下面是来自pom.xml of com.example.project的相关示例

代码语言:javascript
复制
<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>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-11-30 01:10:47

经过进一步研究,问题似乎不是sql-maven-plugin,而是在从lib.example.jdbc中的测试范围的配置运行com.example.project测试时执行Spring注释(@Sql)的方式。下面是lib.example.jdbc中的类级注释,它给我带来了问题。我将代码从schema.sqldata.sql移到pom.xml <sqlCommand>块中,在测试com.example.project时不再执行。

代码语言:javascript
复制
@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 {
        ...
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47544640

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档