8 Replies Latest reply on Dec 20, 2010 6:22 PM by Ondrej Zizka

    Maven repos order when using profiles

    Ondrej Zizka Master

      Hi,

       

      I have some issues with setting Maven repos order correctly.

       

      I have settings.xml (see below) with two profiles which set some repos.
      Then if I run    `mvn -s ../../../settings.xml help:effective-pom -Pmavenhoe-repo,jboss-repos` ,

      I would expect the repos to be in the order respective to the order of profiles they are defined in.
      However, they are merged into this order:

       

      <repositories>
           <repository>
             <id>jboss</id>
              <url>https://repository.jboss.org/nexus/content/groups/public</url>
           </repository>
           <repository>
             <id>mavenhoe</id>
             <url>http://localhost:17283/jars?mvnPath=</url>
           </repository>
           <repository>
             <id>central</id>
             <name>Maven Repository Switchboard</name>
             <url>http://repo1.maven.org/maven2</url>
           </repository>
       </repositories>
      

       

      (Setting both <activeByDefault>'s to true leads to the same order.)


      How does Maven figure out this order?
      Could someone please hint how to get the mavenhoe repo at the very top?

       


      Thanks,
      Ondra

       

       

       

       

       
        <profiles>
           <!-- Mavenhoe fake repository -->
           <profile>
             <id>mavenhoe-repo</id>
              <activation><activeByDefault>false</activeByDefault></activation>
             <repositories>
                <repository> <id>mavenhoe</id>  <url>http://localhost:17283/jars?mvnPath=</url>  </repository>
             </repositories>
           </profile>
      
          <!-- JBoss repos -->
           <profile>
             <id>jboss-repos</id>
              <activation><activeByDefault>false</activeByDefault></activation>
             <!-- JBoss release and snapshot repos -->
             <repositories>
               <repository>
                 <id>jboss</id>
                  <url>https://repository.jboss.org/nexus/content/groups/public</url>
                 <releases>  <enabled>true</enabled>  <updatePolicy>never</updatePolicy> </releases>
                 <snapshots> <enabled>true</enabled>  <updatePolicy>never</updatePolicy> </snapshots>
               </repository>
             </repositories>
      
             <!-- JBoss release and snapshot repos *for plugins* -->
             <pluginRepositories>
               <pluginRepository>
                 <id>jboss-plugins</id>
                  <url>https://repository.jboss.org/nexus/content/groups/public</url>
                 <releases>  <enabled>true</enabled>  <updatePolicy>never</updatePolicy> </releases>
                 <snapshots> <enabled>true</enabled>  <updatePolicy>never</updatePolicy> </snapshots>
               </pluginRepository>
             </pluginRepositories>
           </profile>
      
        </profiles>
      
        • 1. Re: Maven repos order when using profiles
          Paul Gier Master

          I wasn't sure what the ordering was, so I did some quick testing.  The order seems to be pretty consistently (tested Maven 3.0.1 and 2.2.1) the reverse order of the profiles in settings.xml.  This matches what you are seeing.  The full ordering also puts settings.xml repos ahead of repos in the pom.

          1. settings.xml - reverse profile order

          2. pom.xml repos

          3. parent pom repos

          I haven't seen this clearly documented anywhere, so I will file an upstream issue.  Also, this describes the output of the effective pom, but I'm not certain that the effective pom always matches the actual resolution order.  There has been some work to control the repo order ( for example, http://jira.codehaus.org/browse/MNG-4400), so it's likely that the order is deterministic, but I think the main issue is that it just needs to be documented.

          • 2. Re: Maven repos order when using profiles
            John Casey Newbie

            I agree that the docs need to be clearer on this point, but I can probably explain what's going on here.

             

            Think of profiles as a sort of inverted inheritance mechanism, at least when it comes to the append/prepend behavior you normally see in Maven. Normally, new things are appended to lists when they are inherited. If my parent POM declares repository FOO and I declare repository BAR, then I would expect BAR to be the first one listed and FOO the second. This is how the central repository winds up at the bottom of the list.

             

            In profiles, changes are prepended. This means that if you have repository FOO and then activate profile X, which declares repository BAR, and Y, which declares BAZ, then the order will be BAZ, BAR, FOO (profile X is applied first, and prepends to FOO to give BAR, FOO...then, Y is applied second, and prepends to BAR, FOO, rendering BAZ, BAR, FOO).

             

            It's confusing, largely because of how similar yet different profiles are to normal inheritance.

            1 of 1 people found this helpful
            • 3. Re: Maven repos order when using profiles
              Paul Gier Master

              Upstream issue for improving the documentation: http://jira.codehaus.org/browse/MNG-4940

              • 4. Re: Maven repos order when using profiles
                Ondrej Zizka Master

                Hmm, John, that makes perfect sense. Thanks both.

                 

                Edit:

                 

                However, it would be useful if one could modify the order by permuting the order of activated profiles.

                Which means,  mvn -Pabc,def,ghi  would give different repo order than  mvn -Pghi,abc,def .

                 

                Does Maven documentation say something about the effect of profile ordering?

                Since this is not yet documented, this could be a good time to enable such feature before it gets carved to the stone.

                 

                WDYT?

                • 5. Re: Maven repos order when using profiles
                  Paul Gier Master

                  The order of the profiles activated on the command line doesn't have any effect on their order of activation, although I agree with you that it could be useful.  You're right this could also be better documented on the Maven site.

                  • 6. Re: Maven repos order when using profiles
                    John Casey Newbie

                    Sorry, I misspoke. I meant the order of declaration, not the order of reference. Too many variables!

                    • 7. Re: Maven repos order when using profiles
                      Ondrej Zizka Master

                      "Let the order of profiles in `mvn -P...` determine their order in effective POM"

                      http://jira.codehaus.org/browse/MNG-4946

                      • 8. Re: Maven repos order when using profiles
                        Ondrej Zizka Master

                        Seems like the auto-activated profiles have even higher priority than those activated via command line. I had this settings.xml:

                         

                            <profile>
                              <id>jboss-repos</id>
                              <activation><activeByDefault>false</activeByDefault></activation>
                              <repositories>
                                <repository>
                                  <id>jboss</id>
                                  <url>https://repository.jboss.org/nexus/content/groups/public</url>
                        ... 
                        
                            <profile>
                              <id>brno-repo</id>
                              <activation><activeByDefault>true</activeByDefault></activation>
                              <repositories>
                                <repository>
                                  <id>brno-proxy</id>
                                  <name>Brno proxy</name>
                                  <url>http://jawa18.englab.brq.redhat.com:1212/nexus/content/groups/public-all</url> 
                        ...
                            <profile>
                              <id>mavenhoe-repo</id>
                              <activation><activeByDefault>false</activeByDefault></activation>
                              <repositories>
                                 <repository> <id>mavenhoe</id> <url>http://localhost:17283/jars?mvnPath=</url> </repository>
                        

                         

                        And running `mvn -s ../../../settings-hbn.xml help:effective-pom -Pmavenhoe-repo,jboss-repos` gives this:

                         

                          <repositories>
                            <repository>
                              <id>brno-proxy</id>
                              <name>Brno proxy</name>
                              <url>http://jawa18.englab.brq.redhat.com:1212/nexus/content/groups/public-all</url>
                            </repository>
                            <repository>
                              <id>mavenhoe</id>
                              <url>http://localhost:17283/jars?mvnPath=</url>
                            </repository>
                            <repository>
                              <releases>
                                <updatePolicy>never</updatePolicy>
                              </releases>
                              <snapshots>
                                <updatePolicy>never</updatePolicy>
                              </snapshots>
                              <id>jboss</id>
                              <url>https://repository.jboss.org/nexus/content/groups/public</url>
                            </repository>
                            <repository>
                              <snapshots>
                                <enabled>false</enabled>
                              </snapshots>
                              <id>central</id>
                              <name>Maven Repository Switchboard</name>
                              <url>http://repo1.maven.org/maven2</url>
                            </repository>
                          </repositories>
                        

                         

                         

                        So it's:

                         

                        1. settings.xml - Auto-activated profiles (<activation/>), reverse definition order
                        2. settings.xml - CLI activated profiles, reverse definition order
                        3. pom.xml repos
                        4. parent pom repos

                         

                        And adding auto-activated profile to -P does not change the result.