【问题标题】:Maven 3 :How to copy a file to test directory , when the file does not existMaven 3:当文件不存在时如何将文件复制到测试目录
【发布时间】:2014-03-11 07:29:45
【问题描述】:

我想在 Maven 阶段测试期间将文件从 src 目录复制到 test/resource 目录,当且仅当文件在 test/resource 目录中不存在时。有没有人知道我们如何实现这一点?提前感谢

【问题讨论】:

    标签: jakarta-ee ant maven-3 maven-plugin


    【解决方案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://stackoverflow.com/a/13701310/1410035 提供的“向插件添加依赖项”解决方案。

    此示例中值得注意的变化是:

    【讨论】:

      【解决方案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 语句?
      • 是的。看起来条件语句没有执行。
      猜你喜欢
      • 1970-01-01
      • 2017-01-01
      • 1970-01-01
      • 2023-04-02
      • 2013-06-08
      • 1970-01-01
      • 1970-01-01
      • 2022-12-03
      • 1970-01-01
      相关资源
      最近更新 更多