所有这些元素都在正确记录的maven XSD schema 中定义(xs:documentation 标记)。
这是 url 的文档:
project.url(第 102 行):
项目主页的URL。
默认值为:父值[+路径调整]+(artifactId或project.directory属性)
路径调整仅适用于子项目的路径与其 artifactid 不匹配的情况。
project.directory 是一个属性,您可以在properties 标记中覆盖它。
site.url(第 544 行):
网站部署位置的url,格式为protocol://hostname/path。
默认值为:父值[+路径调整]+artifactId
scm.url(第 823 行):
项目的可浏览 SCM 存储库的 URL,例如 ViewVC 或 Fisheye。
默认值为:父值[+路径调整]+artifactId
因此,所有这些元素的默认值都包括路径调整和 artifactId。
还有其他几个名为 url 的元素。并非所有这些都经过路径调整,例如存储库的 url 不是。
示例
让我们创建一个涵盖上述所有选项的示例。
对于这个例子,我们需要一个包含三个模块的项目 (maven-urls):
-
child-project - 一个不覆盖任何东西的常规子模块。
-
child-with-a-project-directory - 声明 project.directory 属性的子模块。
-
path-adjusted-child - 具有意外相对路径的子模块。
。目录结构应如下所示:
maven-urls
child-project
child-with-a-project-directory
path
path-adjusted-child
父项目pom
这里我们将声明三个不同的 url 和子项目(注意 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 属性
...
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<project.directory>project</project.directory>
</properties>
...
路径调整项目
经过路径调整的项目需要声明到父模块的相对路径:
...
<parent>
...
<relativePath>../../pom.xml</relativePath>
</parent>
...
您可以在 github 上查看完整代码:https://github.com/defaultlocale/maven-urls。
结果
现在我们可以构建整个项目并检查值。我们可以使用 maven 帮助插件,effective-pom 的目标是为每个子项目找到有效的 URL。
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>
这里没有什么意外的。每个 URL 都只是一个附加了 artifactId 的父 URL。
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 会覆盖所有三个 URL 的 artifactId。这是意料之外的,文档中没有介绍。
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>
...
毫无疑问,每个 url 都包含一个相对路径和 artifactId。