Home > Back-end >  Maven Inherit Parent profile property as classifier in child
Maven Inherit Parent profile property as classifier in child

Time:09-01

I have multi hierarchy pom maven project.

Parent POM: This contains two profiles with different dependencies and property value (env-type) which is used for classifiers. deploy_mav/pom_multi_cloud.xml ::

    <project>
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.abc</groupId>
    <artifactId>master-pom-multi-cloud</artifactId>
    <version>ver-1.0.0-SNAPSHOT</version>
    <packaging>pom</packaging>

    <name>Master POM Multi Cloud</name>
    <modules>
        <module>pom_multi_cloud_webapp_servlet3.xml</module>
    </modules>
 <build>
        
        <finalName>${project.artifactId}-${project.version}</finalName>

        <plugins>
            <plugin>
                <artifactId>maven-jar-plugin</artifactId>
                <executions>
                    <execution>
                        <id>default-jar</id>
                        <configuration>
                            <classifier>${env-type}</classifier>
                        </configuration>
                    </execution>
                </executions>

            </plugin>
            <plugin>
                <artifactId>maven-install-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <classifier>${env-type}</classifier>
                </configuration>
                <executions>
                    <execution>
                        <id>default-install</id>
                        <phase>none</phase>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <artifactId>maven-source-plugin</artifactId>
                <version>2.2.1</version>
                <configuration>
                    <classifier>${env-type}</classifier>
                </configuration>
                <executions>
                    <execution>
                        <id>default-jar</id>
                        <configuration>
                            <classifier>${env-type}</classifier>
                        </configuration>
                    </execution>
                </executions>

            </plugin>
        </plugins>

   <profiles>
        <profile>
            <id>build_aws</id>
            <properties>
                <env-type>aws</env-type>
            </properties>
            <dependencies>
                <dependency>
                    <groupId> com.abc.org</groupId>
                    <artifactId>aws-common</artifactId>
                    <version>${env-type-common-version}</version>
                </dependency>
            </dependencies>
            <activation>
                <activeByDefault>false</activeByDefault>
            </activation>
        </profile>
        <profile>
            <id>build_gcp</id>
            <properties>
                <env-type>gcp</env-type>
            </properties>
            <dependencies>
                <dependency>
                    <groupId> com.abc.org</groupId>
                    <artifactId>gcp-common</artifactId>
                    <version>${env-type-common-version}</version>
                </dependency>
            </dependencies>
            <activation>
                <activeByDefault>false</activeByDefault>
            </activation>
        </profile>
    </profiles>
</project>

In 1st Level Child pom.xml,

<project>
    <groupId>com.abc.org.plat</groupId>
    <artifactId>apps-service-ser</artifactId>
    <version>ver-1.0.0-SNAPSHOT</version>
    <packaging>pom</packaging>

    <parent>
        <groupId>com.abc.org</groupId>
        <artifactId>master-pom-multi-cloud</artifactId>
        <version>ver-1.0.0-SNAPSHOT</version>
        <relativePath>../deploy_mav/pom_multi_cloud.xml</relativePath>
    </parent>
    
       <modules>
        <module>app-ui-service-ingest</module>
        <module>shared-service-api</module>
        <module>shared-service-engine</module>
        <module>shared-service-plan</module>
        <module>shared-service-specs</module>        
    </modules>
</project>

In 2nd level child module shared-service-engine,

<project>
    <modelVersion>4.0.0</modelVersion>
    <groupId> com.abc.org</groupId>
    <artifactId>service-engine</artifactId>
    <version>ver-1.0.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>Service Engine</name>


    <parent>
        <groupId>com.abc.org.plat</groupId>
        <artifactId>apps-service-ser</artifactId>
        <version>ver-1.0.0-SNAPSHOT</version>
        <relativePath>../pom.xml</relativePath>
    </parent>
    
    ...
    
        <dependency>
            <groupId> com.abc.org</groupId>
            <artifactId>service-plan</artifactId>
            <classifier>${env-type}</classifier>
            <version>ver-1.0.0-SNAPSHOT</version>
        </dependency>
    ...
</project>

In 3rd level service-plan,

<project>
    <modelVersion>4.0.0</modelVersion>
    <groupId> com.abc.org</groupId>
    <artifactId>service-plan</artifactId>
    <version>ver-1.0.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>Service Plan</name>

    <parent>
        <groupId>com.abc.org.plat</groupId>
        <artifactId>apps-service-ser</artifactId>
        <version>ver-1.0.0-SNAPSHOT</version>
        <relativePath>../pom.xml</relativePath>
    </parent>
    
        <dependency>
            <groupId> com.abc.org</groupId>
            <artifactId>service-api</artifactId>
            <classifier>${env-type}</classifier>
            <version>ver-1.0.0-SNAPSHOT</version>
        </dependency>

</project>

Issue is the vaue for env-type ise received till 2nd level. 2nd level has dependency call to 3rd level to which the 2nd level has to pass the classifier value set by the active profile. The value is not passed to 3rd level and instead dependency searches for -${env-type}.jar

On debuging, getting logs like

[DEBUG]    com.abc.org:service-plan:jar:gcp:ver-1.0.0-SNAPSHOT:compile
[DEBUG]       org.jooq:jooq:jar:3.11.9:compile (version managed from 3.11.9)
[DEBUG]          javax.xml.bind:jaxb-api:jar:2.2.12:compile
[DEBUG]       com.abc.org:service-api:jar:${env-type}:ver-1.0.0-SNAPSHOT:compile

[ERROR] Failed to execute goal on project service-engine: Could not resolve dependencies for project com.abc.org:service-engine:jar:ver-1.0.0-SNAPSHOT: Could not find artifact com.abc.org:dashboard
-query-api:jar:${env-type}:ver-1.0.0-SNAPSHOT in nexus (https://repo.abc.xyz/nexus/content/groups/public) -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal on project service-engine: Could not resolve dependencies for project com.abc.org:service-engine:jar:ver-1.0.0-SNAPSHOT: Coul
d not find artifact com.abc.org:service-api:jar:${env-type}:ver-1.0.0-SNAPSHOT in nexus (https://repo.abc.xyz/nexus/content/groups/public)


Caused by: org.apache.maven.project.DependencyResolutionException: Could not resolve dependencies for project com.abc.org:service-engine:jar:ver-1.0.0-SNAPSHOT: Could not find artifact com.abc.org:dashboar
d-query-api:jar:${env-type}:ver-1.0.0-SNAPSHOT in nexus (https://repo.abc.xyz/nexus/content/groups/public)

CodePudding user response:

Solved it by creating a profile in the child project pom.xml and activating it with command line argument value.

mvn -DskipTests -X -P build_aws -Denv-type=aws clean install

<profiles>
    <profile>
        <id>build_local_aws</id>
        <properties>
            <test-cloud-type>aws</test-cloud-type>
            <cache-version>trunk-2.0.0-SNAPSHOT</cache-version>
        </properties>
        <activation>
            <property>
                <name>env-type</name>
                <value>aws</value>
            </property>
        </activation>
    </profile>
    <profile>
        <id>build_local_gcp</id>
        <properties>
            <test-cloud-type>gcp</test-cloud-type>
            <cache-version>trunk-2.0.0-SNAPSHOT</cache-version>
        </properties>
        <activation>
            <property>
                <name>env-type</name>
                <value>gcp</value>
            </property>
        </activation>
    </profile>
</profiles>
  • Related