Domanda

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
È stato utile?

Soluzione 2

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>

Altri suggerimenti

You are better off using an existing plugin for that. Check out the maven-war plugin:

https://maven.apache.org/plugins/maven-war-plugin/

Just put this thing in the build section of your pom:

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

It will take all files that are in your src dir and place them in your WEB-INF/classes. If you want to add class files outside of the WEB-INF/classes (which you shouldn't, cause you should stick to a standard maven directory structure), you can also do that:

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

And below is more about filtering results, although you wouldn't need it:

https://maven.apache.org/plugins/maven-war-plugin/

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top