Search code examples
mavenmaven-3maven-pluginmaven-assembly-pluginmaven-war-plugin

Maven: How to get nested resource with partial directory structure to WEB-INF/classes in maven


I am migrating a non-maven project to maven project. I cant change the current directory structure of the project or the structure of the created war file. Now I want to configure the location of the source folder and target folder of resource in pom.xml. This will allow me to automate the build by just call the "mvn clean install", and the war with the existing structure will get created.

Below is the project directory structure.

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

Now I want that all the xml files in all “src” folder irrespective of their location should come into the my "target\webapp\WEB-INF\classes" folder. Files should come with the directory structure which is after “src” folder(i.e. Child directory structure of src) in classes folder.

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

I shall highly appreciate the help on same.


Just in case the below information is needed:

I am getting this behaviour by default with the java source files. I set the below line

<sourceDirectory>services</sourceDirectory>

All java files which are in below format are getting compiled and “class” files are getting placed in webapp/WEB-INF/classes folder with the directory structure after “src” folder.

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

Solution

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