Skip to main content
 首页 » 编程设计

maven-2之Maven : copy files without subdirectory structure

2024年11月24日19emanlee

我正在尝试使用 Maven 将给定文件夹中包含的所有 *.xsd 文件移动到另一个文件夹,但没有源子目录结构。

这是我到目前为止:

<plugin> 
    <groupId>org.apache.maven.plugins</groupId> 
    <artifactId>maven-resources-plugin</artifactId> 
    <version>2.3</version> 
    <executions> 
        <execution> 
            <id>move-schemas</id> 
            <phase>generate-sources</phase> 
            <goals> 
                <goal>resources</goal> 
            </goals> 
            <configuration> 
                <outputDirectory>${basedir}/schemas-target</outputDirectory> 
            </configuration> 
        </execution> 
    </executions> 
</plugin> 
 
... 
 
<resources> 
    <resource> 
        <directory>${basedir}/schemas-source</directory> 
        <includes> 
            <include>**/*.xsd</include> 
        </includes> 
    </resource> 
</resources> 

它(几乎)正在工作。唯一的问题是它保留了源子目录结构,而我需要删除该层次结构并将所有 xsd 文件放在目标文件夹中。例子:

这是我在 schemas-source 文件夹中的内容:
schemas-source 
 │- current 
 │    │- 0.3 
 │        │- myfile.xsd 
 │- old 
      │- 0.2 
          │- myfile-0.2.xsd 

这就是我在 schemas-target 文件夹中需要的:
schemas-target 
 │- myfile.xsd 
 │- myfile-0.2.xsd 

请您参考如下方法:

我自己一次又一次地用头撞到那个限制。

基本上:我不认为只有 maven 解决方案。你将不得不求助于使用动态的东西

  • Maven Antrun Plugin
    在 maven 中嵌入 Ant 任务,在本例中为 Ant copy task ,像这样:
    <copy todir="${project.basedir}/schemas-target" flatten="true"> 
        <fileset dir="${project.basedir}/schemas-source"> 
            <include name="**/*.xsd"/> 
        </fileset> 
    </copy> 
    
  • GMaven plugin
    让您从 pom 执行 Groovy 代码,如下所示:
    new File(pom.basedir, 'schemas-source').eachFileRecurse(FileType.FILES){ 
        if(it.name.endsWith('.xsd')){ 
            new File(pom.basedir, 'schemas-target/${it.name}').text = it.text; 
        } 
    }