0

I have a Java EE project packaged as an EAR using Maven, it contains two WAR modules (one web one mobile) in following structure:

EAR
    -Entities.jar
    -EJB.jar
    -Test.jar
    -Web.war
    -Mobile.war

I have a directory within the Web.war/WEB-INF which contains some .xhtml files that are common to both .wars

Is there any way to instruct Maven to copy this directory into the other .war automatically during build?

eg. I would like..

WebApp-Web/src/main/webapp/WEB-INF/emails/*

to build into

WebApp/WebApp-web/WEB-INF/emails

AND

WebApp/WebApp-mobile/WEB-INF/emails

(I am using the maven-war-plugin btw)

DaveB
  • 2,953
  • 7
  • 38
  • 60
  • You could use the overlay feature from the maven-war-plugin. See http://maven.apache.org/plugins/maven-war-plugin/overlays.html – Maxime Jul 08 '16 at 13:55

2 Answers2

2

If you add your shared resources into the "parent project" (or elsewhere), you could define the relative path in maven-war-plugin's webResources tag (for both web projects):

<plugin>
    <artifactId>maven-war-plugin</artifactId>
    <version>2.6</version>
    <configuration>
        <failOnMissingWebXml>false</failOnMissingWebXml>
        <webResources>
            <resource>
                <directory>../src/main/resources</directory>
                <targetPath>WEB-INF</targetPath>
            </resource>
        </webResources>
    </configuration>
</plugin>

Another way would be to use the maven-resources-plugin to copy the files from a relative path at build-time, but I believe the maven-war-plugin approach is better.

Using maven-resources-plugin: (source how-to-get-a-war-package-with-resources-copied-in-web-inf) I only changed the directory, to make it relative to its parent (ie: ../src/main/resource)

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-resources-plugin</artifactId>
    <version>2.4.2</version>
    <executions>
      <execution>
        <id>default-copy-resources</id>
        <phase>process-resources</phase>
        <goals>
          <goal>copy-resources</goal>
        </goals>
        <configuration>
          <overwrite>true</overwrite>
          <outputDirectory>${project.build.directory}/${project.artifactId}-${project.version}/WEB-INF/</outputDirectory>
          <resources>
            <resource>
              <directory>../src/main/resources</directory>
            </resource>
          </resources>
        </configuration>
      </execution>
    </executions>
  </plugin>
Community
  • 1
  • 1
alexbt
  • 16,415
  • 6
  • 78
  • 87
1

If you're using a web container that implements Servlet 3.0 or newer then you can make use of the web-fragment feature to build these common files into a jar file that is added to the WEB-INF/lib directory of each web application.

Make web resources such as your xhtml files available by placing them in the META-INF/resources directory of the jar file. Any web.xml configuration that might be associated with these xhtml files can be placed in a META-INF/web-fragment.xml file.

Steve C
  • 18,876
  • 5
  • 34
  • 37