title: Maven reactor builds in order that doesn't satisfy all dependencies
body: <p>I am having trouble with build ordering in a SNAPSHOT build using Maven.  The reactor is building all of the jars in an invalid order.</p>
  
  <p>The following is an example structure to recreate the issue (the project I'm having issues with is much larger, >100 jars):</p>
  
  <pre><code>pom.xml        [reactor]
  parent/pom.xml [parent]
  jar1/pom.xml
  jar2/pom.xml
  jar3/pom.xml
  </code></pre>
  
  <p><strong>pom.xml [reactor]</strong></p>
  
  <pre><code>&lt;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"&gt;
    &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
    &lt;packaging&gt;pom&lt;/packaging&gt;
    &lt;groupId&gt;com.test.buildorder&lt;/groupId&gt;
    &lt;artifactId&gt;reactor&lt;/artifactId&gt;
    &lt;version&gt;1.0&lt;/version&gt;
    &lt;modules&gt;
      &lt;module&gt;jar3&lt;/module&gt;
      &lt;module&gt;jar2&lt;/module&gt;
      &lt;module&gt;jar1&lt;/module&gt;
    &lt;/modules&gt;
  &lt;/project&gt;
  </code></pre>
  
  <p><strong>parent/pom.xml</strong></p>
  
  <pre><code>&lt;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"&gt;
    &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
    &lt;groupId&gt;com.test.buildorder&lt;/groupId&gt;
    &lt;artifactId&gt;parent&lt;/artifactId&gt;
    &lt;version&gt;1.1&lt;/version&gt;
    &lt;packaging&gt;pom&lt;/packaging&gt;
  
    &lt;dependencyManagement&gt;
    &lt;dependencies&gt;
        &lt;dependency&gt;
          &lt;groupId&gt;com.test.buildorder&lt;/groupId&gt;
          &lt;artifactId&gt;jar1&lt;/artifactId&gt;
          &lt;version&gt;1.1&lt;/version&gt;
        &lt;/dependency&gt;
        &lt;dependency&gt;
          &lt;groupId&gt;com.test.buildorder&lt;/groupId&gt;
          &lt;artifactId&gt;jar2&lt;/artifactId&gt;
          &lt;version&gt;1.0&lt;/version&gt;
        &lt;/dependency&gt;
        &lt;dependency&gt;
          &lt;groupId&gt;com.test.buildorder&lt;/groupId&gt;
          &lt;artifactId&gt;jar3&lt;/artifactId&gt;
          &lt;version&gt;1.1&lt;/version&gt;
        &lt;/dependency&gt;
      &lt;/dependencies&gt;
    &lt;/dependencyManagement&gt;
  &lt;/project&gt;
  </code></pre>
  
  <p><strong>jar1/pom.xml</strong></p>
  
  <pre><code>&lt;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"&gt;
    &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
    &lt;groupId&gt;com.test.buildorder&lt;/groupId&gt;
    &lt;artifactId&gt;jar1&lt;/artifactId&gt;
    &lt;version&gt;1.1&lt;/version&gt;
    &lt;parent&gt;
      &lt;groupId&gt;com.test.buildorder&lt;/groupId&gt;
      &lt;artifactId&gt;parent&lt;/artifactId&gt;
      &lt;version&gt;1.1&lt;/version&gt;
    &lt;/parent&gt;
  &lt;/project&gt;
  </code></pre>
  
  <p><strong>jar2/pom.xml</strong></p>
  
  <pre><code>&lt;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"&gt;
    &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
    &lt;groupId&gt;com.test.buildorder&lt;/groupId&gt;
    &lt;artifactId&gt;jar2&lt;/artifactId&gt;
    &lt;version&gt;1.1&lt;/version&gt;
    &lt;parent&gt;
      &lt;groupId&gt;com.test.buildorder&lt;/groupId&gt;
      &lt;artifactId&gt;parent&lt;/artifactId&gt;
      &lt;version&gt;1.0&lt;/version&gt;
    &lt;/parent&gt;
  
    &lt;dependencies&gt;
      &lt;dependency&gt;
        &lt;groupId&gt;com.test.buildorder&lt;/groupId&gt;
        &lt;artifactId&gt;jar1&lt;/artifactId&gt;
      &lt;/dependency&gt;
    &lt;/dependencies&gt;
  &lt;/project&gt;
  </code></pre>
  
  <p><strong>jar3/pom.xml</strong></p>
  
  <pre><code>&lt;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"&gt;
    &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
    &lt;groupId&gt;com.test.buildorder&lt;/groupId&gt;
    &lt;artifactId&gt;jar3&lt;/artifactId&gt;
    &lt;version&gt;1.1&lt;/version&gt;
    &lt;parent&gt;
      &lt;groupId&gt;com.test.buildorder&lt;/groupId&gt;
      &lt;artifactId&gt;parent&lt;/artifactId&gt;
      &lt;version&gt;1.1&lt;/version&gt;
    &lt;/parent&gt;
  
    &lt;dependencies&gt;
      &lt;dependency&gt;
        &lt;groupId&gt;com.test.buildorder&lt;/groupId&gt;
        &lt;artifactId&gt;jar2&lt;/artifactId&gt;
      &lt;/dependency&gt;
    &lt;/dependencies&gt;
  &lt;/project&gt;
  </code></pre>
  
  <p>My repository (local in this case) only has the released 1.0 versions of each jar and jar3-1.0 depended on jar2-1.0, and jar2-1.0 depended on jar1-1.0 and each had parent-1.0 (I can post this code if needed, but my question is already really long so I'll hold off unless asked).  </p>
  
  <p>I now install the 1.1 parent pom above, and then attempt to run an install on the reactor and get the following result.  </p>
  
  <pre><code>[INFO] Scanning for projects...
  [WARNING]
  [WARNING] Some problems were encountered while building the effective model for com.test.buildorder:jar3:jar:1.1
  [WARNING] 'parent.relativePath' points at com.test.buildorder:reactor instead of com.test.buildorder:parent, please verify your project structure @ line 6, column 11
  [WARNING]
  [WARNING] Some problems were encountered while building the effective model for com.test.buildorder:jar2:jar:1.1
  [WARNING] 'parent.relativePath' points at com.test.buildorder:reactor instead of com.test.buildorder:parent, please verify your project structure @ line 6, column 11
  [WARNING]
  [WARNING] Some problems were encountered while building the effective model for com.test.buildorder:jar1:jar:1.1
  [WARNING] 'parent.relativePath' points at com.test.buildorder:reactor instead of com.test.buildorder:parent, please verify your project structure @ line 6, column 11
  [WARNING]
  [WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
  [WARNING]
  [WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
  [WARNING]
  [INFO] ------------------------------------------------------------------------
  [INFO] Reactor Build Order:
  [INFO]
  [INFO] jar3
  [INFO] jar2
  [INFO] jar1
  [INFO] reactor
  [INFO]
  [INFO] ------------------------------------------------------------------------
  [INFO] Building jar3 1.1
  [INFO] ------------------------------------------------------------------------
  Downloading: https://repo.maven.apache.org/maven2/com/test/buildorder/jar1/1.1/jar1-1.1.jar
  [INFO] ------------------------------------------------------------------------
  [INFO] Reactor Summary:
  [INFO]
  [INFO] jar3 ............................................... FAILURE [  0.645 s]
  [INFO] jar2 ............................................... SKIPPED
  [INFO] jar1 ............................................... SKIPPED
  [INFO] reactor ............................................ SKIPPED
  [INFO] ------------------------------------------------------------------------
  [INFO] BUILD FAILURE
  [INFO] ------------------------------------------------------------------------
  [INFO] Total time: 0.775 s
  [INFO] Finished at: 2015-06-03T16:56:04-05:00
  [INFO] Final Memory: 4M/15M
  [INFO] ------------------------------------------------------------------------
  [ERROR] Failed to execute goal on project jar3: Could not resolve dependencies for project com.test.buildorder:jar3:jar:1.1: Could not find artifact com.test.buildorder:jar1:jar:1.1 in central (https://repo.maven.apache.org/maven2) -&gt; [Help 1]
  [ERROR]
  [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
  [ERROR] Re-run Maven using the -X switch to enable full debug logging.
  [ERROR]
  [ERROR] For more information about the errors and possible solutions, please read the following articles:
  [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/DependencyResolutionException
  </code></pre>
  
  <p>When I built the 1.0 versions the Reactor was smart enough to realize the dependency tree jar3-1.0 -> jar2-1.0 -> jar1-1.0 and build in the order jar1, jar2, jar3 to satisfy the dependencies.  </p>
  
  <p>Now though because jar3-1.1 depends on jar2-1.0 (which is in the repo) the reactor does not assume that jar1 needs to be built first but the debug (using the -X argument) shows the dependency tree to be:</p>
  
  <pre><code>[DEBUG] com.test.buildorder:jar3:jar:1.1
  [DEBUG]    com.test.buildorder:jar2:jar:1.0:compile
  [DEBUG]       com.test.buildorder:jar1:jar:1.1:compile (version managed from 1.0 by com.test.buildorder:parent:1.1)
  </code></pre>
  
  <p>So I have a few questions about this scenario:<br>
  1. Why does maven think jar3-1.1 has a transitive dependency to jar1-1.1?<br>
  2. Why does the reactor not realize this transitive dependency and build in the required order to satisfy it?<br>
  3. What can I do to prevent this issue from occurring? (Preferrably without rewriting over 100 poms.)</p>
  
  <p><strong>Edit: found answer to question one, and one solution to question three.  But still would like to hear about question two.</strong></p>
  
  <p><em>1</em>. Because I never defined a scope to the dependencies maven determines assigned a default scope of <code>compile</code>.  Because of this the dependency from jar2 to jar1 will appear as a transitive dependency for jars that depend on jar2 (such as jar3).  <code>(version managed from 1.0 by com.test.buildorder:parent:1.1)</code> informs me that the dependency is from jar2-1.0 to jar1-1.0 but because the parent specifies another version for jar 1 (1.1) it takes that.  </p>
  
  <p><em>3</em>. If I change the scope of the dependency from jar2 to jar1 to <code>provided</code> the dependency no longer becomes transitive.  <em>This assumes that at runtime the required jars will all be present, so this may not be the solution for everybody who comes across this issue.</em>  <a href="https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html#Dependency_Scope" rel="noreferrer">Here is a guide to the scopes available in Maven</a></p>