Spring项目构建生成可执行Jar与依赖Jar时出现“no main manifest attribute in jar”错误的解决咨询
Hey there, let's break down why you're seeing that error and get your dual jars working properly!
The Root Cause
When you configure the spring-boot-maven-plugin with <classifier>exec</classifier>, the plugin does two key things:
- It builds a standard, non-executable JAR (without the
execsuffix) meant to be used as a dependency. This JAR doesn't include the main manifest attribute because it's not designed to run directly. - It creates a second executable JAR with the
execsuffix, which includes all necessary manifest entries (like the main class) to launch your Spring Boot app.
The error you're hitting happens because you're trying to run the non-executable dependency JAR instead of the exec-suffixed executable version.
Step-by-Step Fixes
1. Ensure Your Main Class is Properly Recognized
First, make sure Spring Boot knows where your application's main class lives. You can use either of these approaches:
- Add the
@SpringBootApplicationannotation to your main class (the plugin usually auto-discovers this, but it's good to confirm it's present), or - Explicitly define the main class in the plugin configuration to avoid auto-detection mishaps:
<configuration> <classifier>exec</classifier> <mainClass>com.your.package.YourSpringBootMainClass</mainClass> </configuration>
2. Optimize Your Plugin Configuration
Your original setup is on the right track, but let's make it more explicit to avoid confusion. Here's a complete, reliable configuration:
<build> <plugins> <!-- Generate the standard dependency JAR (non-executable) --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>3.3.0</version> <executions> <execution> <id>default-jar</id> <phase>package</phase> <goals> <goal>jar</goal> </goals> <configuration> <archive> <manifest> <!-- Disable classpath entries since this is a dependency JAR --> <addClasspath>false</addClasspath> </manifest> </archive> </configuration> </execution> </executions> </plugin> <!-- Generate the executable JAR with 'exec' classifier --> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>${spring.boot.version}</version> <executions> <execution> <id>repackage-executable</id> <goals> <goal>repackage</goal> </goals> <configuration> <classifier>exec</classifier> <!-- Explicit main class to eliminate auto-detection issues --> <mainClass>com.your.package.YourSpringBootMainClass</mainClass> </configuration> </execution> </executions> </plugin> </plugins> </build>
3. Run the Correct JAR
After running mvn clean package, check your target directory. You'll see two files:
your-project-name-<version>.jar: This is the dependency JAR (non-executable—don't run this one)your-project-name-<version>-exec.jar: This is the executable JAR. Launch it with:java -jar your-project-name-<version>-exec.jar
Troubleshooting Tips
- Double-check the main class path in your plugin configuration—typos here are a super common culprit.
- Scan your Maven build logs to confirm the Spring Boot plugin picks up the right main class. You should see a line like
[INFO] Repackaging JAR ... with main class: com.your.package.YourSpringBootMainClass. - If you're working in a multi-module project, ensure this plugin configuration is applied to the module that contains your main application class.
内容的提问来源于stack exchange,提问作者Sardothien




