使用maven将多个资源目录复制到独立目标目录

时间:2013-10-16 16:52:26

标签: java maven

Maven resources plugin

  

此目标要求您配置要复制的资源,并指定outputDirectory。

使用maven将basedir中的两个(或更多)外部资源目录复制到构建输出目录(请参阅blahuggh)。

${basedir}/ 
  - pom.xml
  - blah/
  - uggh/
  - src/
    - main/..
    - test/..
  - target/
    - classes/..
    - blah/
    - uggh/

例如,给定上面的目录结构使用maven将blahuggh复制到目标目录。复制一个或另一个很容易,但插件只接受一个outputDirectory。如果将target目录和两个目录指定为资源,则每个目录的内容将复制到target,但不会复制到目录本身。

插件的额外使用会覆盖初始插件。此外,我已尝试指定整个basedir并仅包含所需的目录。这不会复制任何内容。

以下是复制单个目录的示例:

  <plugin>
    <artifactId>maven-resources-plugin</artifactId>
    <version>2.6</version>
    <executions>
      <execution>
        <id>copy-resources</id>
        <phase>validate</phase>
        <goals>
          <goal>copy-resources</goal>
        </goals>
        <configuration>
          <outputDirectory>${basedir}/target/blah</outputDirectory>
          <resources>
            <resource>
                <directory>blah</directory>
                <filtering>true</filtering>
            </resource>
          </resources>
        </configuration>
      </execution>
    </executions>
  </plugin>

9 个答案:

答案 0 :(得分:76)

这是文件结束的地方:

<outputDirectory>${basedir}/target/blah</outputDirectory>

这是从以下地方复制的地方:

<directory>src/main/otherresources</directory>

会有一个<include><includes>标签来告诉文件名

<强>倍数

对于多个文件夹,您需要具有不同<execution>的多个<id>

  <plugin>
    <artifactId>maven-resources-plugin</artifactId>
    <version>2.6</version>
    <executions>
      <execution>
        <id>copy-resources-1</id>
        <phase>validate</phase>
        <goals>
          <goal>copy-resources</goal>
        </goals>
        <configuration>
          <outputDirectory>${basedir}/target/blah</outputDirectory>
          <resources>
            <resource>
                <directory>blah</directory>
                <filtering>true</filtering>
            </resource>
          </resources>
        </configuration>
      </execution>
      <execution>
        <id>copy-resources-2</id>
        <phase>validate</phase>
        <goals>
          <goal>copy-resources</goal>
        </goals>
        <configuration>
          <outputDirectory>${basedir}/target/ughh</outputDirectory>
          <resources>
            <resource>
                <directory>ughh</directory>
                <filtering>true</filtering>
            </resource>
          </resources>
        </configuration>
      </execution>
    </executions>
  </plugin>

答案 1 :(得分:13)

对我来说,这个在Maven 3中效果很好:

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-resources-plugin</artifactId>
            <version>2.5</version>
            <executions>
                <execution>
                    <id>custom-resources</id>
                    <phase>process-resources</phase>
                    <goals>
                        <goal>copy-resources</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${project.build.directory}</outputDirectory>
                        <resources>                                        
                            <resource>
                                <targetPath>${basedir}/target/blah</targetPath>
                                <directory>blah</directory>
                                <filtering>true</filtering>
                            </resource>             
                            <resource>
                                <targetPath>${basedir}/target/uggh</targetPath>
                                <directory>uggh</directory>
                                <filtering>false</filtering>
                            </resource>              
                        <encoding>UTF-8</encoding>
                    </configuration>            
                </execution>
            </executions>
        </plugin>

答案 2 :(得分:9)

这是我发现的更简单的解决方案,它正在运作......

<build>
    <plugins>
        <plugin>
            <artifactId>maven-resources-plugin</artifactId>
            <version>2.7</version>
            <configuration>   
            </configuration>
        </plugin>
    </plugins> 
    <resources>
        <resource>
            <directory>${basedir}/src/main/java/org/mc2/mymusic/gui/main/Menu/resources</directory>
            <targetPath>${basedir}/target/classes/org/mc2/mymusic/gui/main/Menu/resources</targetPath>
            <filtering>false</filtering>
        </resource>
    </resources>  
