启动springboot项目时,出现以下异常
no main manifest attribute, in ./XXX.jar
这是因为启动jar包时,未找到主类
解决: 使用spring-boot-maven-plugin插件,在pom.xml配置以下内容
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.5.0</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>主类全名</mainClass>
</configuration>
</plugin>
</plugins>
</build>
需要注意一点,spring-boot-maven-plugin尽量选择高版本,(2.3.12.RELEASE)低版本可以打包成功,但是启动时依旧找不到主类。
目前最新版本已经是2.7.2了,项目里面使用,要跟spring-boot-starter-web版本尽量保持一致,后续升级也好管理。
在pom.xml配置好spring-boot-maven-plugin,先clean、后package即可完成打包
如果不使用spring-boot-maven-plugin插件怎么打包?
1、使用maven-shade-plugin,配置之后执行mvn clean package就OK了。
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.cn.dl.main.ApplicationJob</mainClass>
</transformer>
<transformer
implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.handlers</resource>
</transformer>
<transformer
implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.schemas</resource>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
2、使用maven-assembly-plugin插件,然后执行mvn clean assembly:assembly
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>com.cn.dl.main.ApplicationJob</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>