repaint-io / maven-tiles

Injecting maven configurations by composition rather than inheritance
155 stars 32 forks source link

How to use parent with relativePath? #126

Open CarstenWickner opened 2 years ago

CarstenWickner commented 2 years ago

We have separated one of our projects into two pom.xml files and make use of the <parent>/<relativePath> property.

  1. A simple but technical "main" pom.xml, that is properly versioned and checked-in:

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>my.example</groupId>
    <artifactId>example-war</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>war</packaging>
    <name>example-war</name>
    
    <parent>
        <groupId>my.example</groupId>
        <artifactId>example-war-parent</artifactId>
        <version>1.0</version>
        <relativePath>example-war-parent.xml</relativePath>
    </parent>
    
    <build>
        <plugins>
            <plugin>
                <groupId>io.repaint.maven</groupId>
                <artifactId>tiles-maven-plugin</artifactId>
                <version>2.23</version>
                <extensions>true</extensions>
                <configuration>
                    <tiles>
                        <tile>my.example.maven-tile:war-tile:1.0</tile>
                    </tiles>
                </configuration>
            </plugin>
        </plugins>
    </build>
    </project>
  2. a local parent POM (which is auto-generated in the CI process), containing all the things we want to include in the war artifact:

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>my.example</groupId>
    <artifactId>example-war-parent</artifactId>
    <version>1.0</version>
    <packaging>pom</packaging>
    <name>example-war-parent</name>
    
    <dependencies>
        <dependency>
            <groupId>my.example</groupId>
            <artifactId>dynamically-built-stuff</artifactId>
            <version>1.0.0-SNAPSHOT</version>
        </dependency>
    </dependencies>
    </project>

When running a local build, one ends up with:

--- tiles-maven-plugin: Injecting 2 tiles as intermediary parent artifacts for com.torqueits:cpms-war...
Mixed 'my.example:example-war:1.0.0-SNAPSHOT' with tile 'my.example.maven-tile:war-tile:1.0' as its new parent.
Mixed 'my.example.maven-tile:war-tile:1.0' with tile 'my.example.maven-tile:nexus-tile:1.0' as its new parent.
Mixed 'my.example.maven-tile:nexus-tile:1.0' with original parent 'my.example:example-war-parent:1.0' as its new top level parent.

Downloading from nexus: https://my.example.com/nexus/content/groups/public/my/example/example-war-parent/1.0/example-war-parent-1.0.pom
Error executing Maven.
Missing:
----------
1) my.example:example-war-parent:pom:1.0
----------
1 required artifact is missing.

for artifact: 
  my.example:example-war-parent:pom:1.0

from the specified remote repositories:
  nexus (https://my.example.com/nexus/content/groups/public, releases=true, snapshots=true)

In our CI environment, we workaround this issue by first installing the parent POM locally before running the main build:

mvn clean install -f example-war-parent.xml
mvn clean install

In a local IDE/build scenario, this extra step is a hassle. Is there a way to respect the parent's <relativePath> instead of attempting to take it from the (possibly outdated) local/remote repository?

talios commented 1 year ago

Just following up on this as I seem to have missed it. Looking at the chain of the code the relative path is preserved in the Parent model class, and we're just reusing that instance when we reattach it to the end of the inheritance chain - so this should work.

I wonder if removing the <version>1.0</version> from your parent declaration makes any difference - it may be that Maven is taking the version number in preference, and looking it up in a remote/local repository as it's not in any reactor?

I'll pull those two pom's out into a CI test to add into the project and give it a try in the morning.

I have added a bit of extra logging there to cover/document the relative path in the mean time as part of testing:

[INFO] --- tiles-maven-plugin: Injecting 1 tiles as intermediary parent artifacts for com.test:civersion-tiletest-ciparent...
[INFO] Mixed 'com.test:civersion-tiletest-ciparent:0.1.0-SNAPSHOT' with tile 'com.test:civersion-tiletest-tile2:1' as its new parent.
[INFO] Mixed 'com.test:civersion-tiletest-tile2:1' with original parent 'com.test:civersion-tiletest-parent3:0.1.0-SNAPSHOT' via ../parent3/pom.xml as its new top level parent.