</build>

答案 3 :(得分:6)

您可以使用蚂蚁风格的模式

<plugin>
    <artifactId>maven-resources-plugin</artifactId>
    <version>2.7</version>
    <executions>
        <execution>
            <id>copy-resources</id>
            <phase>validate</phase>
            <goals>
                <goal>copy-resources</goal>
            </goals>
            <configuration>
                <outputDirectory>${project.build.directory}</outputDirectory>
                <resources>
                    <resource>
                        <directory>${basedir}</directory>
                        <includes>
                            <include>blah/**</include>
                            <include>uggh/**</include>
                        </includes>
                    </resource>
                </resources>
            </configuration>
        </execution>
    </executions>
</plugin>

答案 4 :(得分:2)

阅读您的示例我认为您不必包含和配置maven-resource-plugin。 只需将这些资源元素添加到<build><resources/> - 标记即可。请参阅http://maven.apache.org/ref/3.1.1/maven-model/maven.html#class_resource您可以使用的其他标签。

答案 5 :(得分:1)

    <resources>
        <resource>
              <directory>${basedir}/src/scripts</directory>
              <includes>
                  <include>data-octopus.ps1</include>
              </includes>
              <targetPath>${basedir}/target/data</targetPath>
        </resource>
        <resource>
              <directory>${basedir}/src/scripts</directory>
              <includes>
                  <include>service-octopus.ps1</include>
              </includes>
              <targetPath>${basedir}/target/service</targetPath>
        </resource>
    </resources>

    </plugins>
        ...
    </plugins>

答案 6 :(得分:0)

        <resources>
          <resource>
            <directory>src/main/resources</directory>
            <filtering>false</filtering>
            <targetPath>${basedir}/target</targetPath>
          </resource>
          <resource>
            <directory>src/main/resources</directory>
            <filtering>false</filtering>
            <targetPath>${basedir}/target/classes</targetPath>
          </resource>
       </resources>

    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.6.1</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-resources-plugin</artifactId>
            <version>3.0.2</version>
            <configuration>
                <nonFilteredFileExtensions>
                    <nonFilteredFileExtension>jks</nonFilteredFileExtension>
                </nonFilteredFileExtensions>
                <executions>
                    <execution>
                        <id>copy-resources-1</id>
                        <phase>validate</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                    </execution>
                </executions>
            </configuration>
        </plugin>
   </plugins>

Maven - Copying resources into multiple targets

答案 7 :(得分:0)

Maven隐藏所有内容以使其更易于编码。 有几种方法可以实现这一目标。

  1. 在“资源”插件中编辑默认执行。 (最简单的) 这也可以使用include标记或其他资源来编写
  2. 在Resources插件中写入不同的执行。
  3. 使用Antrun插件。 (您最好将整个构建用ant编写)
  4. Maven复制重命名插件。
  5. 还有我在这里没有提到的许多其他方式。...

编辑默认插件-

<resources>
    <resource>
        <directory>${basedir}<directory>
        <includes>
            <include>blah</include>
            <include>ughh</include>
        </includes>
    <resource>
<resources>
<plugins>
    <plugin>
        <artifactId>maven-resources-plugin</artifactId>
        <configuration>
            <outputDirectory>${basedir}/target</outputDirectory>
        </configuration>
    </plugin>
</plugins>

答案 8 :(得分:0)

如果您要复制更多目录或文件-更好的选择:

  <plugin>
    <artifactId>maven-resources-plugin</artifactId>
    <version>2.6</version>
    <executions>
      <execution>
        <id>copy-resources</id>
        <phase>validate</phase>
        <goals>
          <goal>copy-resources</goal>
        </goals>
        <configuration>
          <tasks>
            <copy todir="${basedir}/target/blah" overwrite="true">
                 <fileset dir="blah"/>
            </copy>
            <copy file="${basedir}/target/blah/somefile"
                  todir="../target_webapp_eclaims/WEB-INF" overwrite="true"/>
          </tasks>
        </configuration>
      </execution>
    </executions>
  </plugin>