When running the jar package produced by Gradle, an error 'No main manifest attribute, in xxxx.jar' is reported and the referenced package cannot be found.

foreword

After this problem occurred, I checked a lot of information, but I couldn't solve it. Including introducing the spring-boot-gradle-plugin package, modifying the springboot version, and modifying the gradle version, none of them can be solved.

text

The main problem is that there is no main class in the META-INF/MANIFEST.MF configuration under the jar package that is typed out.
insert image description here
Add it at the end of build.gradle

jar {
    manifest {
    	// 标志主类
        attributes 'Main-Class' : 'com.xxx.Main' 
    }
}

Take a look after packaging again, now the main class configuration has appeared in MANIFEST.MF
insert image description here

additional error

The error is reported again when running, and the externally referenced Spring package cannot be found.
insert image description here
Modify the build.gradle just written, and add the Class-Path configuration after the main class configuration

jar {
    manifest {
    	// 标志主类
        attributes 'Main-Class' : 'com.xxx.Main' 
		// 添加引用的外部包的索引,避免运行时引用不到
		// 我这边是把外部包放在libs目录下,根据项目情况修改下面的目录就好
        String someString = configurations.compileClasspath.files.collect { "libs/$it.name" }.join(' ')
        configurations.runtimeClasspath.each { someString = someString + " libs/" + it.name + ' ' }
        attributes 'Class-Path': someString
    }
}

Then pack it and look at MANIFEST.MF. The Class-Path configuration appears below, so there is no map.
Throw the jar package on the server and execute it.
insert image description here
Perfect.

project structure

insert image description here

additional supplement

Additional workarounds I've seen:

In the build.gradle file, add spring-boot-gradle-plugin

apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

buildscript {
    
    
   ...
    dependencies {
    
    
        classpath("org.springframework.boot:spring-boot-gradle-plugin:2.3.7.RELEASE")
       ...
    }
}
...

dependencies {
    
    
 //框架包
...
}

Quoted from https://blog.csdn.net/csdnzyb/article/details/115378818

Guess you like

Origin blog.csdn.net/sinat_18538231/article/details/126739571