问题:

springBoot项目 没有注清单 springboot打包没有主清单属性_maven

 

解决方案

1.修改pom.xml文件的build标签为

该标签作用:

添加一个SpringBoot的构建的插件

<build>
        <plugins>
            <plugin>
                <!-- Build an executable JAR -->
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

结果:失败 分析:虽然查阅网上的资料以及官网的技术文档都给出上述解决方案,但是对于我这个demo还是不行

2.分析打包出来的jar包文件

2.1分析
1.在这里有一个问题就是主清单属性是什么?
以SpringBoot为例,jar包中包含了三个文件夹:BOOT-INF,META-INF,org,可以把jar包解压到文件夹下查看, 其中META-INF文件夹下有一个MANIFEST.MF文件,该文件指明了程序的入口以及版本信息等内容,如下 应为:

Manifest-Version: 1.0
    Implementation-Title: spring-xxx-xxx
    Implementation-Version: 0.0.1-SNAPSHOT
    Archiver-Version: Plexus Archiver
    Built-By: XXXX
    Implementation-Vendor-Id: com.huyikang.practice
    Spring-Boot-Version: 1.5.9.RELEASE
    Implementation-Vendor: Pivotal Software, Inc.
    Main-Class: org.springframework.boot.loader.JarLauncher
    Start-Class: com.huyikang.practice.eureka.Application
    Spring-Boot-Classes: BOOT-INF/classes/
    Spring-Boot-Lib: BOOT-INF/lib/
    Created-By: Apache Maven 3.5.2
    Build-Jdk: 1.8.0_151
    Implementation-URL: http://maven.apache.org

Main-Class代表了Spring Boot中启动jar包的程序
Start-Class属性就代表了Spring Boot程序的入口类,这个类中应该有一个main方法
Spring-Boot-Classes代表了类的路径,所有编译后的class文件,以及配置文件,都存储在该路径下
Spring-Boot-Lib表示依赖的jar包存储的位置
这些值都是SpringBoot打包插件会默认生成的,如果没有这些属性,SpringBoot程序自然不能运行,就会报错:jar中没有主清单属性,也就是说没有按照SpringBoot的要求,生成这些必须的属性。

而我运行失败的jar包文件结构为

解压:springboot_profiles-0.0.1-SNAPSHOT.jar

springBoot项目 没有注清单 springboot打包没有主清单属性_springBoot项目 没有注清单_02


  其中的MANIFEST.MF文件为:

Manifest-Version: 1.0
Created-By: Maven JAR Plugin 3.2.2
Build-Jdk-Spec: 16
Implementation-Title: springboot_profiles
Implementation-Version: 0.0.1-SNAPSHOT

显然,我打包出来的jar包存在问题,或者说我的这个打包插件有问题,亦或者打包方式存在问题

2.2.成功的jar包以及运行
借用黑马的代码进行分析
成功打包运行成功的的MANIFEST.MF文件内容为

Manifest-Version: 1.0
Implementation-Title: springboot-profiles
Implementation-Version: 0.0.1-SNAPSHOT
Start-Class: com.itheima.springbootprofiles.SpringbootProfilesApplication
Spring-Boot-Classes: BOOT-INF/classes/
Spring-Boot-Lib: BOOT-INF/lib/
Build-Jdk-Spec: 1.8
Spring-Boot-Version: 2.1.8.RELEASE
Created-By: Maven Archiver 3.4.0
Main-Class: org.springframework.boot.loader.JarLauncher

运行: 

springBoot项目 没有注清单 springboot打包没有主清单属性_maven_03

 

3.使用mvn package打包

springBoot项目 没有注清单 springboot打包没有主清单属性_spring boot_04

 运行:  

springBoot项目 没有注清单 springboot打包没有主清单属性_spring_05

 

MANIFEST.MF文件内容为

Manifest-Version: 1.0
Created-By: Maven JAR Plugin 3.2.2
Build-Jdk-Spec: 16
Implementation-Title: springboot_profiles
Implementation-Version: 0.0.1-SNAPSHOT
Main-Class: org.springframework.boot.loader.JarLauncher
Start-Class: com.example.springboot_profiles.SpringbootProfilesApplicati
 on
Spring-Boot-Version: 2.7.5
Spring-Boot-Classes: BOOT-INF/classes/
Spring-Boot-Lib: BOOT-INF/lib/
Spring-Boot-Classpath-Index: BOOT-INF/classpath.idx
Spring-Boot-Layers-Index: BOOT-INF/layers.idx

所以,结果显而易见,之前使用maven的插件打包存在问题

4.修改打包方式

修改pom.xml文件的build标签

<!--<build>
        <finalName>{file_name}</finalName><!– 导出jar包的名字 –>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.2.0</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <transformers>
                                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>{Mainclass}</mainClass>
                                    <!– 主类的位置,修改成主配置类即可,主类配置应为: –>
                                    <!– <mainClass>fatcats.top.App</mainClass> –>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>-->

5.配置打包插件

<!--使用maven-jar-plugin和maven-dependency-plugin插件打包-->
    <build>
        <plugins>

            <!-- 使用maven-jar-plugin和maven-dependency-plugin插件打包 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.1.0</version>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>lib/</classpathPrefix>
                            <mainClass>com.test.api.MyMain</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>3.1.1</version>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}/lib</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

        </plugins>
    </build>
</project>