Search code examples
javamavenweb-applicationswildflyear

Building two ears from two wars in one webapp


as always, excuse me if I make a silly query or one that is easily resolved. In a project I'm working on I was asked to find out if it's possible to build two wars from one webapp. This I could solve in the following way:

WebProject

<artifactId>FacturaElectronica</artifactId>
<packaging>war</packaging>
<name>factElectronica - web</name>
<description>This is the web POM file</description>
<groupId>factElectronica-web</groupId>
<version>1.0-SNAPSHOT</version>

<build>
        <finalName>${project.artifactId}</finalName>
        <plugins>       
            <plugin>         
                <artifactId>maven-install-plugin</artifactId>         
                <executions>           
                    <execution>             
                        <id>default-install</id>             
                        <phase>never</phase>           
                    </execution>         
                </executions>       
            </plugin>   
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.5.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.2.2</version>
                <executions>
                    <execution>
                      <id>default-war</id>
                      <phase>none</phase>
                      <configuration>
                        <finalName>unwanted</finalName>
                        <classifier>unwanted</classifier>
                      </configuration>
                    </execution>
                    <execution>
                        <id>facturaelectronicafija</id>
                        <phase>install</phase>
                        <goals>
                            <goal>war</goal>
                        </goals>
                        <configuration>
                            <classifier>facturaelectronicafija</classifier>
                            <webResources>
                                <resource>
                                    <filtering>true</filtering>
                                    <directory>src/main/webapp</directory>
                                    <includes>
                                        <include>**</include>
                                    </includes>
                                </resource>
                                <resource>
                                    <directory>src/main/webapp</directory>
                                </resource>
                            </webResources>
                            <failOnMissingWebXml>false</failOnMissingWebXml>
                            <archive>
                                <manifestEntries>
                                    <Dependencies>org.apache.santuario.xmlsec,org.apache.commons.codec,javax.ws.rs.api</Dependencies>
                                </manifestEntries>
                            </archive>
                        </configuration>
                    </execution>
                    <execution>
                        <id>facturaelectronicamovil</id>
                        <phase>install</phase>
                        <goals>
                            <goal>war</goal>
                        </goals>
                        <configuration>
                            <classifier>facturaelectronicamovil</classifier>
                            <webResources>
                                <resource>
                                    <filtering>true</filtering>
                                    <directory>src/main/webapp</directory>
                                    <includes>
                                        <include>**</include>
                                    </includes>
                                </resource>
                                <resource>
                                    <directory>src/main/webappFant</directory>
                                </resource>
                            </webResources>
                            <failOnMissingWebXml>false</failOnMissingWebXml>
                            <archive>
                                <manifestEntries>
                                    <Dependencies>org.apache.santuario.xmlsec,org.apache.commons.codec,javax.ws.rs.api</Dependencies>
                                </manifestEntries>
                            </archive>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
        <resources>
            <resource>
                <directory>src/main/java/resources</directory>
            </resource>
        </resources>
    </build>

Each of these war has the same classes but differs in the xhtml files. One points to mobile phone components and the other points to landline phone components.

After doing this, I realized that it was necessary to build an ear for each one of them, each one with its root context and perform the deploy in wildfly. But I can't find the way to do it. On the one hand I don't know how to point to the wars that are built in the execution in the webapp, and on the other hand I don't know if the way is only with one ear for both or it is necessary to build two. I would be very grateful if someone could give me a hand with this. Thank you very much!

Update

Taking the example I found in this link: Maven ear plugin multiple artifacts content

I made the following changes:

WebProject

<build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <artifactId>maven-war-plugin</artifactId>
                    <executions>
                        <execution>
                            <id>package-facturaelectronicafija</id>
                            <phase>package</phase>
                            <configuration>
                                <classifier>facturaelectronicafija</classifier>
                                <webappDirectory>${project.build.directory}/${project.build.finalName}-facturaelectronicafija</webappDirectory>
                                <webResources>
                                    <resource>
                                        <directory>src/main/webapp</directory>
                                    </resource>
                                </webResources>
                            </configuration>
                            <goals>
                                <goal>war</goal>
                            </goals>
                        </execution>
                        <execution>
                            <id>package-facturaelectronicamovil</id>
                            <phase>package</phase>
                            <configuration>
                                <classifier>facturaelectronicamovil</classifier>
                                <webappDirectory>${project.build.directory}/${project.build.finalName}-facturaelectronicamovil</webappDirectory>
                                <webResources>
                                    <resource>
                                        <directory>src/main/webappFant</directory>
                                    </resource>
                                </webResources>
                            </configuration>
                            <goals>
                                <goal>war</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </pluginManagement>
        <finalName>FacturaElectronica</finalName> 
     </build>
