1. 程式人生 > >7、assembly打包命令

7、assembly打包命令

學習目標:

1、使用assembly打包專案

學習過程:

   使用maven打包時,maven-jar-plugin外掛會在target目錄下生成可執行的xxx-0.0.1-SNAPSHOT.jar檔案,但是一般生產程式部署時需要打包自定義的格式包,這種情況就可以使用maven-assembly-plugin外掛。

一、新建相關的目錄和檔案。

其中bin下面的執行命令檔案也是需要我們直接編寫的,具體大家可以下載原始碼看一下。

attcontent/d1d4a078-e896-4be6-94b2-43fddf83ae6c.png

二、打包配置

<!-- - Copyright 1999-2011 Alibaba Group. - - Licensed under the Apache License, 
    Version 2.0 (the "License"); - you may not use this file except in compliance 
    with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 
    - - Unless required by applicable law or agreed to in writing, software - 
    distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT 
    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the 
    License for the specific language governing permissions and - limitations 
    under the License. -->

<assembly>
    <id>assembly</id>
    <formats>
        <format>tar.gz</format>
    </formats>

    <includeBaseDirectory>true</includeBaseDirectory>
    <fileSets>
        <fileSet>
            <directory>${project.basedir}/src/main/assembly/bin</directory>
            <outputDirectory>bin</outputDirectory>
            <fileMode>0755</fileMode>
            <filtered>true</filtered> 
        </fileSet>

        <fileSet>
            <directory>${project.basedir}/src/main/resources</directory>
            <!-- <includes> <include>*.properties</include> </includes> -->
            <outputDirectory>conf</outputDirectory>
            <fileMode>0644</fileMode>
            <filtered>true</filtered> 
        </fileSet>
    </fileSets>


    <files>
        <file>
            <source>${project.build.directory}/${project.build.finalName}.${project.packaging} </source>
            <outputDirectory>bin/</outputDirectory>
        </file>
    </files>


    <dependencySets>
        <dependencySet>
            <useProjectArtifact>true</useProjectArtifact>
            <outputDirectory>lib</outputDirectory>
            <scope>runtime</scope>
        </dependencySet>
    </dependencySets>

</assembly>

三、修改pom.xml

  我們可以繫結package命令。也可以直接使用assembly:assembly時才使用打包命令

<plugin>
       <artifactId>maven-assembly-plugin</artifactId>
       <configuration>
            <descriptor>src/main/assembly/assembly.xml</descriptor>
        </configuration>

          <!-- 使用assembly:assembly -->
           <!-- <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions> -->
            </plugin>

執行打包命令後回生產一個壓縮包

attcontent/4d3039aa-9d2c-4450-a67c-f53cc522f443.png

裡面是我們定義的標準格式,以後我們可以直接使用bin下面的命令就可以運行了。壓縮包是我們的標準目錄結構

attcontent/a5011a11-0d28-4ffc-9f6a-57d1a501551b.png

完整的pom.xml如下:

<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>
    <groupId>com.liubao.dao</groupId>
    <artifactId>basedao</artifactId>
    <version>1.0</version>
    <packaging>jar</packaging>

    <name>basedao</name>
    <url>http://maven.apache.org</url>
    <profiles>
        <!-- 開發/測試環境,預設啟用 -->
        <profile>
            <id>dev</id>
            <properties>
                <env>dev</env>
                <deploy.environment>local</deploy.environment>
            </properties>

            <activation>
                <activeByDefault>true</activeByDefault><!--預設啟用的是dev環境配置 -->
            </activation>
        </profile>

        <!-- 生產環境 -->
        <profile>
            <id>prod</id>
            <properties>
                <env>prod</env>
            </properties>
        </profile>
    </profiles>


    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>

            <!-- 打包jar檔案時,配置manifest檔案,加入lib包的jar依賴 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <classesDirectory>target/classes/</classesDirectory>
                    <archive>
                        <manifest>
                            <mainClass>com.liubao.dao.basedao.BaseDao</mainClass>
                            <!-- 打包時 MANIFEST.MF檔案不記錄的時間戳版本 -->
                            <useUniqueVersions>false</useUniqueVersions>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>../lib/</classpathPrefix>
                        </manifest>
                        <manifestEntries>
                            <Class-Path>.</Class-Path>
                        </manifestEntries>
                    </archive>
                </configuration>
            </plugin>

            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <descriptor>src/main/assembly/assembly.xml</descriptor>
                </configuration>
                <!-- 使用assembly:assembly -->
                <!-- <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions> -->
            </plugin>
        </plugins>


        <filters>
            <!-- 指定使用的 filter 執行時候記得clean -P 執行環境 , 比如: mvn clean install -Dmaven.test.skip=true 
                -P dev 或 -P prod 平時打包命令 : clean package -P prod src/main/filters -->
            <filter>src/main/filters/filter-${env}.properties</filter>
        </filters>

        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering> <!-- 是否使用過濾器 -->
            </resource>

            <resource>
                <directory>src/main/assembly</directory>
                <filtering>true</filtering> <!-- 是否使用過濾器 -->
            </resource>
        </resources>
    </build>
</project>