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>

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值