7、assembly打包命令
学习目标:
1、使用assembly打包项目
学习过程:
使用maven打包时,maven-jar-plugin插件会在target目录下生成可执行的xxx-0.0.1-SNAPSHOT.jar文件,但是一般生产程序部署时需要打包自定义的格式包,这种情况就可以使用maven-assembly-plugin插件。
一、新建相关的目录和文件。
其中bin下面的运行命令文件也是需要我们直接编写的,具体大家可以下载源码看一下。
二、打包配置
<!-- - 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>
运行打包命令后回生产一个压缩包
里面是我们定义的标准格式,以后我们可以直接使用bin下面的命令就可以运行了。压缩包是我们的标准目录结构
完整的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>