maven module 路径_Maven使用子POM中的子路径解析哪些URL?

Maven在继承父POM时,会根据子模块的artifactId自动添加子路径到某些URL。项目.url、site.url和scm.url的默认值会包含路径调整和artifactId。但当子模块设置project.directory属性时,所有三个URL的artifactId将被该项目.directory覆盖。通过实际示例展示了不同情况下的URL解析结果。
摘要由CSDN通过智能技术生成

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.

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:

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.

解决方案

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

Here's the documentation for urls:

project.url (line 102):

The URL to the project's homepage.

Default value is: parent value [+ path adjustment] + (artifactId or project.directory property)

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

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

site.url (line 544):

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 (line 823):

The URL to the project's browsable SCM repository, such as ViewVC or Fisheye.

Default value is: parent value [+ path adjustment] + artifactId

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

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

Example

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

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

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

Parent project pom

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

...

https://example.com

https://example.com/scm

child-project

path/path-adjusted-child

child-with-a-project-directory

https://example.com/distribution

...

Project directory

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

...

1.8

1.8

project

...

Path-adjusted project

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

...

...

../../pom.xml

...

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

Results

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

Here's the output for all of them:

child-project:

https://example.com/child-project

https://example.com/scm/child-project

https://example.com/distribution/child-project

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

child-with-a-project-directory:

...

https://example.com/project

https://example.com/scm/project

https://example.com/distribution/project

...

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:

https://example.com/path/path-adjusted-child

https://example.com/scm/path/path-adjusted-child

https://example.com/distribution/path/path-adjusted-child

...

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值