spring boot打jar包(maven对jar和lib分离)

时间:2023-03-08 16:45:32

spring boot intellij Ide打包有两种方式:

1、maven:熟悉、方便配置灵活

2、Build artifacts:操作比较复杂,jar和lib包分离

重点讲maven如何支持jar包和lib包分离方式:

pom:

    <build>
<plugins> <plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>prepare-package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>cn.huilink.cloud.eureka.SpringCloudEurekaApplication</mainClass>
<!-- 入口程序 -->
<addClasspath>true</addClasspath>
<!-- 添加依赖jar路径 -->
<classpathPrefix>lib/</classpathPrefix>
</manifest> </archive>
</configuration>
</plugin> </plugins>
</build>

配置maven运行方式,或者maven工具的Lifecycle->package

spring boot打jar包(maven对jar和lib分离)

注意:

如果打包后运行

错误: 找不到或无法加载主类 

解决办法:菜单中点Build->Build Project,然后在target目录中会编译出classes等目录,然后在打包

参考:

idea工具将SpringBoot工程打包成 jar或war