首页 > 解决方案 > 使用 Spring Boot 的 starter web parent 时出现无效的打包错误

问题描述

我想spring-boot-starter-web为我的项目使用父级。不幸的是,如果我尝试使用它,我会收到以下错误消息:

[ERROR] [ERROR] 处理 POM 时遇到了一些问题:
[错误]父 POM org.springframework.boot:spring-boot-starter-web:2.0.5.RELEASE 的包装无效,必须是“pom”但是“jar”@org.springframework.boot:spring-boot-starter -web:2.0.5.RELEASE
 @
[错误] 构建无法读取 1 个项目 -> [帮助 1]
[错误]   
[错误] 项目 com.company:my-artifact:1.0-SNAPSHOT (/home/user/Projects/my-artifact/pom.xml) 有 1 个错误
[错误]     父 POM org.springframework.boot:spring-boot-starter-web:2.0.5.RELEASE 的包装无效,必须是“pom”但是“jar”@org.springframework.boot:spring-boot-starter -web:2.0.5.RELEASE

如果我将其更改为parent不会spring-boot-starter-parent发生此错误,并且构建完成且没有错误。

这是我的 MWE pom.xml

<?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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.company</groupId>
    <artifactId>my-artifact</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <version>2.0.5.RELEASE</version>
        <relativePath/>
    </parent>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <kotlin.version>1.2.71</kotlin.version>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-stdlib-jdk8</artifactId>
            <version>${kotlin.version}</version>
        </dependency>
    </dependencies>
    <build>
        <sourceDirectory>src/main/kotlin</sourceDirectory>
        <testSourceDirectory>src/test/kotlin</testSourceDirectory>
        <plugins>
            <plugin>
                <groupId>org.jetbrains.kotlin</groupId>
                <artifactId>kotlin-maven-plugin</artifactId>
                <version>${kotlin.version}</version>
                <configuration>
                    <jvmTarget>1.8</jvmTarget>
                </configuration>
                <executions>
                    <execution>
                        <id>compile</id>
                        <phase>compile</phase>
                        <goals>
                            <goal>compile</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

该项目是使用 IntelliJ 创建的 Kotlin 项目,通过从原型创建一个新的 Maven 项目并为 JVM 原型选择 kotlin,因此我拥有的唯一文件是pom.xml以下src/main/kotlin/com/company/Hello.kt内容:

package com.company

fun main(args: Array<String>) {
    println("Hello, World")
}

和下的测试文件src/test/kotlin/com/company/HelloTest.kt

package com.copmany

import org.junit.Test
import kotlin.test.assertEquals

class HelloTest {

}

据我所知,我的 POM 是正确的。我尝试了mvn clean build多次,但下载的依赖项似乎没有问题。不spring-boot-starter-web应该被用作父母吗?

标签: springmavenspring-bootkotlin

解决方案


你必须设置spring-boot-starter-parent为父母

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.5.RELEASE</version>
</parent>

作为你的 spring-boot-starter-web依赖

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.jetbrains.kotlin</groupId>
        <artifactId>kotlin-stdlib-jdk8</artifactId>
        <version>${kotlin.version}</version>
    </dependency>
</dependencies>

推荐阅读