文章目录
- 一、需求概述
 - 二、代码结构
 - 三、运行结果
 - 四、打包设置
 - 1. 一体化可执行包
 - 2. 带外部依赖lib的可执行包
 
- 五、打包运行
 - 1. 源码放送
 - 2. 打包执行
 - 3. 打包结果
 
一、需求概述
普通Java工程 docker-show 实现了定时打印docker应用信息,现在需要将其打包成可执行Jar部署到服务器端运行。
打包方式分为2种:
- 一体化可执行包
 - 带外部依赖lib的可执行包
 
二、代码结构

三、运行结果
此项目使用了线程池定时打印docker应用名,端口信息
 
 
四、打包设置
1. 一体化可执行包
pom文件中引入 maven-assembly-plugin插件,核心配置
			<!-- 方式一:带dependencies运行包 -->
			<plugin>
				<artifactId>maven-assembly-plugin</artifactId>
				<version>3.5.0</version>
				<configuration>
					<appendAssemblyId>false</appendAssemblyId>
					<archive>
						<manifest>
							<mainClass>com.fly.simple.MainRun</mainClass>
						</manifest>
					</archive>
					<descriptorRefs>
						<!--将所有外部依赖JAR都加入生成的JAR包-->
						<descriptorRef>jar-with-dependencies</descriptorRef>
					</descriptorRefs>
				</configuration>
				<executions>
					<execution><!-- 配置执行器 -->
						<id>make-assembly</id>
						<phase>package</phase><!-- 绑定到package阶段 -->
						<goals>
							<goal>single</goal><!-- 只运行一次 -->
						</goals>
					</execution>
				</executions>
			</plugin>
 
2. 带外部依赖lib的可执行包
pom文件中引入 maven-dependency-plugin、maven-jar-plugin插件,核心配置
			<!-- 方式二:外部依赖lib目录运行包 -->
			<!-- 将项目依赖包复制到<outputDirectory>指定的目录下 -->
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-dependency-plugin</artifactId>
				<version>3.1.2</version>
				<executions>
					<execution>
						<id>copy-dependencies</id>
						<phase>package</phase>
						<goals>
							<goal>copy-dependencies</goal>
						</goals>
						<configuration>
							<outputDirectory>${project.build.directory}/lib</outputDirectory>
							<excludeArtifactIds>lombok</excludeArtifactIds>
							<includeScope>runtime</includeScope>
							<!-- 默认为test,包含所有依赖 -->
						</configuration>
					</execution>
				</executions>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-jar-plugin</artifactId>
				<version>3.2.0</version>
				<configuration>
					<archive>
						<manifest>
							<addClasspath>true</addClasspath>
							<classpathPrefix>lib</classpathPrefix>
							<mainClass>com.fly.simple.MainRun</mainClass>
						</manifest>
						<manifestEntries>
							<Class-Path>./</Class-Path>
						</manifestEntries>
					</archive>
				</configuration>
			</plugin>
 
五、打包运行
1. 源码放送
https://gitcode.com/00fly/demo
git clone https://gitcode.com/00fly/demo.git
 
2. 打包执行
#完整打包
mvn clean package
#一体化可执行包
mvn clean package -f pom-deps.xml
#带外部依赖lib的可执行包
mvn clean package -f pom-lib.xml
 
3. 打包结果

有任何问题和建议,都可以向我提问讨论,大家一起进步,谢谢!
-over-



















