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

后端 未结 1 1621
面向向阳花
面向向阳花 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):

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

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