Copying multiple resource directories to independent target directories with maven
Asked Answered
F

9

68

The Maven resources plugin:

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

Copy two (or more) external resource directories within the basedir to the build output directory using maven (see blah and uggh).

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

For example, given the directory structure above copy blah and uggh to 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 target directory and both directories as resources, then the contents of each directory gets copied to target but not the directories themselves.

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

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>
Farnese answered 16/10, 2013 at 16:52 Comment(0)
P
107

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)

Multiples

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

  <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>
Purcell answered 16/10, 2013 at 16:56 Comment(3)
where are the folders on file systen? src/main/resources/ughh ?Radom
i am using maven 3.3.3 is this still possible?Radom
${basedir} is the root folder of your workspace. It is the folder with the pom.xml in it.Purcell
P
23

For me this one works well in 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>
Percipient answered 16/10, 2013 at 17:12 Comment(4)
This would dump all the contents of blah and uggh to target but not the directories themselves in my case. Not exactly what I'm looking for but thanks for the suggestionFarnese
It's not clear to me from the config alone, how I would expect this to behave. From the Maven docs, <targetPath> " is relative to the target/classes directory". maven.apache.org/ref/3.2.2/maven-model/apidocs/org/apache/maven/…Inversion
I just tried this out and it also works for the "maven-war-plugin" as wellTegument
As I noticed u ever don't need to set <outputDirectory> if u specify the <targetPath> for both different directories is differentAcropetal
U
13

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>
Upheld answered 19/11, 2014 at 17:35 Comment(2)
works for me even without filtering section (as I use it for plain files), nice and neat syntaxHelmand
This works for me without the plugin element. I just used the resources element.Cleveland
I
11

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>
Independency answered 4/3, 2015 at 12:10 Comment(0)
H
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>
Helmand answered 10/10, 2017 at 1:51 Comment(0)
R
3

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_resource which other tags you can use.

Risibility answered 16/10, 2013 at 17:11 Comment(1)
You are right. It was just to show a slightly less verbose way to use maven-resource-plugin. And for this case, to preserve the source folder names on output directory.Independency
A
0

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

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

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>
Aleras answered 7/7, 2019 at 15:54 Comment(0)
D
0

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>
Dug answered 22/5, 2020 at 7:37 Comment(0)
G
-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 - Copying resources into multiple targets

Gibbet answered 18/6, 2019 at 20:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.