maven構建openfire插件

記錄一次使用maven插件構建openfire插件的所有過程html

由於項目須要, 故把openfire源碼下載下來修改發佈, 期間要使用本身編寫的openfire插件, 最新版的openfire使用maven構建, 故插件的項目構建方式也使用了maven, openfire的插件目錄結構很是特殊,
點我瞭解更多, 須要個性化
的maven配置才能生成可用的插件jar
java

項目結構

project/
  |- pom.xml
  |- plugin.xml      
  |- readme.html     
  |- changelog.html  
  |- logo_small.gif  
  |- logo_large.gif  
  |- src
    |- main
      |- java(source dir)
      |- web

構建過程

首先把src-main下的java設置成source root文件夾, 使下面的類能夠引用jdk環境, 編寫好代碼以後, 直接編寫pom文件, 用maven插件去定義生成好的jar包內部結構web

爬坑1: 與src平級的幾個文件不能打進jar包, 由於頂級文件夾並非resources文件夾

解決1: 使用maven-resources插件, 指定resource位置, 與在jar裏的位置, 
也能夠直接在build標籤下使用resources標籤聲明, 這是maven的約定寫法, 不過我仍是聲明瞭插件

爬坑2: 仔細看openfire插件結構, 能夠看到, 插件jar裏面有個lib文件夾, lib下存放的jar纔是真正java代碼打包好的文件, 
我一開始想着使用resources插件, 把打包好的jar重複放進jar裏(至關於向本身裏放了個本身), 後來證實是矛盾的, 並且是很差使的

解決2: 使用ant插件, 定義最終的jar結構, 而且配置maven-jar, 只編譯java類, 其他組織的工做交給ant插件作

pom文件(附說明)

<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <!-- 父工程爲openfire插件工程 -->
    <parent>
        <artifactId>plugins</artifactId>
        <groupId>org.igniterealtime.openfire</groupId>
        <version>4.4.2</version>
    </parent>
    <groupId>com.gomyck</groupId>
    <artifactId>openfire-plugin-gomyck-sendmessage</artifactId>
    <name>Openfire Plugin Gomyck Sendmessage</name>
    <description>
        消息插件
    </description>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <build>
        <!-- 初次打包的jar名稱 -->
        <finalName>gomyck-sendMessage</finalName>
        <plugins>
            <!-- maven編譯插件, 聲明週期爲compiler, 主要負責編譯java類 -->
            <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>
                    <!-- 編碼方式 -->
                    <encoding>${project.build.sourceEncoding}</encoding>
                </configuration>
            </plugin>
            <!-- maven 打jar包插件, 這個插件遇到的坑最多, 其遵循就近原則, 若是在configuration中寫了配置, 
            那麼其默認的配置都會很差使, 好比include, 若是用了, 那麼只會編譯include的類 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.1.0</version>
                <configuration>
                    <!-- 只在jar裏放class文件, 若是不寫這個, 那麼resources插件生成的資源文件也會打進jar裏, 若是寫了這個,
                     resources插件只會在target-class文件夾下生成文件 -->
                    <includes>
                        <include>**/com/gomyck/**</include>
                    </includes>
                    <outputDirectory>${project.build.directory}</outputDirectory>
                </configuration>
            </plugin>
            
            <!-- maven 資源插件, 這個插件主要把一些外部文件變成打包時的資源文件 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>3.1.0</version>
                <executions>
                    <execution>
                        <phase>install</phase>
                        <!-- resources 和 copy-resources, 若是是resources, 
                        則直接會在jar包裏體現, 若是是copy-resources, 那還必須指定要copy的目標文件夾位置 -->
                        <goals>
                            <goal>resources</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <resources>
                        <resource>
                            <!-- 資源文件所在文件夾 -->
                            <directory>${project.basedir}</directory>
                            <!-- 是否使用文件過濾器 -->
                            <filtering>true</filtering>
                            <!-- 若是使用過濾器, 那麼包含哪些文件 -->
                            <includes>
                                <include>changelog.html</include>
                                <include>logo_large.gif</include>
                                <include>logo_small.gif</include>
                                <include>plugin.xml</include>
                                <include>readme.html</include>
                            </includes>
                        </resource>

                        <resource>
                            <directory>${project.basedir}/src/main/web</directory>
                            <!-- jar包目標位置, 下面的意思是會在jar的根目錄下創建web文件夾, 
                            而且把src/main/web下的全部文件都copy進去 -->
                            <targetPath>web</targetPath>
                        </resource>

                    </resources>
                </configuration>
            </plugin>

            <plugin>
                <!-- ant插件, 這個最重要了, 以上三個插件只是可以零散的把文件編譯並放在target文件夾下, 
                我使用ant插件, 把文件整理起來, 並壓縮到指定的jar裏面 -->
                <artifactId>maven-antrun-plugin</artifactId>
                <version>1.3</version>
                <executions>
                    <execution>
                        <!-- 調用階段 -->
                        <phase>install</phase>
                        <goals>
                            <!-- 目標(命令) -->
                            <goal>run</goal>
                        </goals>
                        <configuration>
                            <tasks>
                                <echo message="開始構建插件包..."></echo>
                                <copy todir="${project.build.directory}/${project.build.finalName}-final/lib" overwrite="true" file="${project.build.directory}/${project.build.finalName}.jar"></copy>
                                <copydir dest="${project.build.directory}/${project.build.finalName}-final/" src="${project.build.directory}/classes/" excludes="org/**"></copydir>
                                <jar basedir="${project.build.directory}/${project.build.finalName}-final/" destfile="${project.build.directory}/${project.build.finalName}-final.jar"></jar>
                                <delete dir="${project.build.directory}/${project.build.finalName}-final/"></delete>
                            </tasks>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

        </plugins>
    </build>
</project>

ant文檔地址(全部的ant構建命令可在此查看)

點擊前往apache

我的博客 點擊前往