Maven:如何创建临时文件夹?

时间:2014-08-14 16:24:35

标签: java maven

我使用sql-maven-plugin执行SQL进行集成测试,看起来像

            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>sql-maven-plugin</artifactId>
                <version>${sql-maven-plugin.version}</version>
                <executions>
                    <execution>
                        <id>default</id>
                        <phase>compile</phase>
                        <goals>
                            <goal>execute</goal>
                        </goals>
                        <configuration>
                            <url>${dataSource.url}</url>
                            <!-- no transaction -->
                            <autocommit>true</autocommit>
                            <sqlCommand>CREATE TABLE User
                            (
                                id             VARCHAR(255) NOT NULL,
                                userExternalId VARCHAR(255) NOT NULL,
                                email          VARCHAR(255) NOT NULL,
                                clientId       VARCHAR(255) NOT NULL,
                                clientSecret   VARCHAR(255) NOT NULL,
                                active         BOOLEAN,
                                PRIMARY KEY (id)
                            )</sqlCommand>
                        </configuration>
                    </execution>
                </executions>

                <dependencies>
                    <!-- specify the dependent jdbc driver here -->
                    <dependency>
                        <groupId>com.h2database</groupId>
                        <artifactId>h2</artifactId>
                        <version>${h2.version}</version>
                    </dependency>
                </dependencies>

                <configuration>
                    <driver>${dataSource.driver}</driver>
                    <url>${dataSource.url}</url>
                    <username>${dataSource.user}</username>
                    <password>${dataSource.password}</password>
                </configuration>

            </plugin>

我配置数据源的方式是

            <configuration>
                <driver>${dataSource.driver}</driver>
                <url>${dataSource.url}</url>
                <username>${dataSource.user}</username>
                <password>${dataSource.password}</password>
            </configuration>

当我运行mvn clean install时,我将问题视为

[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.022s
[INFO] Finished at: Thu Aug 14 16:13:20 UTC 2014
[INFO] Final Memory: 14M/91M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.codehaus.mojo:sql-maven-plugin:1.5:execute (default) on project integration: Table USER already exists; SQL statement:
[ERROR] CREATE TABLE User
[ERROR] (
[ERROR] id VARCHAR(255) NOT NULL,
[ERROR] userExternalId VARCHAR(255) NOT NULL,
[ERROR] email VARCHAR(255) NOT NULL,
[ERROR] clientId VARCHAR(255) NOT NULL,
[ERROR] clientSecret VARCHAR(255) NOT NULL,
[ERROR] active BOOLEAN,
[ERROR] PRIMARY KEY (id)
[ERROR] )
[ERROR] [42101-180]
[ERROR] -> [Help 1]
[ERROR]

这发生在我的CI管道中。我怀疑这种情况正在发生,因为数据库从上一次运行mvn clean install

出现

所以我尝试用${datasource.url}作为

<dataSource.url>jdbc:h2:${project.build.outputDirectory}/oauth;create=true</dataSource.url> 

<dataSource.url>jdbc:h2:${project.build.testOutputDirectory}/oauth;create=true</dataSource.url>  

但他们都没有工作

问题
如何在每次运行时创建临时目录,或在运行集成测试时使用maven清理现有数据库?

2 个答案:

答案 0 :(得分:0)

你可以使用ant插件,你可以在给定阶段创建一个文件夹,然后你可以在另一个阶段删除它:

<plugin>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>1.1</version>
    <executions>
        <execution>
            ....
            <configuration>
                 <task>
                    <mkdir>./your_directory_to create</mkdir>
                </task>
            </configuration>
        </execution>
    </executions>
</plugin>

答案 1 :(得分:0)

您应该创建另一个绑定到clean目标的插件实例,该目标执行sql以准备数据库以用于以后的目标。例如,此sql可以删除在先前版本中创建的表。