Java 在命令行上运行目标的所有绑定实例

Java 在命令行上运行目标的所有绑定实例,java,maven-2,Java,Maven 2,我有一堆SQL命令绑定到集成前测试阶段,其任务是创建一个“测试”数据库并将应用程序指向它 有时,我只想“重建”我的测试数据库,而不需要生命周期中的所有其他东西。例如,如果我的测试发生了灾难性的失败并破坏了测试数据库,我可能需要多次重建它,直到我找到问题所在 以下是我的POM的外观: <profile> <id>test-setup-teardown</id> <activation> <active

我有一堆SQL命令绑定到集成前测试阶段,其任务是创建一个“测试”数据库并将应用程序指向它

有时,我只想“重建”我的测试数据库,而不需要生命周期中的所有其他东西。例如,如果我的测试发生了灾难性的失败并破坏了测试数据库,我可能需要多次重建它,直到我找到问题所在

以下是我的POM的外观:

<profile>
    <id>test-setup-teardown</id>
       <activation>
           <activeByDefault>true</activeByDefault>
       </activation>
       <build>
           <plugins>
               <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>sql-maven-plugin</artifactId>
                    <version>1.3</version>

                    <dependencies>
                        <dependency>
                            <groupId>${database-dependency-groupId}</groupId>
                            <artifactId>${database-dependency-artifactId}</artifactId>
                            <version>${database-dependency-version}</version>
                        </dependency>
                    </dependencies>

                    <configuration>
                        <url>${test-database-admin-url}</url>
                        <username>${test-database-admin-username}</username>
                        <password>${test-database-admin-password}</password>
                        <driver>${database-driver}</driver>
                        <autocommit>true</autocommit>
                    </configuration>

                    <executions>
                        <execution>
                            <id>test-database-pre-setup</id>
                            <phase>pre-integration-test</phase>
                            <goals>
                                <goal>execute</goal>
                            </goals>
                            <configuration>
                                <sqlCommand>${test-database-teardown}</sqlCommand>
                                <onError>continue</onError>
                            </configuration>
                        </execution>

                        <execution>
                            <id>test-database-setup</id>
                            <phase>pre-integration-test</phase>
                            <goals>
                                <goal>execute</goal>
                            </goals>
                            <configuration>
                                <sqlCommand>${test-database-setup}</sqlCommand>
                            </configuration>
                        </execution>

                        <execution>
                            <id>test-database-schema</id>
                            <phase>pre-integration-test</phase>
                            <goals>
                                <goal>execute</goal>
                            </goals>
                            <configuration>
                                <url>${test-database-url}</url>
                                <username>${database-user}</username>
                                <password>${database-password}</password>
                                <srcFiles>
                                    <srcFile>${basedir}/metadata/build/database/${database-engine}/appx.sql</srcFile>
                                </srcFiles>
                            </configuration>
                        </execution>

                        <execution>
                            <id>test-database-teardown</id>
                            <phase>post-integration-test</phase>
                            <goals>
                                <goal>execute</goal>
                            </goals>
                            <configuration>
                                <sqlCommand>${test-database-teardown}</sqlCommand>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </profile>
简单地强调了一个事实,即非玩具项目在POM中有很多魔力。请给我指路

也许一个好的解决方案是从命令行按id运行执行的方法?

“预集成测试”是一个可以直接调用的阶段

mvn -Ptest-setup-teardown pre-integration-test
这仍将贯穿编译、复制资源、单元测试等生命周期。您可以尝试将
-DskipTests=true
添加到命令中(不确定这是否也将跳过集成测试阶段):

也许一个好的解决方案是从命令行按id运行执行

这是不可能的。执行是为了绑定到生命周期

调用
sql:execute
并在命令行上传递所有参数似乎不是一个可行的选项(这可以在批处理脚本中完成,但
srcFiles
可选参数无论如何都会有问题)

因此,在我看来,“不那么糟糕”的选项是在另一个配置文件中复制(叹气)sql maven插件的
设置,例如
rebuild
,在
validate
阶段绑定所需的执行,并在此配置文件中将
validate
目标声明为默认目标。这将允许键入如下内容:

mvn -Prebuild

也许可以通过一些优化来避免重复(通过像您那样改变执行阶段),但这对我来说意味着更多的黑魔法(这可能是不可取的)。

我特别尝试避免经历整个生命周期,因为进行预集成测试需要3-4分钟。我已经尝试将所有插件添加到配置文件中,以便可以轻松关闭它们,但这在尝试进行分支行为时增加了很多复杂性(例如,有时这样做,有时那样做,等等)。这种任务听起来像是最好在maven之外完成的任务类型,因为它实际上与构建主appGood没有多大关系,所以可以根据默认目标进行调用。我也不喜欢重复,但我同意这可能是最不坏的解决方案。谢谢
mvn -Ptest-setup-teardown pre-integration-test -DskipTests=true
mvn -Prebuild