</project>

EarProject

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>factElectronicaWeb-ear</groupId>
  <artifactId>factElectronicaWeb-ear</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>ear</packaging>
   
    <dependencies>
        <dependency>
            <groupId>factElectronica-web</groupId>
            <artifactId>FacturaElectronica</artifactId>
            <version>1.0-SNAPSHOT</version>
            <type>war</type>
        </dependency>
        <dependency>
            <groupId>factElectronica-web</groupId>
            <artifactId>FacturaElectronica</artifactId>
            <version>1.0-SNAPSHOT</version>
            <classifier>facturaelectronicafija</classifier>
            <scope>provided</scope>
            <type>war</type>
        </dependency>
        <dependency>
            <groupId>factElectronica-web</groupId>
            <artifactId>FacturaElectronica</artifactId>
            <classifier>facturaelectronicamovil</classifier>
            <version>1.0-SNAPSHOT</version>
            <scope>provided</scope>
            <type>war</type>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <!-- <groupId>org.apache.maven.plugins</groupId> -->
                <artifactId>maven-ear-plugin</artifactId>
                <version>3.0.2</version>
                <executions>
                    <execution>
                        <id>package-facturaelectronicafija</id>
                        <phase>package</phase>
                        <configuration>
                            <workDirectory>target/facturaelectronicafija</workDirectory>
                            <classifier>facturaelectronicafija</classifier>
                            <version>8</version>
                            <modules>
                                <webModule>
                                    <groupId>factElectronica-web</groupId>
                                    <artifactId>FacturaElectronica</artifactId>
                                    <classifier>facturaelectronicafija</classifier>
                                    <contextRoot>/FacturaElectronica</contextRoot>
                                    <bundleFileName>/FacturaElectronica.war</bundleFileName>
                                </webModule>
                            </modules>
                        </configuration>
                        <goals>
                            <goal>ear</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>package-facturaelectronicamovil</id>
                        <phase>package</phase>
                        <configuration>
                            <workDirectory>target/facturaelectronicamovil</workDirectory>
                            <classifier>facturaelectronicamovil</classifier>
                            <version>8</version>
                            <modules>
                                <webModule>
                                    <groupId>factElectronica-web</groupId>
                                    <artifactId>FacturaElectronica</artifactId>
                                    <classifier>facturaelectronicamovil</classifier>
                                    <contextRoot>/FacturaElectronica</contextRoot>
                                    <bundleFileName>/FacturaElectronica.war</bundleFileName>
                                </webModule>
                            </modules>
                        </configuration>
                        <goals>
                            <goal>ear</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
        <finalName>FacturaElectronica</finalName>
    </build>
</project>

Now the executions are done and maven creates three ears, the first one, which is called "FacturaElectronica" I created it to test that it actually works. The problem is that the three ears have exactly the same content as "FacturaElectronica". I think it is because maven does not find the dependence towards "facturaelectronicafija" and "facturaelectronicafija". Maybe because when I create these WARs in executions I can't add a version to them. Somebody could clarify me this please? Thank you very much!


Solution

  • I had to make some corrections, but I managed to leave it working as expected.

    WebProject

    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
    
    <build>
            <pluginManagement>
                <plugins>
                    <plugin>         
                        <artifactId>maven-install-plugin</artifactId>         
                        <executions>           
                            <execution>             
                                <id>default-install</id>             
                                <phase>never</phase>           
                            </execution>         
                        </executions>       
                    </plugin>   
                    <plugin>
                        <artifactId>maven-war-plugin</artifactId>
                        <executions>
                            <execution>
                                <id>default-war</id>
                                <phase>none</phase>
                                <configuration>
                                    <finalName>unwanted</finalName>
                                    <classifier>unwanted</classifier>
                                </configuration>
                            </execution>
                            <execution>
                                <id>package-facturaelectronicafija</id>
                                <phase>package</phase>
                                <configuration>
                                    <classifier>facturaelectronicafija</classifier>
                                    <webappDirectory>${project.build.directory}/${project.build.finalName}-facturaelectronicafija</webappDirectory>
                                    <webResources>
                                        <resource>
                                            <directory>src/main/webapp</directory>
                                        </resource>
                                    </webResources>
                                </configuration>
                                <goals>
                                    <goal>war</goal>
                                </goals>
                            </execution>
                            <execution>
                                <id>package-facturaelectronicamovil</id>
                                <phase>package</phase>
                                <configuration>
                                    <classifier>facturaelectronicamovil</classifier>
                                    <webappDirectory>${project.build.directory}/${project.build.finalName}-facturaelectronicamovil</webappDirectory>
                                    <webResources>
                                        <resource>
                                            <directory>src/main/webappFant</directory>
                                        </resource>
                                    </webResources>
                                </configuration>
                                <goals>
                                    <goal>war</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </pluginManagement>
            <finalName>FacturaElectronica</finalName>
         </build>
    

    The default install run is there so that the default war base is not created.

    EarProject

    <modelVersion>4.0.0</modelVersion>
      <groupId>factElectronicaWeb-ear</groupId> 
      <artifactId>factElectronicaWeb-ear</artifactId>
      <version>0.0.1-SNAPSHOT</version> 
      <packaging>ear</packaging>
        
        <dependencies>
            
            <dependency>
                <groupId>factElectronica-web</groupId>
                <artifactId>FacturaElectronica</artifactId>
                <version>1.0-SNAPSHOT</version>
                <classifier>facturaelectronicafija</classifier>
                <scope>compile</scope>
                <type>war</type>
            </dependency>
             
            <dependency>
                <groupId>factElectronica-web</groupId>
                <artifactId>FacturaElectronica</artifactId>
                <classifier>facturaelectronicamovil</classifier>
                <version>1.0-SNAPSHOT</version>
                <scope>compile</scope>
                <type>war</type>
            </dependency>
            
        </dependencies>
        
        <build>
            <plugins>
                <plugin>         
                    <artifactId>maven-install-plugin</artifactId>         
                    <executions>           
                        <execution>             
                            <id>default-install</id>             
                            <phase>never</phase>           
                        </execution>         
                    </executions>       
                </plugin>   
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId> 
                    <artifactId>maven-ear-plugin</artifactId>
                    <version>3.0.2</version>
                    <executions>
                        <execution>
                          <id>default-ear</id>
                          <phase>none</phase>
                          <configuration>
                            <finalName>unwanted</finalName>
                            <classifier>unwanted</classifier>
                          </configuration>
                        </execution>
                        
                        <execution>
                            <id>package-facturaelectronicafija</id>
                            <phase>package</phase>
                            <configuration>
                                <workDirectory>target/facturaelectronicafija</workDirectory>
                                <classifier>facturaelectronicafija</classifier>
                                <version>8</version>
                                <modules>
                                    <webModule>
                                        <groupId>factElectronica-web</groupId>
                                        <artifactId>FacturaElectronica</artifactId>
                                        <classifier>facturaelectronicafija</classifier>
                                        <contextRoot>/FacturaElectronica</contextRoot>
                                        <bundleFileName>/FacturaElectronica-facturaelectronicafija.war</bundleFileName>
                                    </webModule>
                                     <webModule>
                                        <groupId>factElectronica-web</groupId>
                                        <artifactId>FacturaElectronica</artifactId>
                                        <classifier>facturaelectronicamovil</classifier>
                                       <excluded>true</excluded>
                                     </webModule>
                                </modules>
                            </configuration>
                            <goals>
                                <goal>ear</goal>
                            </goals>
                        </execution>
                        
                        <execution>
                            <id>package-facturaelectronicamovil</id>
                            <phase>package</phase>
                            <configuration>
                                <workDirectory>target/facturaelectronicamovil</workDirectory>
                                <classifier>facturaelectronicamovil</classifier>
                                <version>8</version>
                                <modules>
                                    <webModule>
                                        <groupId>factElectronica-web</groupId>
                                        <artifactId>FacturaElectronica</artifactId>
                                        <classifier>facturaelectronicamovil</classifier>
                                        <contextRoot>/DetalleFactura</contextRoot>
                                        <bundleFileName>/FacturaElectronica-facturaelectronicamovil.war</bundleFileName>
                                    </webModule>
                                     <webModule>
                                        <groupId>factElectronica-web</groupId>
                                        <artifactId>FacturaElectronica</artifactId>
                                        <classifier>facturaelectronicafija</classifier>
                                       <excluded>true</excluded>
                                     </webModule>
                                </modules>
                            </configuration>
                            <goals>
                                <goal>ear</goal>
                            </goals>
                        </execution>
                        
                    </executions>
                </plugin>
            </plugins>
            <finalName>FacturaElectronica</finalName>
        </build>
    

    Previously the dependencies generated only a meta inf file, this was because they were with a scope provided, when you switch to compile it works correctly.

    <dependency>
                    <groupId>factElectronica-web</groupId>
                    <artifactId>FacturaElectronica</artifactId>
                    <classifier>facturaelectronicamovil</classifier>
                    <version>1.0-SNAPSHOT</version>
                    <scope>compile</scope>
                    <type>war</type>
                </dependency>