Maven: How to copy sub folders and their content i

2019-05-23 15:44发布

问题:

In a Maven project, I have directory, which I need the content from..

${basedir}/target/webapp-SNAPSHOT/WEB-INF/vendorspace/  
    |- dealo  
         |-static  
             |- css  
             |- images  
             |- js  
    |- blog  
         |- static
             |-css
             |-images
             |-js
    ------

And I want to copy the content of static directory to its parent directory as

${basedir}/target/tstatic/
     |- dealo  
          |- css  
          |- images  
          |- js  
     |- blog  
          |-css
          |-images
          |-js  

One solution is to give name of the folder like this:

 <copy todir="${basedir}/target/vstatic/dealo">
   <fileset dir="${basedir}/target/webapp-SNAPSHOT/WEB-INF/vendorspace/dealo/static/" includes="**/*" />  
 </copy>  
 <copy todir="${basedir}/target/vstatic/blog">
   <fileset dir="${basedir}/target/webapp-SNAPSHOT/WEB-INF/vendorspace/blog/static/" includes="**/*" />
 </copy>

but I have 10's of those folders and it will grow in future so don't want to give hard-coded name

Related question: Maven : copy files without subdirectory structure but it is copying only files, I need to copy the sub-directories inside "static" directory with its content

回答1:

I would suggest to use the maven-war-plugin to handle such things like the following:

<project>
  ...
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <version>2.3</version>
        <configuration>
          <webResources>
            <resource>
              <!-- this is relative to the pom.xml directory -->
              <directory>resource2</directory>
            </resource>
          </webResources>
        </configuration>
      </plugin>
    </plugins>
  </build>
  ...
</project>

It's also possible to turn off filtering etc. via configuration:

  <configuration>
      <!-- the default value is the filter list under build -->
      <!-- specifying a filter will override the filter list under build -->
      <filters>
        <filter>properties/config.prop</filter>
      </filters>
      <nonFilteredFileExtensions>
        <!-- default value contains jpg,jpeg,gif,bmp,png -->
        <nonFilteredFileExtension>pdf</nonFilteredFileExtension>
      </nonFilteredFileExtensions>
      <webResources>
        <resource>
          <directory>resource2</directory>
          <!-- it's not a good idea to filter binary files -->
          <filtering>false</filtering>
        </resource>
        <resource>
          <directory>configurations</directory>
          <!-- enable filtering -->
          <filtering>true</filtering>
          <excludes>
            <exclude>**/properties</exclude>
          </excludes>
        </resource>
      </webResources>
    </configuration>


标签: maven maven-3