简体   繁体   中英

maven picking wrong version of dependency with right profile

I am having a strange issue when doing a clean isntall using maven. I am using integration profile as below. mvn clean install -Pintegration,mainmodule -U -DskipTests=true

This will build mainmodule with profile integration. The mainmodule uses sco as its dependency. And SCO in repsository uses 3 profiles mainly development,distribution and integration. Each has dependency added with artifact id ws, but each has a different version. As I am running maven using profile integration, it should ideally pick up the artifact with version mentioned in integration. But strangely it picks up development version. I am not sure how it does that. How do I make sure that the version with the correct profile is being loaded?

I am adding snippets of POMs as below.

<groupId>a.b.c.d</groupId>
<artifactId>smc</artifactId>
<version>1.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<parent>
    <artifactId>dummy</artifactId>
    <groupId>a.b</groupId>
    <version>2-SNAPSHOT</version>
</parent>
<name>smc</name>
.
.
.
       <profile>
        <id>mainmodule</id>
        <modules>
            <module>../mainmodule</module>
        </modules>

    <parent>
    <groupId>a.b.c.d</groupId>
    <artifactId>smc</artifactId>
    <version>1.0.1-SNAPSHOT</version>
    <relativePath>../smc/pom.xml</relativePath>
</parent>
<artifactId>mainmodule</artifactId>
<packaging>war</packaging>
<name>mainmodule</name>

<dependencies>
    <dependency>
        <groupId>${project.groupId}</groupId>
        <artifactId>sco</artifactId>
        <version>3.0.0-SNAPSHOT</version>
        <type>jar</type>
    </dependency>
    --------------------------------------------

<parent>
    <groupId>a.b.c.d</groupId>
    <artifactId>smc</artifactId>
    <version>1.0.1-SNAPSHOT</version>
    <relativePath>../smc/pom.xml</relativePath>
</parent>
<artifactId>sco</artifactId>
<version>3.0.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>sco</name>

....

<profiles>
    <profile>
    <activation>
    <activeByDefault>true</activeByDefault>
    </activation>
        <id>development</id>
        <dependencies>
            <dependency>
                <groupId>a.b.spp</groupId>
                <artifactId>ws</artifactId>
                <version>2.1.1-SNAPSHOT</version>
            </dependency>
        </dependencies>
    </profile>
    <profile>
        <id>distribution</id>
        <dependencies>
            <dependency>
                <groupId>a.b.spp</groupId>
                <artifactId>ws</artifactId>
                <version>2.1.0</version>
            </dependency>
        </dependencies>
    </profile>
    <profile>
        <id>integration</id>
        <dependencies>
            <dependency>
                <groupId>a.b.spp</groupId>
                <artifactId>ws</artifactId>
                <version>2.1.0</version>
            </dependency>
        </dependencies>
    </profile>
</profiles>


Thanks for help.

You are trying to change the profiles of a dependency from a root module.

A working, better solution would be to move the profiles to the smc module with dependencyManagement sections with correct ws versions and just put the dependency to ws without version in the sco module.

add this to smc

<profiles>
    <profile>
    <activation>
    <activeByDefault>true</activeByDefault>
    </activation>
        <id>development</id>
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>a.b.spp</groupId>
                    <artifactId>ws</artifactId>
                    <version>2.1.1-SNAPSHOT</version>
                </dependency>
            </dependencies>
        </dependencyManagement>
    </profile>
    <profile>
        <id>distribution</id>
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>a.b.spp</groupId>
                    <artifactId>ws</artifactId>
                    <version>2.1.0</version>
                </dependency>
            </dependencies>
        </dependencyManagement>
    </profile>
    ...
</profiles>

Now just put the dependency in sco. (No profiles there)

<dependencies>
    <dependency>
        <groupId>a.b.spp</groupId>
        <artifactId>ws</artifactId>
    </dependency>
</dependencies>

Now run following following at smc and see the proper versions at work

mvn dependency:tree 
mvn dependency:tree -Pdistribution

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM