Spring boot 项目用maven build 打jar包

开发工具:eclipse

项目中需要把部分功能单独剥离出来,单独作为微服务部署,因此需要新建一个spring boot 项目,单独构建jar包,需要保证jar能成功启动运行。经过一整天的试错之后,终于成功了!!!

需要先保证电脑环境中已经配置好了maven的环境变量,不清楚的可以百度。

先通过maven build 打jar;这过程可能会遇到很多问题,需要一个个百度(问题基本都是pom文件配置不对导致,我修改了一天的配置,一个个解决后才成功构建了jar包),下面记录一下pom.xml配置。

构建方式如下:

下面是我最终的pom文件配置内容

<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.5.10</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.hyit.jgdxzt</groupId>
	<artifactId>jgdxzt-analysis</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>jgdxzt-analysis</name>
	<description>Analysis project for Spring Boot</description>
	<properties>
		<java.version>1.8</java.version>
	</properties>
	
	<dependencies>
	<!--  手动引用的依赖 -->
		<dependency>
            <groupId>com.**</groupId>
            <artifactId>**-zjanalysis</artifactId>
            <version>simple-1.4.7</version> 
    <!--  设置依赖包所在位置为lib文件夹 -->
            <scope>system</scope>
            <systemPath>${basedir}/lib/**-zjanalysis-simple-1.4.7.jar</systemPath>
        </dependency>
        
        <dependency>
            <groupId>com.**</groupId>
            <artifactId>**-kernel.base</artifactId>
            <version>1.1.3</version> 
            <scope>system</scope>
            <systemPath>${basedir}/lib/**-kernel.base-1.1.3.jar</systemPath>
        </dependency>
        
        <dependency>
            <groupId>com.**</groupId>
            <artifactId>**-core-datamodel</artifactId>
            <version>3.1.0</version> 
            <scope>system</scope>
            <systemPath>${basedir}/lib/**-core-datamodel-3.1.0.jar</systemPath>
        </dependency>
        
        <dependency>
            <groupId>com.sgcc.cis</groupId>
            <artifactId>sgcis-common</artifactId>
            <version>0.0.2-RELEASE</version>
            <scope>system</scope>
            <systemPath>${basedir}/lib/sgcis-common-0.0.2-RELEASE.jar</systemPath>
        </dependency>
        
        <!-- POI相关包 -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>4.1.1</version> 
        </dependency>
        
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>4.1.1</version> 
        </dependency>
	    <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-scratchpad</artifactId>
            <version>4.1.1</version> 
        </dependency>
	    
	           <!-- json所需要的所有jar包 -->
		<dependency>
		    <groupId>net.sf.json-lib</groupId>
		    <artifactId>json-lib</artifactId>
		    <version>2.4</version>
		    <classifier>jdk15</classifier>
		</dependency>
		<dependency>
		    <groupId>net.sf.ezmorph</groupId>
		    <artifactId>ezmorph</artifactId>
		    <version>1.0.6</version>
		</dependency>
		<dependency>
		    <groupId>commons-logging</groupId>
		    <artifactId>commons-logging</artifactId>
		    <version>1.1.1</version>
		</dependency>
		<dependency>
		    <groupId>commons-lang</groupId>
		    <artifactId>commons-lang</artifactId>
		    <version>2.5</version>
		</dependency>
		<dependency>
		    <groupId>commons-collections</groupId>
		    <artifactId>commons-collections</artifactId>
		    <version>3.2.1</version>
		</dependency>
		<dependency>
		    <groupId>commons-beanutils</groupId>
		    <artifactId>commons-beanutils</artifactId>
		    <version>1.8.3</version>
		</dependency>
	    
	    <!-- Fastjson反序列化相关 -->
	    <dependency>
		     <groupId>com.alibaba</groupId>
		     <artifactId>fastjson</artifactId>
		     <version> 1.2.83</version>
		</dependency>
		<dependency>
		    <groupId>com.fasterxml.jackson.core</groupId>
		    <artifactId>jackson-core</artifactId>
		    <version>2.12.6</version>
		</dependency>
		<dependency>
		    <groupId>com.fasterxml.jackson.core</groupId>
		    <artifactId>jackson-annotations</artifactId>
		    <version>2.12.6</version>
		</dependency>
		<dependency>
		    <groupId>com.fasterxml.jackson.core</groupId>
		    <artifactId>jackson-databind</artifactId>
		    <version>2.12.6</version>
		</dependency>
	    
	    <dependency>
			<groupId>org.mvel</groupId>
			<artifactId>mvel2</artifactId>
			<version>2.4.14.Final</version>
		</dependency>
		
		<!-- SpringBoot核心jar包 -->
	  	<dependency>
	  		<groupId>org.springframework.boot</groupId>
	  		<artifactId>spring-boot-starter</artifactId>
	  	</dependency>
	  	
	  	<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	  	<!-- web开发包:包括了tomca和springmvc -->
	  	<dependency>
	  		<groupId>org.springframework.boot</groupId>
	  		<artifactId>spring-boot-starter-web</artifactId>
	  	</dependency>
	  	
	  	<!-- <dependency>
		   <groupId>javax.servlet</groupId>
		   <artifactId>servlet-api</artifactId>
		   <version>2.5</version>
		</dependency> -->
	</dependencies>

	<build>
	<finalName>bw-analysis</finalName>
<!-- 指定默认的goal方式,缺失可能会报错,也可以直接maven build的时候指定 -->
	<defaultGoal>compile</defaultGoal> 
        <plugins>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <!--这里要替换成jar包main方法所在类 -->
                            <mainClass>com.hyit.jgdxzt.JgdxztAnalysisApplication</mainClass>
                        </manifest>
                        <manifestEntries>
                            <Class-Path>.</Class-Path>
                        </manifestEntries>
                    </archive>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                    <appendAssemblyId>false</appendAssemblyId>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id> <!-- this is used for inheritance merges -->
                        <phase>package</phase> <!-- 指定在打包节点执行jar包合并操作 -->
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <fork>true</fork>
                    <mainClass>com.hyit.jgdxzt.JgdxztAnalysisApplication</mainClass>
                    <includeSystemScope>true</includeSystemScope>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>8</source>
                    <target>8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
	
	
	<repositories>
		<repository>
			<id>spring-milestones</id>
			<name>Spring Milestones</name>
			<url>https://repo.spring.io/milestone</url>
			<snapshots>
				<enabled>false</enabled>
			</snapshots>
		</repository>
		<repository>
			<id>spring-snapshots</id>
			<name>Spring Snapshots</name>
			<url>https://repo.spring.io/snapshot</url>
			<releases>
				<enabled>false</enabled>
			</releases>
		</repository>
	</repositories>
	<pluginRepositories>
		<pluginRepository>
			<id>spring-milestones</id>
			<name>Spring Milestones</name>
			<url>https://repo.spring.io/milestone</url>
			<snapshots>
				<enabled>false</enabled>
			</snapshots>
		</pluginRepository>
		<pluginRepository>
			<id>spring-snapshots</id>
			<name>Spring Snapshots</name>
			<url>https://repo.spring.io/snapshot</url>
			<releases>
				<enabled>false</enabled>
			</releases>
		</pluginRepository>
	</pluginRepositories>

</project>

 我在打包时遇到的问题就是一些手动引入的依赖在打包的时候找不到,所以一直报错。百度了很久后发现是需要将lib文件夹中的所有引入的jar包分别单独的添加以下配置,设置systemPath为本地路径(解决)。

其他相关的jar包可直接通过原有的配置从maven仓库下载,还有几个spring boot 项目相关的依赖,注意不能缺失,不然项目启动直接会报错。

<!-- SpringBoot核心jar包 -->
	  	<dependency>
	  		<groupId>org.springframework.boot</groupId>
	  		<artifactId>spring-boot-starter</artifactId>
	  	</dependency>
	  	
	  	<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	  	<!-- web开发包:包括了tomca和springmvc -->
	  	<dependency>
	  		<groupId>org.springframework.boot</groupId>
	  		<artifactId>spring-boot-starter-web</artifactId>
	  	</dependency>

看到以下提示说明打包已经成功,接下来就需要验证jar能否正常启动运行。

进入到jar包所在文件夹(我这里构建成功后,jar直接发送到了maven仓库,具体地址可以看控制台的日志输出),打开cmd命令窗口,执行下面命令启动,观察是否报错。

后续过程中使用到的相关命令:

-- jar启动命令
java -jar **.jar 

java -jar jgdxzt-analysis-0.0.1-SNAPSHOT.jar


-- maven打包命令
mvn clean package:本地打包,jar或者war存放在本地项目target下
mvn clean install:本地打包,jar或者war存放在本地项目target下,以及本地maven仓库下
mvn clean deploy:本地打包,jar或者war存放在本地项目target下,以及本地maven仓库下,远程nexus仓库

结果jar包正常启动未报错,通过项目代码也能成功调用接口,说明一切这个包没问题,可直接使用,后面我是吧jar拿到linux服务器上启动,没问题后再打成docker镜像部署即可

验证的时候,在linux上启动jar报错,一直提示我启动失败

反复排查后发现是因为,我在配置中指定了ip导致,删除后重新打包就没问题了

编码过程中的问题在前面的文章中已经记录,参考:http://t.csdn.cn/gNvJa

文件配置参考文章:https://blog.csdn.net/qq2523208472/article/details/119357484

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值