Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which URLs does Maven resolve with a subpath in child POMs?

Tags:

java

maven

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:

  • <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.

like image 513
Garret Wilson Avatar asked May 14 '18 00:05

Garret Wilson


People also ask

What is a POM file in a Maven project?

A Project Object Model or POM is the fundamental unit of work in Maven. It is an XML file that contains information about the project and configuration details used by Maven to build the project.

What is parent tag in POM XML?

As the name suggests, we can point out a parent pom.xml file for the current pom.xml file. Doing so, dependencies, properties, constants and many more defined at the parent pom.xml file also get merged with the current pom.xml (child pom.xml) file.

What is relative path in POM XML?

relativePath allows you to select a different location, for example when your structure is flat, or deeper without an intermediate parent POM. However, the group ID, artifact ID and version are still required, and must match the file in the location given or it will revert to the repository for the POM.

Where is the POM XML file located?

The POM file is named pom. xml and should be located in the root directory of your project.


1 Answers

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):

...
<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>
...

Project directory

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>
...

Path-adjusted project

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

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

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:

<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>

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

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>
...

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:

<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>
...

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

like image 190
default locale Avatar answered Oct 01 '22 09:10

default locale