【发布时间】:2017-03-05 20:57:45
【问题描述】:
我在我的 Maven 项目中添加了 this Stack Overflow question 中建议的解决方案。与我介绍的建议解决方案的唯一区别是将<tasks /> 替换为<target />(我遇到的问题出现在两者中)。
在测试方面一切都很好。当我运行测试时,正在使用正确的 (test-persistence.xml) 持久性文件。但是,当我从我的 IDE(Netbeans 8.2)进行全新安装甚至点击运行时,只有第一个目标(copy-test-persistence)正在执行。在测试之后进入第二次执行(请参阅下面的构建输出),但未执行目标。在每个clean install 之后以及在服务器上运行应用程序时,我得到的是test-persistence.xml 的内容在persistence.xml 文件中。正确的内容保留在第一个目标中创建的persistence.xml.proper 中。
--- maven-antrun-plugin:1.8:run (copy-test-persistence) @ RimmaNew ---
Executing tasks
main:
[copy] Copying 1 file to /my-project-home/target/classes/META-INF
[copy] Copying 1 file to /my-project-home/target/classes/META-INF
Executed tasks
...
--- maven-antrun-plugin:1.8:run (restore-persistence) @ RimmaNew ---
Executing tasks
main:
Executed tasks
您会注意到restore-persistence 下执行了0 个任务。奇怪的是,在创建的 /target/antrun 文件夹中有一个 build-main.xml 文件,其中包含跳过的任务:
<?xml version="1.0" encoding="UTF-8" ?>
<project name="maven-antrun-" default="main" >
<target name="main">
<copy file="/home/vgorcinschi/NetBeansProjects/rimmanew/target/classes/META-INF/persistence.xml.proper" tofile="/home/vgorcinschi/NetBeansProjects/rimmanew/target/classes/META-INF/persistence.xml"/>
</target>
</project>
如果您能给我一个提示,我们将不胜感激,因为我无法理解这一点。因为这很常见,我发布了我当前的pom.xml:
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<id>copy-test-persistence</id>
<phase>process-test-resources</phase>
<configuration>
<target>
<!--backup the "proper" persistence.xml-->
<copy file="${project.build.outputDirectory}/META-INF/persistence.xml" tofile="${project.build.outputDirectory}/META-INF/persistence.xml.proper" />
<!--replace the "proper" persistence.xml with the "test" version-->
<copy file="${project.build.testOutputDirectory}/META-INF/test-persistence.xml" tofile="${project.build.outputDirectory}/META-INF/persistence.xml" />
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
<execution>
<id>restore-persistence</id>
<phase>prepare-package</phase>
<configuration>
<target>
<!--restore the "proper" persistence.xml-->
<copy file="${project.build.outputDirectory}/META-INF/persistence.xml.proper" tofile="${project.build.outputDirectory}/META-INF/persistence.xml" />
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
【问题讨论】:
标签: java maven pom.xml persistence.xml maven-antrun-plugin