62

Maven 资源插件:

此目标要求您配置要复制的资源,并指定 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>
4

9 回答 9

99

这是文件结束的地方:

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

这是它从以下位置复制的:

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

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

倍数

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

  <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>
于 2013-10-16T16:56:16.757 回答
19

对我来说,这个在 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>
于 2013-10-16T17:12:56.000 回答
13

这是我找到的更简单的解决方案,它正在工作......

<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>
于 2014-11-19T17:35:58.223 回答
10

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

<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>
于 2015-03-04T12:10:45.407 回答
4
    <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>
于 2017-10-10T01:51:06.680 回答
3

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

于 2013-10-16T17:11:29.953 回答
0

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

  1. 编辑资源插件中的默认执行。(最简单)这也可以使用包含标签或不同的资源来编写
  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>
于 2019-07-07T15:54:50.190 回答
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>
于 2020-05-22T07:37:11.710 回答
-1
        <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 - 将资源复制到多个目标

于 2019-06-18T20:22:19.293 回答