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

后端 未结 1 1642
面向向阳花
面向向阳花 2021-02-20 12:36

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

相关标签:
1条回答
  • 2021-02-20 13:20

    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.

    0 讨论(0)
提交回复
热议问题