如何为项目的子模块指定父POM?

时间:2019-01-09 13:32:10

标签: maven

有一个带有模块的Spring项目:

enter image description here

该项目的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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>com.bni.wfcredit</groupId>
        <artifactId>bni-wfcredit-root</artifactId>
        <version>1.0-SNAPSHOT</version>
        <relativePath>bni-wfcredit-root</relativePath>
    </parent>

    <artifactId>bni-wfcredit</artifactId>
    <packaging>pom</packaging>
    <name>Bni wfcredit</name>
    <url>http://maven.apache.org</url>

    <modules>
        <module>bni-wfcredit-root</module>
  </modules>
</project>

如您所见,有一个父标记指向模块bni-wfcredit-root。

bni-wfcredit-root模块的

pom.xml:

<?xml version="1.0"?>
<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.bni.wfcredit</groupId>
    <artifactId>bni-wfcredit-root</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>pom</packaging>

    <name>Bni wfcredit root</name>
    <url>http://maven.apache.org</url>

    <properties>
        ...
    </properties>

    <dependencyManagement>
        <dependencies>
            ...
        </dependencies>
    </dependencyManagement>

    <dependencies>
        ...
    </dependencies>

    <modules>
        <module>../bni-wfcredit-common</module>
        <module>../bni-wfcredit-applogging</module>
        <module>../bni-wfcredit-checklist</module>
        <module>../bni-wfcredit-instruction</module>
        <module>../bni-wfcredit-messaging</module>
        <module>../bni-wfcredit-organisation</module>
        <module>../bni-wfcredit-security</module>
        <module>../bni-wfcredit-workflow</module>
        <module>../bni-wfcredit-mainapp</module>
    </modules>

    <!-- Build -->
    <build>
        ...
    </build>
</project>

当我在目录D:\ Projets \ bni-wfcredit执行mvn -X clean install时,我会收到错误消息:

[ERROR] The build could not read 8 projects -> [Help 1]
org.apache.maven.project.ProjectBuildingException: Some problems were encountered while processing the POMs:
[WARNING] 'parent.relativePath' points at com.bni.wfcredit:bni-wfcredit instead of com.bni.wfcredit:bni-wfcredit-root, please verify your project structure @ line 6, column 13
[FATAL] Non-resolvable parent POM: Could not find artifact com.bni.wfcredit:bni-wfcredit-root:pom:1.0-SNAPSHOT and 'parent.relativePath' points at wrong local POM @ line 6, column 13
[WARNING] 'parent.relativePath' points at com.bni.wfcredit:bni-wfcredit instead of com.bni.wfcredit:bni-wfcredit-root, please verify your project structure @ line 6, column 13
[FATAL] Non-resolvable parent POM: Could not find artifact com.bni.wfcredit:bni-wfcredit-root:pom:1.0-SNAPSHOT and 'parent.relativePath' points at wrong local POM @ line 6, column 13
[WARNING] 'parent.relativePath' points at com.bni.wfcredit:bni-wfcredit instead of com.bni.wfcredit:bni-wfcredit-root, please verify your project structure @ line 6, column 13
[FATAL] Non-resolvable parent POM: Could not find artifact com.bni.wfcredit:bni-wfcredit-root:pom:1.0-SNAPSHOT and 'parent.relativePath' points at wrong local POM @ line 6, column 13
[WARNING] 'parent.relativePath' points at com.bni.wfcredit:bni-wfcredit instead of com.bni.wfcredit:bni-wfcredit-root, please verify your project structure @ line 5, column 13
[FATAL] Non-resolvable parent POM: Could not find artifact com.bni.wfcredit:bni-wfcredit-root:pom:1.0-SNAPSHOT in JasperReports (http://jaspersoft.artifactoryonline.com/jaspersoft/third                                                                                      -party-ce-artifacts/) and 'parent.relativePath' points at wrong local POM @ line 5, column 13
[WARNING] 'parent.relativePath' points at com.bni.wfcredit:bni-wfcredit instead of com.bni.wfcredit:bni-wfcredit-root, please verify your project structure @ line 7, column 10
[FATAL] Non-resolvable parent POM: Could not find artifact com.bni.wfcredit:bni-wfcredit-root:pom:1.0-SNAPSHOT and 'parent.relativePath' points at wrong local POM @ line 7, column 10
[WARNING] 'parent.relativePath' points at com.bni.wfcredit:bni-wfcredit instead of com.bni.wfcredit:bni-wfcredit-root, please verify your project structure @ line 6, column 13
[FATAL] Non-resolvable parent POM: Could not find artifact com.bni.wfcredit:bni-wfcredit-root:pom:1.0-SNAPSHOT and 'parent.relativePath' points at wrong local POM @ line 6, column 13
[WARNING] 'parent.relativePath' points at com.bni.wfcredit:bni-wfcredit instead of com.bni.wfcredit:bni-wfcredit-root, please verify your project structure @ line 7, column 10
[FATAL] Non-resolvable parent POM: Could not find artifact com.bni.wfcredit:bni-wfcredit-root:pom:1.0-SNAPSHOT and 'parent.relativePath' points at wrong local POM @ line 7, column 10
[WARNING] 'parent.relativePath' points at com.bni.wfcredit:bni-wfcredit instead of com.bni.wfcredit:bni-wfcredit-root, please verify your project structure @ line 7, column 10
[FATAL] Non-resolvable parent POM: Could not find artifact com.bni.wfcredit:bni-wfcredit-root:pom:1.0-SNAPSHOT and 'parent.relativePath' points at wrong local POM @ line 7, column 10

        at org.apache.maven.project.DefaultProjectBuilder.build(DefaultProjectBuilder.java:364)
        at org.apache.maven.DefaultMaven.collectProjects(DefaultMaven.java:637)
        at org.apache.maven.DefaultMaven.getProjectsForMavenReactor(DefaultMaven.java:586)
        at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:229)
        at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:152)
        at org.apache.maven.cli.MavenCli.execute(MavenCli.java:555)
        at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:214)
        at org.apache.maven.cli.MavenCli.main(MavenCli.java:158)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:289)
        at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229)
        at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:415)
        at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356)
