简体   繁体   English

如何将 maven 父子项目设置为单独的项目?

[英]How to setup maven parent and child project as separate projects?

There is a project (type=pom), which is supposed to be used as a parent for another project.有一个项目(type=pom),它应该用作另一个项目的父级。

parent父母

<project>
  <modelVersion>4.0.0</modelVersion>

  <groupId>my-firm</groupId>
  <artifactId>custom-parent</artifactId>
  <version>1.0.0</version>
  <packaging>pom</packaging>

  <distributionManagement>
    <repository>
      <id>lib-repo-local</id>
      <name>my-releases</name>
      <url>http://artifactory.local:8081/artifactory/libs-release-local</url>
    </repository>
    <snapshotRepository>
      <id>lib-repo-snapshots</id>
      <name>my-snapshots</name>
      <url>http://artifactory.local:8081/artifactory/libs-snapshot-local</url>
    </snapshotRepository>
  </distributionManagement>

</project>

child孩子

<project>
  <modelVersion>4.0.0</modelVersion>

  <parent>
    <groupId>my-firm</groupId>
    <artifactId>custom-parent</artifactId>
    <version>1.0.0</version>
  </parent>

  <artifactId>child-sample</artifactId>
  <version>0.0.1</version>
  <packaging>jar</packaging>
    
  <distributionManagement>
    <repository>
      <id>lib-repo-local</id>
      <name>my-releases</name>
      <url>http://artifactory.local:8081/artifactory/libs-release-local</url>
    </repository>
    <snapshotRepository>
      <id>lib-repo-snapshots</id>
      <name>my-snapshots</name>
      <url>http://artifactory.local:8081/artifactory/libs-snapshot-local</url>
    </snapshotRepository>
  </distributionManagement>

</project>

The parent project is deployed to the remote repository - artifact my-firm:custom-parent:1.0.0 is available.父项目部署到远程存储库 - 工件my-firm:custom-parent:1.0.0可用。

When I run mvn clean on the child project there's an error当我在子项目上运行mvn clean时出现错误

[FATAL] Non-resolvable parent POM for my-firm:child-sample:0.0.1: 
        Failure to find my-firm:custom-parent:1.0.0 in https://repo.maven.apache.org/maven2
        was cached in the local repository, resolution will not be reattempted until
        the update interval of central has elapsed or updates are forced
        and 'parent.relativePath' points at wrong local POM @ line 7, column 11

All the three points in the error message seem to be unrelated to my intentions.错误消息中的所有三点似乎都与我的意图无关。

  1. Maven does not try to look in the repo described in distributionManagement , but complaints about maven.central Maven 没有尝试查看distributionManagement中描述的repo,但抱怨maven.central
  2. Nothing is cached in local repository - all artifacts removed from there before the build.本地存储库中没有缓存任何内容 - 在构建之前从那里删除的所有工件。
  3. The parent.relativePath is intentionally absent to have the child project agnostic to the parent project location and let it rely only on the deployed artifact (parent pom).故意缺少parent.relativePath以使子项目与父项目位置无关,并让它仅依赖于已部署的工件(父 pom)。

Please, show how to edit the pom s to have child and parent as separate projects and let child to depend only on the parent artifact.请展示如何编辑pom以将子项和父项作为单独的项目,并让子项仅依赖于父项工件。

Please note that <distributionManagement> is only for upload.请注意, <distributionManagement>仅用于上传。 This is where Maven puts the artifacts if you run mvn deploy .如果您运行mvn deploy ,这就是 Maven 放置工件的地方。

So if you want Maven to look into your artifactory for the parent POM, you need to add an appropriate <repository> element or -- which is the preferred way -- configure your artifactory in the settings.xml .因此,如果您希望 Maven 查看您的父 POM 的工件,您需要添加适当的<repository>元素,或者 - 这是首选方式 - 在settings.xml中配置您的工件。

Nevertheless if you build the parent first on some machine and then build the child on the same machine, the parent POM is read from the local repository.然而,如果您首先在某台机器上构建父级,然后在同一台机器上构建子级,则从本地存储库中读取父 POM。 It is not removed from there in any way.它不会以任何方式从那里移除。 I don't know what went wrong in your case, but I guess you either had a different local repository for both builds or the content was somehow erased in between.我不知道你的情况出了什么问题,但我猜你要么有两个版本的不同本地存储库,要么内容在两者之间以某种方式被删除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM