Spring項目打包部署( Maven Assembly插件)

一、Maven Assembly插件 打包

爲了在服務器上部署項目,需要將代碼打包,其目錄結構如下:
/bin 執行啓動或停止的腳本
/conf 配置文件所在
/lib 代碼的jar包以及所依賴的jar包
/webapp 與項目的webapp相同

爲了能夠打包,我們需要使用assembly插件,pom.xml配置如下:

     <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <configuration>
            <descriptor><!--配置文件所在 -->
                src/main/assembly/assembly.xml</descriptor>
        </configuration>
        <executions>
            <execution>
                <id>make-assembly</id>
                <phase>package</phase><!-- 綁定到package生命週期階段上 -->
                <goals>
                    <goal>single</goal><!-- 只運行一次 -->      
                </goals>
            </execution>
        </executions>
    </plugin>

下面看一下詳細的配置文件assembly.xml:


<assembly>
    <id>assembly</id>
    <formats>
 <!--format設置包輸出的格式,當前格式設置的爲tar.gz格式,目前還支持zip,tar,tar.gz,tar.bz2,jar,dir,war格式  -->
        <format>tar.gz</format>
    </formats>
    <includeBaseDirectory>true</includeBaseDirectory>
    <fileSets>
    <!--fileSet定義代碼目錄中與輸出目錄的映射-->
        <fileSet>
            <directory>src/main/assembly/bin</directory>
            <outputDirectory>bin</outputDirectory>
            <fileMode>0755</fileMode>
        </fileSet>
        <fileSet>
            <directory>src/main/conf</directory>
            <outputDirectory>conf</outputDirectory>
            <fileMode>0644</fileMode>
        </fileSet>
        <fileSet>
            <directory>src/main/webapp</directory>
            <outputDirectory>webapp</outputDirectory>
            <fileMode>0644</fileMode>
        </fileSet>
    </fileSets>
    <!--dependencySets節點下爲依賴設置-->
    <dependencySets>
        <dependencySet>
            <outputDirectory>lib</outputDirectory>
        </dependencySet>
    </dependencySets>
</assembly>

不管是在命令行還是在IDE中進行相應的Maven install 操作,會把這個項目打出一個tar包,然後 將其部署到服務器就可以了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章