[ERROR]
[ERROR]   The project com.bni.wfcredit:bni-wfcredit-common:1.0-SNAPSHOT (D:\Projets\bni-wfcredit\bni-wfcredit-common\pom.xml) has 1 error
[ERROR]     Non-resolvable parent POM: Could not find artifact com.bni.wfcredit:bni-wfcredit-root:pom:1.0-SNAPSHOT and 'parent.relativePath' points at wrong local POM @ line 6, column 1                                                                                      3 -> [Help 2]
org.apache.maven.model.resolution.UnresolvableModelException: Could not find artifact com.bni.wfcredit:bni-wfcredit-root:pom:1.0-SNAPSHOT
        at org.apache.maven.project.ProjectModelResolver.resolveModel(ProjectModelResolver.java:159)
        at org.apache.maven.model.building.DefaultModelBuilder.readParentExternally(DefaultModelBuilder.java:817)
        at org.apache.maven.model.building.DefaultModelBuilder.readParent(DefaultModelBuilder.java:669)
        at org.apache.maven.model.building.DefaultModelBuilder.build(DefaultModelBuilder.java:307)
        at org.apache.maven.project.DefaultProjectBuilder.build(DefaultProjectBuilder.java:411)
        at org.apache.maven.project.DefaultProjectBuilder.build(DefaultProjectBuilder.java:380)
        at org.apache.maven.project.DefaultProjectBuilder.build(DefaultProjectBuilder.java:496)
        at org.apache.maven.project.DefaultProjectBuilder.build(DefaultProjectBuilder.java:380)
        at org.apache.maven.project.DefaultProjectBuilder.build(DefaultProjectBuilder.java:496)
        at org.apache.maven.project.DefaultProjectBuilder.build(DefaultProjectBuilder.java:380)
        at org.apache.maven.project.DefaultProjectBuilder.build(DefaultProjectBuilder.java:344)
        at org.apache.maven.DefaultMaven.collectProjects(DefaultMaven.java:637)
        at org.apache.maven.DefaultMaven.getProjectsForMavenReactor(DefaultMaven.java:586)
        at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:229)
        at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:152)
        at org.apache.maven.cli.MavenCli.execute(MavenCli.java:555)
        at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:214)
        at org.apache.maven.cli.MavenCli.main(MavenCli.java:158)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:289)
        at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229)
        at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:415)
        at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356)
Caused by: org.eclipse.aether.resolution.ArtifactResolutionException: Could not find artifact com.bni.wfcredit:bni-wfcredit-root:pom:1.0-SNAPSHOT
        at org.eclipse.aether.internal.impl.DefaultArtifactResolver.resolve(DefaultArtifactResolver.java:459)
        at org.eclipse.aether.internal.impl.DefaultArtifactResolver.resolveArtifacts(DefaultArtifactResolver.java:262)
        at org.eclipse.aether.internal.impl.DefaultArtifactResolver.resolveArtifact(DefaultArtifactResolver.java:239)
        at org.eclipse.aether.internal.impl.DefaultRepositorySystem.resolveArtifact(DefaultRepositorySystem.java:295)
        at org.apache.maven.project.ProjectModelResolver.resolveModel(ProjectModelResolver.java:155)
        ... 25 more
Caused by: org.eclipse.aether.transfer.ArtifactNotFoundException: Could not find artifact com.bni.wfcredit:bni-wfcredit-root:pom:1.0-SNAPSHOT
        at org.eclipse.aether.internal.impl.DefaultArtifactResolver.resolve(DefaultArtifactResolver.java:449)
        ... 29 more

那么如何指定父项?

3 个答案:

答案 0 :(得分:1)

多模块结构应类似于-

parent
├── pom.xml
├── module
    ├── pom.xml

首先,检查父项目在本地存储库中吗?如果没有,那么您需要将其检出并自行构建。

您需要向<relativePath>添加一个空的<parent>,以便它可以从存储库中解析父pom。

<parent>
   <groupId>com.bni.wfcredit</groupId>
   <artifactId>bni-wfcredit-root</artifactId>
   <version>1.0-SNAPSHOT</version>
   <relativePath></relativePath>
</parent>

您可以阅读更多详细信息here

答案 1 :(得分:0)

在编译时从配置的maven存储库中加载父pom。不是根项目中的那个,也不是模块中的那个。

要在本地安装父pom,您需要在父pom项目上运行以下命令:

  

mvn安装

或者,如果您已经拥有父pom项目jar

  

mvn install:安装文件-Dfile =文件路径

有关更多信息,请参考maven repositoriesmvn install command(如何创建JAR并将其安装在本地存储库中?)

答案 2 :(得分:0)

好,我找到了解决方案(谢谢大家的回答):我将主项目的相对路径设置为:<relativePath>bni-wfcredit-root/pom.xml</relativePath>,并将其他模块的相对路径设置为<relativePath>../bni-wfcredit-root/pom.xml</relativePath>(除了用于根模块:))