Maven:如何将部分目录结构的嵌套资源获取到Maven中的WEB-INF/类

Maven:如何将部分目录结构的嵌套资源获取到Maven中的WEB-INF/类,maven,maven-3,maven-plugin,maven-assembly-plugin,maven-war-plugin,Maven,Maven 3,Maven Plugin,Maven Assembly Plugin,Maven War Plugin,我正在将一个非maven项目迁移到maven项目。我无法更改项目的当前目录结构或创建的war文件的结构。 现在我想在pom.xml中配置资源的源文件夹和目标文件夹的位置。这将允许我通过调用“mvn干净安装”来自动化构建,并且将创建与现有结构的战争 下面是项目目录结构 Project Root>\services\**\**\src\**\**\**\abc.xml Project Root>\services\**\src\abc.xml Project Root>\ser

我正在将一个非maven项目迁移到maven项目。我无法更改项目的当前目录结构或创建的war文件的结构。 现在我想在pom.xml中配置资源的源文件夹和目标文件夹的位置。这将允许我通过调用“mvn干净安装”来自动化构建,并且将创建与现有结构的战争

下面是项目目录结构

Project Root>\services\**\**\src\**\**\**\abc.xml

Project Root>\services\**\src\abc.xml

Project Root>\services\**\**\**\**\**\src\**\**\abc.xml

Project Root>\services\**\**\xyz\abc.xml

Project Root>\services\**\xyz\abc.xml

Project Root>\services\**\**\**\**\**\xyz\abc.xml

Project Root>pom.xml
现在,我希望所有“src”文件夹中的所有xml文件,无论其位置如何,都应该进入我的“target\webapp\WEB-INF\classes”文件夹。文件的目录结构应位于classes文件夹中“src”文件夹(即src的子目录结构)之后

<resources>
    <resource>
        <directory>services/**/src</directory>
    </resource>
</resources>

你最好使用现有的插件。查看maven war插件:

只需将此内容放在pom的构建部分:

<plugin>
   <artifactId>maven-war-plugin</artifactId>
   <version>2.4</version>
</plugin>

最后我发现maven不支持这种开箱即用的方式。
复制资源时剥离父目录结构并仅保留子目录结构。
因此,我们得到了“maven antrun插件”的帮助。
将以下截图发布到pom.xml和ant build.xml,以防其他人需要。
----------------------pom.xml------------------------------------
org.apache.maven.plugins
maven antrun插件
1.7
编译
编译
跑
-------------------ant build.xml-----------------------------

这是将服务中的任何内容添加为web-INF文件夹上方的web资源。但是,我需要的是将xml文件添加到WEB-INF/classes文件夹中。另外请注意,我只需要src folder后面的目录结构。您可以结合使用和maven war插件来实现这一点。但是,您应该做的不是这样做,而是创建一个标准的目录结构。我不相信有一个理智的理由让你想要如此混乱的目录。
Project Root>\services\**\**\src\**\**\**\abc.java

Project Root>\services\**\src\abc.java

Project Root>\services\**\**\**\**\**\src\**\abc.java

Project Root>\services\**\**\xyz\abc.java

Project Root>\services\**\xyz\abc.java

Project Root>\services\**\**\**\**\**\xyz\abc.java
<plugin>
   <artifactId>maven-war-plugin</artifactId>
   <version>2.4</version>
</plugin>
<plugin>
   <artifactId>maven-war-plugin</artifactId>
   <version>2.4</version>
   <configuration>
      <webResources>
          <resource>
            <!-- this is relative to the pom.xml directory -->
            <directory>service**</directory>
           </resource>
       </webResources>
    </configuration>
</plugin>
Finally I found that maven does not support this out of box.
To strip the parent directory structure and keep only the child directory structure when copying resources.

Hence took the help of "maven-antrun-plugin". 

Posting the below snipped to pom.xml and ant build.xml, in case anybody else need it.
    ----------------------pom.xml------------------------------------
    <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-antrun-plugin</artifactId>
                    <version>1.7</version>
                    <executions>
                        <execution>
                            <id>compile</id>
                            <phase>compile</phase>
                            <configuration>
                                <target>
                                <echo message="calling ant file ${basedir}/build.xml" />
                                    <ant antfile="${basedir}/build.xml" target="add-service-resources" />
                                    <echo message="called ant file ${basedir}/build.xml" />
                                </target>
                            </configuration>
                            <goals>
                                <goal>run</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
    -------------------ant build.xml-----------------------------
        <target name="add-service-resources" description="add service resources">
            <copy todir="./target/classes" overwrite="true">
                <fileset dir="./services">
                    <include name="**/src/**"/>
                    <exclude name="**/*.java"/>
                    <exclude name="**/*.class"/>
                    <exclude name="**/servicedef.xml"/>
                </fileset>
                 <cutdirsmapper dirs="2"/><!-- to strip parent directory structure-->
            </copy>
        </target>