为什么Maven会抛出类似“必须是pom却是jar”这样的错误?

时间:2019-12-11 13:55:03

标签: java maven

我需要修复以下Maven错误。

当我跑步时:

  

mvn全新安装

我明白了:

[ERROR] The build could not read 1 project -> [Help 1]
org.apache.maven.project.ProjectBuildingException: Some problems were encountered while processing the POMs:
[ERROR] Invalid packaging for parent POM org.springframework.boot:spring-boot-starter-parent:2.0.5.RELEASE, must be "pom" but is "jar" @ org.springframework.boot:spring-boot-starter-parent:2.0.5.RELEASE
[ERROR] 'packaging' with value 'jar' is invalid. Aggregator projects require 'pom' as packaging. @ line 3, column 102

这是我的包裹层次结构:

.
├── pom.xml
|
└── application
    └── pom.xml

下面是我的pom根目录:

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.tim.project</groupId>
    <artifactId>myservice</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>${project.artifactId}-${project.version}</name>
    <description>My Service</description>
    <packaging>pom</packaging>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.5.RELEASE</version>
        <relativePath/>
    </parent>

    <modules>
        <module>application</module>
    </modules>

</project>

下面是我在应用程序文件夹中的pom:

<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
         xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

    <modelVersion>4.0.0</modelVersion>
    <groupId>com.tom.project</groupId>
    <artifactId>myservice-application</artifactId>
    <version>0.0.2</version>
    <packaging>jar</packaging>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
    </properties>

    <repositories>
        <repository>
          ...
        </repository>
    </repositories>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-sleuth</artifactId>
                <version>2.0.0.RC1</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

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

    </dependencies>

</project>

2 个答案:

答案 0 :(得分:1)

应用程序文件夹中的pom.xml丢失:

<parent>
    <groupId>com.tim.project</groupId>
    <artifactId>myservice</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</parent>

答案 1 :(得分:0)

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

尝试删除<relativePath/>并将其替换为<type>pom</type>

编辑

我有一些非常相似的东西: 根pom

<groupId>org.company</groupId>
    <artifactId>my-parent</artifactId>
    <version>1.1</version>
    <packaging>pom</packaging>

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

然后申请:

<artifactId>my-spring-app</artifactId>
    <packaging>jar</packaging>

    <parent>
        <groupId>org.company</groupId>
        <artifactId>my-parent</artifactId>
        <version>1.1</version><!-- version can be different of course-->
    </parent>