使用rpm-maven-plugin的多模块Web应用程序示例?

时间:2011-10-25 01:57:52

标签: java maven maven-2 rpm rpm-maven-plugin

有没有人知道使用Maven构建到RPM的任何Web应用程序? RPM Maven Plugin提供了构建RPM的功能,但缺少文档。

具体来说,我正在寻找一个包含多个模块的示例,即{Maver by Example“系列中的Chapter 8. A Multi-module Project

只有一个模块的例子是:

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

感谢您的帮助!

3 个答案:

答案 0 :(得分:7)

所以,事实证明我在问我的问题时提出的例子是非常错误的,这可能导致我为什么没有得到我正在寻找的完整答案。我真正想要的是安装到服务器的RPM,它依赖于servlet容器(即Tomcat),并将包含的Web应用程序(webapps)安装到Tomcat的webapps目录。

因此,这是正确答案:

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

需要注意的是,我正在寻找一个“多模块”项目,但实际上我的意思是将多个相关的Web应用程序打包到一个RPM中。因此,此Maven构建的正确配置告诉RPM安装程序需要Apache Tomcat并将Web应用程序安装到Tomcat中的正确文件夹中。

答案 1 :(得分:3)

我建议将rpm作为一个单独的项目(即使是多模块的一部分),并让它声明对战争或其他项目提供的其他工件的依赖。

答案 2 :(得分:0)

据我所知 Red Hat 正努力在Fedora(RPM)中提供他们的Java项目(POM)。好的例子可能是 JBoss Application Server 7 ,现在正在为Fedora打包。它是一大堆POM文件,使用各种技术和RPM宏从POM转换为RPM。这些RPM在某种程度上与Maven兼容。我不知道很多细节,但可以随意询问列表/ irc频道。

但我的信息是 - 通常不可能使用“翻译器”来读取一组pom文件并生成一组rpm文件。有障碍,你必须手工逐一包装所有东西。

如果您的目标不是干净利落的方式,您可以在一个大的RPM中分发您的应用程序。它很容易创建二进制rpm。但这不是linux发行版中开源的方式。