Maven 3:当文件不存在时如何将文件复制到测试目录
Posted
技术标签:
【中文标题】Maven 3:当文件不存在时如何将文件复制到测试目录【英文标题】:Maven 3 :How to copy a file to test directory , when the file does not exist 【发布时间】:2014-03-11 07:29:45 【问题描述】:我想在 Maven 阶段测试期间将文件从 src 目录复制到 test/resource 目录,当且仅当文件在 test/resource 目录中不存在时。有没有人知道我们如何实现这一点?提前感谢
【问题讨论】:
【参考方案1】:这是@Saif Asif 答案的更新版本,它在 Maven3 上对我有用:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<phase>test</phase>
<configuration>
<target>
<taskdef resource="net/sf/antcontrib/antlib.xml" classpathref="maven.dependency.classpath" />
<if>
<available file="/path/to/your/file "/>
<then>
<!-- Do something with it -->
<copy file="/your/file" tofile="/some/destination/path" />
</then>
</if>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>ant-contrib</groupId>
<artifactId>ant-contrib</artifactId>
<version>1.0b3</version>
<exclusions>
<exclusion>
<groupId>ant</groupId>
<artifactId>ant</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.ant</groupId>
<artifactId>ant-nodeps</artifactId>
<version>1.8.1</version>
</dependency>
</dependencies>
</plugin>
感谢https://***.com/a/13701310/1410035 提供的“向插件添加依赖项”解决方案。
此示例中值得注意的变化是:
将插件更新到 1.8 版(撰写本文时最新) 根据https://***.com/a/4372491/1410035 更新了taskdef 以具有classpathref 根据https://***.com/a/13569218/1410035 更改了 taskdef classpathref 以使用 maven3【讨论】:
【参考方案2】:您可以使用copy-maven-plugin
和runIf
检查文件是否存在。
【讨论】:
感谢@UR6LAD,这个插件做得很好。文件复制按预期工作。 ,我们的客户对这个插件不满意...... :(。他们说这个插件似乎向 maven 引入了一种程序性“脚本”,对我来说就像与 maven 与例如 ant 的区别相反。我使用的插件的核心逻辑如下。请让我知道它们是否有其他选择。使用Maven AntRun plugin 来完成此操作。在你的 pom.xml 中,使用类似
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.6</version>
<executions>
<execution>
<phase>test</phase>
<configuration>
<tasks>
<taskdef resource="net/sf/antcontrib/antcontrib.properties" />
<if>
<available file="/path/to/your/file "/>
<then>
<!-- Do something with it -->
<copy file="/your/file" tofile="/some/destination/path" />
</then>
</if>
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
【讨论】:
感谢@Saif 的响应。当我尝试您的代码时,我得到以下异常.. [错误] 无法执行目标 org.apache.maven.plugins:maven-antrun-plugin:1.6:在项目上运行(默认) *****:发生了 Ant BuildException:问题:如果 [ERROR],则无法创建任务或类型 原因:名称未定义。 [错误] 行动:检查拼写。 [错误] 操作:检查是否已声明任何自定义任务/类型。 [错误] 操作:检查所有以上是关于Maven 3:当文件不存在时如何将文件复制到测试目录的主要内容,如果未能解决你的问题,请参考以下文章
如果该文件夹不存在,如何通过创建文件夹将文件从一个目录复制到另一个目录