spring cloud 使用maven-assembly-plugin打zip包

本文介绍如何通过Maven配置可执行的Jar包,并详细解释了pom.xml中maven-jar-plugin和maven-assembly-plugin的用法。同时提供了assembly.xml文件的配置示例及必要的脚本。

1、pom文件

<plugin>
					<groupId>org.apache.maven.plugins</groupId>
					<artifactId>maven-jar-plugin</artifactId>
					<version>2.6</version>
					<configuration>
						<archive>
							
							<manifest>
								<mainClass>xxxxx.xx.xx</mainClass>
								
								<addClasspath>true</addClasspath>
								<classpathPrefix>lib/</classpathPrefix>
							</manifest>
							<manifestEntries>
								<Class-Path>./</Class-Path>
							</manifestEntries>
						</archive>
						<excludes>
							<exclude>config/**</exclude>
						</excludes>
					</configuration>
				</plugin>

				<plugin>
					<artifactId>maven-assembly-plugin</artifactId>
					<configuration>
						<!-- not append assembly id in release file name -->
						<appendAssemblyId>false</appendAssemblyId>
						<descriptors>
							<descriptor>src/main/assembly/assembly.xml</descriptor>
						</descriptors>
					</configuration>
					<executions>
						<execution>
							<id>make-assembly</id>
							<phase>package</phase>
							<goals>
								<goal>single</goal>
							</goals>
						</execution>
					</executions>
				</plugin>
			</plugins>

注意:mainClass需要指定需要执行的方法,打包时会自动打成可执行jar

          需要指定assembly.xml路径

2、assembly.xml文件

<?xml version="1.0" encoding="UTF-8"?>  
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd">  
    <id>package</id>  
    <formats>  
        <format>zip</format>  
    </formats>  
    <includeBaseDirectory>true</includeBaseDirectory>  
    <fileSets>  
        <fileSet>  
            <directory>src/main/bin</directory>  
            <outputDirectory>bin</outputDirectory>  
        </fileSet>  
       <fileSet>  
            <directory>src/main/conf</directory>  
            <outputDirectory>conf</outputDirectory>  
        </fileSet>  
        <fileSet>  
            <directory>src/main/logs</directory>  
            <outputDirectory>logs</outputDirectory>  
        </fileSet>  
        <fileSet>  
            <directory>src/main/work</directory>  
            <outputDirectory>work</outputDirectory>  
        </fileSet>  
        <fileSet>  
            <directory>${project.build.directory}</directory>  
            <outputDirectory>/</outputDirectory>  
            <includes>  
                <include>*.jar</include>  
            </includes>  
        </fileSet>  
    </fileSets>  
    <dependencySets>  
        <dependencySet>  
            <outputDirectory>lib</outputDirectory>  
            <scope>runtime</scope>  
<!--             <unpack>false</unpack> -->  
            <excludes>  
<!--                 <exclude>${project.name}-${project.version}</exclude> -->  
                <exclude>${groupId}:${artifactId}</exclude>  
            </excludes>  
        </dependencySet>  
    </dependencySets>  
</assembly>  

3、新建目录工程


4、放入启动、停止、查看脚本

/mnt/jdk1.8.0_151/bin/java -jar -Xms512m -Xmx512m -Dserver.port=8008 -Dfile.encoding=UTF-8  ../lib/xxxxx.jar > ../logs/log.out 2>&1 &
tail -f ./logs/log.out
#!/bin/sh
pid=`ps -ef | grep xxxx.jar | grep -v grep | awk '{print $2}'`
if   [ -z $pid ];then
                echo   "There isn't this process!"
  else
      echo "This process is "$pid
      kill -9 $pid
fi
#!/bin/sh
ps -aux | grep "8008"
5、使用maven打包,命令

mvn clean assembly:assembly






### 对比 - **适用范围**:maven-assembly-plugin 是 Apache Maven 中的通用插件,适用于各种类型的项目,可创建多种格式的归档文件,如 ZIPJAR、TAR.GZ 等;而 spring-boot-maven-plugin 是专门为 Spring Boot 项目提供的插件,主要用于 Spring Boot 项目的构建和打包 [^3]。 - **打包方式和产物结构**:maven-assembly-plugin 可根据自定义描述符文件将项目构建输出、依赖库、模块、站点文档以及其他指定文件进行打包,生成的产物结构取决于描述符文件的配置;spring-boot-maven-plugin 的产物是以一种约定好的方式存放,用于后期直接运行 [^3][^4]。 ### 使用 - **maven-assembly-plugin**:需要配置自定义的描述符文件(通常是 assembly.xml),在打包阶段将项目打包成带有所有依赖的可执行 JAR 文件,并指定主类,执行 mvn package 命令后,将生成一个含所有依赖的 JAR 文件 [^2]。 - **spring-boot-maven-plugin**:在 Spring Boot 项目的 pom.xml 文件中配置该插件,一般默认配置即可满足基本需求,通过执行 mvn spring-boot:repackage 命令可将项目打包可执行JAR 或 WAR 文件。 ### 功能 - **maven-assembly-plugin**:允许根据自定义描述符文件创建各种格式的归档文件,可轻松将项目构建输出、依赖库、模块、站点文档以及其他指定的文件进行打包,适合用于打包应用程序以便于发布或部署 [^3]。 - **spring-boot-maven-plugin**:主要功能是将 Spring Boot 项目打包可执行JAR 或 WAR 文件,方便项目的部署和运行,同时支持在开发阶段启动 Spring Boot 应用。 ### 代码示例 #### maven-assembly-plugin 配置示例 ```xml <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <version>3.3.0</version> <configuration> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> <archive> <manifest> <mainClass>com.example.MainClass</mainClass> </manifest> </archive> </configuration> <executions> <execution> <id>make-assembly</id> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin> ``` #### spring-boot-maven-plugin 配置示例 ```xml <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>2.5.4</version> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> ```
评论 5
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值