Maven使用子POM中的子路径解析哪些URL? [英] Which URLs does Maven resolve with a subpath in child POMs?

查看:785
本文介绍了Maven使用子POM中的子路径解析哪些URL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在某处读到如果Maven项目使用继承,那么它的子项目将使用子项目的 artifactId 自动为URL添加子路径。但现在我找不到引用。

I read somewhere that if a Maven project uses inheritance, then its child projects will automatically add a subpath to the URL using the child projects's artifactId. But now I can't find the reference.

哪些继承的URL将Maven从父POM逐字继承,并为子POM添加子路径?例如,以下是我可以找到的一些网址:

Which inherited URLs will Maven inherit verbatim from the parent POM, and to which will it add a subpath for the child POM? For example, here are a few URLs I could find:


  • < project>< url>

  • < project>< scm>< url>

  • < project>< distributionManagement>< site>< url>

  • <project><url>
  • <project><scm><url>
  • <project><distributionManagement><site><url>

也许我错了,他们都没有添加一个子路径,但我在笔记中有它。不幸的是,我似乎找不到使用互联网搜索的明确答案。

Maybe I'm mistaken and none of them add a subpath, but I have it in my notes. Unfortunately I can't seem to find a definitive answer using Internet searches.

请尽量在答案中提供权威性参考。 (当然如果没有关于此的文档,直接测试将是第二好的。)提前感谢。

Please try to provide an authoritative reference in your answer. (Of course if there is no documentation on this, direct testing will be second best.) Thanks in advance.

推荐答案

全部这些元素在 maven XSD架构中定义,该架构已正确记录( xs:documentation tags)。

All of these elements are defined in the maven XSD schema which is properly documented (xs:documentation tags).

以下是网址的文档:

project.url (第102行):


网址到项目的主页。

默认值是:父值[+路径调整] +(artifactId或project.directory属性)

The URL to the project's homepage.
Default value is: parent value [+ path adjustment] + (artifactId or project.directory property)

路径调整仅适用于子项目的路径与其artifactid不匹配的情况。

Path adjustment only applies when the path to a child project doesn't match its artifactid.

project.directory 是一个属性,您可以在 <中覆盖它code>属性 代码。

project.directory is a property and you can override it in the properties tag.

site.url (第544行):


部署网站的位置的网址,格式为 protocol:// hostname / path
默认值为:父值[+路径调整] + artifactId

The url of the location where website is deployed, in the form protocol://hostname/path.
Default value is: parent value [+ path adjustment] + artifactId

scm.url (第823行):


项目可浏览SCM存储库的URL,例如作为ViewVC或Fisheye。
默认值为:父值[+路径调整] + artifactId

The URL to the project's browsable SCM repository, such as ViewVC or Fisheye.
Default value is: parent value [+ path adjustment] + artifactId

所以,所有这些元素的默认值包括路径调整和artifactId。

So, the default value for all of these elements includes both path adjustment and artifactId.

还有其他几个名为 url 的元素。并非所有这些都是路径调整的,例如存储库的url不是。

There're several other elements named url. Not all of them are path adjusted, e.g. the url of a repository is not.

让我们创建一个涵盖上述所有选项的示例。

Let's create an example that covers all of the options above.

对于这个例子,我们需要一个带有三个模块的项目( maven-urls ):

For this example, we'll need a project (maven-urls) with three modules:


  • child-project - 一个不覆盖任何内容的常规子模块。

  • child-with-a-project-directory - 一个声明 project.directory property。

  • path-adjusted-child - 具有意外相对路径的子模块。

  • child-project - a regular submodule that doesn't override anything.
  • child-with-a-project-directory - a submodule that declares a project.directory property.
  • path-adjusted-child - a submodule that has an unexpected relative path.

。目录结构应如下所示:

. The directory structure should look like this:

maven-urls
    child-project
    child-with-a-project-directory
    path
        path-adjusted-child

父项目pom

这里我们将声明三个不同的网址和子项目(注意 path-adjusted-child的路径):

Here we'll declare three different urls and child projects (note the path to path-adjusted-child):

...
<url>https://example.com</url>
<scm>
    <url>https://example.com/scm</url>
</scm>
<modules>
    <module>child-project</module>
    <module>path/path-adjusted-child</module>
    <module>child-with-a-project-directory</module>
</modules>
<distributionManagement>
    <site>
        <url>https://example.com/distribution</url>
    </site>
</distributionManagement>
...

项目目录

对于 child-with-a-project-directory ,我们将设置 project.directory property

For a child-with-a-project-directory we'll set a project.directory property

...
<properties>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    <project.directory>project</project.directory>
</properties>
...

路径调整项目

路径调整项目需要声明父模块的相对路径:

A path-adjusted project needs to declare a relative path to the parent module:

...
<parent>
    ...
    <relativePath>../../pom.xml</relativePath>
</parent>
...

您可以查看github上的完整代码: https://github.com/defaultlocale/maven-urls

You can check out the full code on github: https://github.com/defaultlocale/maven-urls.

现在我们可以构建整个项目并检查值。我们可以使用maven帮助插件,它是 effective-pom 目标,找到每个子项目的有效网址。

Now we can build the whole project and check the values. We can use maven help plugin and it's effective-pom goal to find the effective URLs for each of the child projects.

mvn help:effective-pom

这是所有这些的输出:

子项目

child-project:

<url>https://example.com/child-project</url>
<scm>
    <url>https://example.com/scm/child-project</url>
</scm>
<distributionManagement>
    <site>
        <url>https://example.com/distribution/child-project</url>
    </site>
</distributionManagement>

此处没有任何意外。每个网址只是一个父网址,附加 artifactId

Nothing unexpected here. Every URLs is just a parent URL with an artifactId attached.

child -with-a-project-directory

child-with-a-project-directory:

...
<url>https://example.com/project</url>
<scm>
    <url>https://example.com/scm/project</url>
</scm>
<distributionManagement>
    <site>
        <url>https://example.com/distribution/project</url>
    </site>
</distributionManagement>
...

事实证明, project.directory 覆盖所有三个网址的 artifactId 。这是意料之外的,不在文档中讨论。

As it turns out, project.directory overrides artifactId for all three URLs. This is unexpected and is not covered in the documentation.

path-adjusted-child

path-adjusted-child:

<url>https://example.com/path/path-adjusted-child</url>
<scm>
    <url>https://example.com/scm/path/path-adjusted-child</url>
</scm>
<distributionManagement>
    <site>
        <url>https://example.com/distribution/path/path-adjusted-child</url>
    </site>
</distributionManagement>
...

此处没有任何惊喜,每个网址都包含相对路径和artifactId。

No surprises here every url includes a relative path and artifactId.

这篇关于Maven使用子POM中的子路径解析哪些URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