Example of a multi-module web application with the rpm-maven-plugin?

16,633

Solution 1

So, it turns out the example that I gave in asking my question was quite wrong, which probably contributed to why I didn't get the complete answer I was looking for. What I was really looking for was an RPM to install to a server, that depended on a servlet container (i.e. Tomcat) and would install the included Web Applications (webapps) to Tomcat's webapps directory.

As such, here is the proper answer:

<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 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.mycompany.app</groupId>
    <artifactId>rpm-with-webapp</artifactId>

    <packaging>pom</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>${project.artifactId}</name>
    <url>http://maven.apache.org</url>

    <parent>
        <groupId>com.mycompany.app</groupId>
        <artifactId>application-master-pom</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath>../pom.xml</relativePath>
    </parent>

    <properties>
        <rpm.install.basedir>/srv/apache-tomcat-6.0.33</rpm.install.basedir>
        <rpm.install.webapps>${rpm.install.basedir}/webapps</rpm.install.webapps>
        <rpm.install.config>${rpm.install.basedir}/lib</rpm.install.config>
    </properties>

    <build>
        <extensions>
            <extension>
                <groupId>org.apache.maven.wagon</groupId>
                <artifactId>wagon-webdav</artifactId>
                <version>1.0-beta-2</version>
            </extension>
        </extensions>
    </build>

    <profiles>
        <profile>
            <id>build-rpm</id>
            <activation>
                <property>
                    <name>build-rpm</name>
                </property>
            </activation>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.codehaus.mojo</groupId>
                        <artifactId>rpm-maven-plugin</artifactId>
                        <version>2.1-alpha-1</version>
                        <extensions>true</extensions>
                        <executions>
                            <execution>
                                <goals>
                                    <goal>attached-rpm</goal>
                                </goals>
                                <phase>verify</phase>
                            </execution>
                        </executions>
                        <configuration>
                            <classifier>${rpm.classifier}</classifier>
                            <copyright>My Company</copyright>
                            <distribution>My Distribution</distribution>
                            <group>Applications/Internet</group>
                            <packager>${user.name}</packager>
                            <changelogFile>CHANGELOG</changelogFile>
                            <defaultDirmode>500</defaultDirmode>
                            <defaultFilemode>400</defaultFilemode>
                            <defaultUsername>tomcatuser</defaultUsername>
                            <defaultGroupname>tomcatuser</defaultGroupname>
                            <requires>
                                <require>apache-tomcat &gt;= 6.0.20-2</require>
                            </requires>
                            <mappings>
                                <!-- web app 1 (module #1) -->
                                <mapping>
                                    <directory>${rpm.install.webapps}/myWebApp1</directory>
                                    <sources>
                                        <source>
                                            <location>../path-to/myWebApp1/target/myWebApp1</location>
                                        </source>
                                    </sources>
                                </mapping>
                                <!-- web app 2 (module #2) -->
                                <mapping>
                                    <directory>${rpm.install.webapps}/myWebApp2</directory>
                                    <sources>
                                        <source>
                                            <location>../path-to/myWebApp2/target/unified-browser-widget</location>
                                        </source>
                                    </sources>
                                </mapping>

                                <!--  web app 3 (module #3) -->
                                <mapping>
                                    <directory>${rpm.install.webapps}/myWebApp3</directory>
                                    <sources>
                                        <source>
                                            <location>../path-to/myWebApp3/target/report-services</location>
                                        </source>
                                    </sources>
                                </mapping>
                            </mappings>
                                <postinstallScriptlet>
                                    <script>echo "WARNING: You may need to restart tomcat to ensure changes take effect."</script>
                                </postinstallScriptlet>
                            </configuration>
                        </plugin>
                    </plugins>
                </build>
            </profile>
        </profiles>
    </project>

The thing to note about this is that I was looking for a "multi-module" project, but really what I meant was packaging multiple related Web Applications into a single RPM. So, the proper configuration of this Maven build tells the RPM installer that Apache Tomcat is required and installs the webapps to the proper folder within Tomcat.

Solution 2

I would suggest making the rpm a separate project (even if part of a multi-module) and have it declare dependencies on the war(s) or additional artifacts provided by other projects.

Share:
16,633
blong
Author by

blong

Updated on July 18, 2022

Comments

  • blong
    blong almost 2 years

    Does anyone know of any web applications that are built with Maven to an RPM? The RPM Maven Plugin provides the functionality to build to an RPM, but it's documentation is lacking.

    Specifically, I'm looking for an example that would include multiple modules, i.e. Chapter 8. A Multi-module Project, from the "Maven by Example" series.

    An example with only a single module would be:

    <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 http://maven.apache.org/maven-v4_0_0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>com.mycompany.app</groupId>
      <artifactId>my-webapp</artifactId>
      <packaging>war</packaging>
      <version>1.0-SOME-SNAPSHOT</version>
      <name>my-webapp</name>
      <url>http://maven.apache.org</url>
    
      <properties>
        <rpm.install.basedir>/opt/tomcat6</rpm.install.basedir>
        <rpm.install.webapps>${rpm.install.basedir}/webapps</rpm.install.webapps>
        <rpm.install.config>${rpm.install.basedir}/lib</rpm.install.config>
      </properties>
    
      <dependencies>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>3.8.1</version>
          <scope>test</scope>
        </dependency>
    
        <!-- Servlet -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.1</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp.jstl</groupId>
            <artifactId>jstl-api</artifactId>
            <version>1.2</version>
        </dependency>       
        <dependency>
            <groupId>org.glassfish.web</groupId>
            <artifactId>jstl-impl</artifactId>
            <version>1.2</version>
        </dependency>
    
        <!-- Jackson JSON Processor -->
        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-mapper-asl</artifactId>
            <version>1.8.1</version>
        </dependency>
      </dependencies>
    
      <build>
        <plugins>
          <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>rpm-maven-plugin</artifactId>
            <version>2.1-alpha-1</version>
            <extensions>true</extensions>
            <executions>
              <execution>
                <goals>
                  <goal>attached-rpm</goal>
                </goals>
              </execution>
            </executions>
            <configuration>
              <copyright>My Company</copyright>
              <distribution>My Distribution</distribution>
              <group>Applications/Internet</group>
              <packager>${user.name}</packager>
              <changelogFile>CHANGELOG</changelogFile>
              <defaultDirmode>500</defaultDirmode>
              <defaultFilemode>400</defaultFilemode>
              <defaultUsername>tomcat6</defaultUsername>
              <defaultGroupname>tomcat6</defaultGroupname>
              <requires>
                <require>apache-tomcat &gt;= 6.0.20-2</require>
              </requires>
              <mappings>
    
                <!-- webapps deployment -->
                <mapping>
                  <directory>${rpm.install.webapps}/${project.artifactId}</directory>
                  <sources>
                    <source>
                      <location>target/${project.artifactId}-${project.version}</location>
                    </source>
                  </sources>
                </mapping>
    
                <!-- configuration files -->
                <mapping>
                  <directory>${rpm.install.config}</directory>
                  <configuration>true</configuration>
                  <sources>
                    <source>
                      <location>src/main/resources/my-webapp.jdbc.properties.sample</location>
                    </source>
                    <source>
                      <location>src/main/resources/my-webapp.runtime.properties</location>
                      <destination>my-webapp.runtime.properties.sample</destination>
                    </source>
                  </sources>
                </mapping>
    
                <!-- (Optional) Create other necessary directory structure -->
                <mapping>
                  <directory>${rpm.install.basedir}/my-webapp-workspace</directory>
                  <filemode>750</filemode>
                  <username>tomcatuser</username>
                  <groupname>tomcatuser</groupname>
                </mapping>
    
              </mappings>
    
              <!-- (Optional) -->
              <preinstallScriptlet>
                <scriptFile>src/main/scripts/rpm/pre-install.sh</scriptFile>
              </preinstallScriptlet>
              <!-- (Optional) -->
              <postinstallScriptlet>
                <script>echo "WARNING: Restart tomcat to ensure changes take effect."</script>
              </postinstallScriptlet>
            </configuration>
          </plugin>
        </plugins>
      </build>
    
    </project>
    

    Thanks for any/all help!