使用Spring Boot的入门版Web父级时出现无效的包装错误

时间:2018-10-04 09:48:01

标签: spring maven spring-boot kotlin

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

[ERROR] [ERROR] Some problems were encountered while processing the POMs:
[ERROR] Invalid packaging for parent POM org.springframework.boot:spring-boot-starter-web:2.0.5.RELEASE, must be "pom" but is "jar" @ org.springframework.boot:spring-boot-starter-web:2.0.5.RELEASE
 @ 
[ERROR] The build could not read 1 project -> [Help 1]
[ERROR]   
[ERROR]   The project com.company:my-artifact:1.0-SNAPSHOT (/home/user/Projects/my-artifact/pom.xml) has 1 error
[ERROR]     Invalid packaging for parent POM org.springframework.boot:spring-boot-starter-web:2.0.5.RELEASE, must be "pom" but is "jar" @ org.springframework.boot:spring-boot-starter-web:2.0.5.RELEASE

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

这是我的pom.xml的MWE:

<?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.xmlsrc/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是否应该用作父母?

2 个答案:

答案 0 :(得分:2)

您必须将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>

答案 1 :(得分:0)

我建议使用https://docs.spring.io中的“ Spring Boot参考指南”