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-pluginrunIf 检查文件是否存在。

【讨论】:

感谢@UR6LAD,这个插件做得很好。文件复制按预期工作。 ,我们的客户对这个插件不满意...... :(。他们说这个插件似乎向 maven 引入了一种程序性“脚本”,对我来说就像与 maven 与例如 ant 的区别相反。我使用的插件的核心逻辑如下。请让我知道它们是否有其他选择。 !new File("src/test/resources/ systemConfig.properties" ).exists() src/test/resourcesdevelopment/env/systemConfig.properties。提前致谢。 【参考方案3】:

使用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],则无法创建任务或类型 原因:名称未定义。 [错误] 行动:检查拼写。 [错误] 操作:检查是否已声明任何自定义任务/类型。 [错误] 操作:检查所有 / 声明是否已生效。 您是否定义了任何名为 antcontrib.properties 的任务?如果没有,请删除该行 正如你在代码中提到的那样,。这是我在 pom.xml 中唯一拥有的 enyrty。我已经厌倦了从 pom not build 中删除此条目执行正常。但是复制不起作用。以下代码我用于文件复制及其工作正常。但是,如果它们在 src 中有一些更改,则它的覆盖目标文件不会发生。 您的意思是说它不适用于 if-then 语句? 是的。看起来条件语句没有执行。

以上是关于Maven 3:当文件不存在时如何将文件复制到测试目录的主要内容,如果未能解决你的问题,请参考以下文章

如果该文件夹不存在,如何通过创建文件夹将文件从一个目录复制到另一个目录

Day5-1 cp mv和cat等查看命令

在Maven构建期间找不到来自外部jar的类文件? [复制]

在 Maven 构建期间将文件夹从源复制到目标

linux如何复制文件夹下所有文件但不复制子目录

仅当文件尚不存在时才将行附加到文件中