从Maven中的项目复制文件

时间:2010-08-06 12:30:21

标签: maven

在某些Maven阶段,是否可以将文件夹从我的项目复制到某个位置?有人知道吗?

3 个答案:

答案 0 :(得分:29)

Maven执行此操作的方法是使用copy-resources中的maven-resources-plugin目标

来自http://maven.apache.org/plugins/maven-resources-plugin/examples/copy-resources.html

<project>
  ...
  <build>
    <plugins>
      <plugin>
        <artifactId>maven-resources-plugin</artifactId>
        <version>2.7</version>
        <executions>
          <execution>
            <id>copy-resources</id>
            <!-- here the phase you need -->
            <phase>validate</phase>
            <goals>
              <goal>copy-resources</goal>
            </goals>
            <configuration>
              <outputDirectory>${basedir}/target/extra-resources</outputDirectory>
              <resources>          
                <resource>
                  <directory>src/non-packaged-resources</directory>
                  <filtering>true</filtering>
                </resource>
              </resources>              
            </configuration>            
          </execution>
        </executions>
      </plugin>
    </plugins>
    ...
  </build>
  ...
</project>

答案 1 :(得分:21)

看看maven-antrun插件。您可以在任何maven阶段复制文件,如下所示:

    <plugin>
      <artifactId>maven-antrun-plugin</artifactId>
      <version>1.4</version>
      <executions>
        <execution>
          <id>copy</id>
          <phase>compile</phase>
          <configuration>
            <tasks>
              <copy file="myFileSource" tofile="MyFileDest"/>
            </tasks>
          </configuration>
          <goals>
            <goal>run</goal>
          </goals>
        </execution> 
      </executions>
    </plugin>

答案 2 :(得分:3)

类似于@mort's one的解决方案 maven-antrun-plugin 1.8

<plugin>
  <artifactId>maven-antrun-plugin</artifactId>
  <version>1.8</version>
  <executions>
    <execution>
      <id>copy</id>
      <phase>compile</phase>
      <configuration>
        <target>
          <copy file="sourceFile" tofile="targetFile"/>
        </target>
      </configuration>
      <goals>
        <goal>run</goal>
      </goals>
    </execution> 
  </executions>
</plugin>

请注意,从 maven-antrun-plugin 1.5 开始,不推荐使用<tasks>节点,而选择<target>节点。