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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19409220/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 16:55:45  来源:igfitidea点击:

Copying multiple resource directories to independent target directories with maven

javamaven

提问by bnjmn

The Maven resources plugin:

Maven的资源插件

This goal requires that you configure the resources to be copied, and specify the outputDirectory.

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

Copy two (or more) external resource directories within the basedirto the build output directory using maven (see blahand uggh).

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

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

For example, given the directory structure above copy blahand ugghto the target directory using maven. It is easy to copy one or the other, however, the plugin only accepts a single outputDirectory. If you specify the targetdirectory and both directories as resources, then the contents of each directory gets copied to targetbut not the directories themselves.

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

Additional use of the plugin overwrites the initial. Also, I've tried specifying the entire basedirand only including the desired directories. This does not copy anything.

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

Here is an example of copying a single directory:

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

  <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>

采纳答案by Lee Meador

This is where the file ends up:

这是文件结束的地方:

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

This is where it is copied from:

这是从这里复制的:

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

There would be an <include>or <includes>tag to tell the file name(s)

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

Multiples

倍数

You need multiple <execution>s with different <id>s for multiple folders:

对于多个文件夹,您需要多个<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>

回答by Robert Scholte

Reading your example I don't think you have to include&configure the maven-resource-plugin. Just add those resource-elements to the <build><resources/>-tag. See http://maven.apache.org/ref/3.1.1/maven-model/maven.html#class_resourcewhich other tags you can use.

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

回答by Boris Biryuchkov

For me this one works well in Maven 3:

对我来说,这个在 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>

回答by Marcoc1712

This is the simpler solution I've found and it's working...

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

<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>

Marco

马可

回答by Ricardo Veloso

You can use ant-style patterns

您可以使用蚂蚁样式的图案

<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>

回答by hit3k

    <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>

回答by Felix Aballi

        <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

Maven - 将资源复制到多个目标

回答by Prabhu

Maven hides everything to make it easier to code. There are several ways you can achieve this.

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

  1. Edit the default execution in Resources plugin. (Easiest) This also can be written using include tag or different resources
  2. Write different executions in Resources plugin.
  3. Use Antrun plugin. (You might as well write the whole build in ant)
  4. Maven Copy-rename plugin.
  5. And many other ways that I am not mentioning here....
  1. 编辑资源插件中的默认执行。(最简单)这也可以使用包含标签或不同的资源编写
  2. 在 Resources 插件中编写不同的执行。
  3. 使用 Antrun 插件。(你也可以用 ant 编写整个构建)
  4. Maven 复制重命名插件。
  5. 还有很多其他的方式,我在这里没有提到......

Edit the default plugin--

编辑默认插件--

<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>

回答by Oleksandr Bondarchuk

If you want to copy more directories or files - a better option:

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

  <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>