java运行Maven构建的Springboot项目报".xxxxx-1.0-SNAPSHOT.jar中没有主清单属性"错误

2022-06-15 15:14:15 浏览数 (1)

错误详情

将Maven构建的Springboot项目打包后使用java -jar xxxxx.jar运行报.xxxxx-1.0-SNAPSHOT.jar中没有主清单属性错误。

原因

Maven在打包时没有配置主类。

解决方法

网上的教程大都只让加<plugin>中的<groupId><artifactId><version>三项,根本不起作用,还需要加上<executions>中的内容才行。

pom.xml中添加如下配置,即可自动检测主类:

代码语言:html复制
<build>

        <plugins>

            <plugin>

                <groupId>org.springframework.boot</groupId>

                <artifactId>spring-boot-maven-plugin</artifactId>

                <version>2.0.0.RELEASE</version>

                <executions>

                    <execution>

                        <goals>

                            <goal>repackage</goal>

                        </goals>

                    </execution>

                </executions>

            </plugin>

        </plugins>

    </build>

如下为pom.xml完整配置,可对照更改(例如<version>什么的):

代码语言:html复制
<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0"

         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>



    <groupId>com.example</groupId>

    <artifactId>xxxxx</artifactId>

    <version>1.0-SNAPSHOT</version>

    <dependencies>

        <dependency>

            <groupId>org.springframework.boot</groupId>

            <artifactId>spring-boot-autoconfigure</artifactId>

            <version>2.0.0.RELEASE</version>

        </dependency>

        <dependency>

            <groupId>org.springframework.boot</groupId>

            <artifactId>spring-boot-starter-web</artifactId>

            <version>2.0.0.RELEASE</version>

        </dependency>

        <dependency>

            <groupId>mysql</groupId>

            <artifactId>mysql-connector-java</artifactId>

            <version>8.0.29</version>

            <scope>compile</scope>

            <exclusions>

                <exclusion>

                    <groupId>com.baomidou</groupId>

                    <artifactId>mybatis-plus-boot-starter</artifactId>

                </exclusion>

            </exclusions>

        </dependency>

        <dependency>

            <groupId>org.bouncycastle</groupId>

            <artifactId>bcprov-ext-jdk16</artifactId>

            <version>1.46</version>

            <type>jar</type>

            <scope>compile</scope>

        </dependency>

        <dependency>

            <groupId>commons-codec</groupId>

            <artifactId>commons-codec</artifactId>

            <version>1.6</version>

        </dependency>

        <!-- smsChan用的 -->

        <dependency>

            <groupId>com.alibaba</groupId>

            <artifactId>fastjson</artifactId>

            <version>1.2.72</version>

        </dependency>



    </dependencies>



    <build>

        <plugins>

            <plugin>

                <groupId>org.springframework.boot</groupId>

                <artifactId>spring-boot-maven-plugin</artifactId>

                <version>2.0.0.RELEASE</version>

                <executions>

                    <execution>

                        <goals>

                            <goal>repackage</goal>

                        </goals>

                    </execution>

                </executions>

            </plugin>

        </plugins>

    </build>



    <properties>

        <maven.compiler.source>11</maven.compiler.source>

        <maven.compiler.target>11</maven.compiler.target>

    </properties>



</project>

0 人点赞