title: How does maven decide when to use the target folder for classpath
body: <p>I have a question regarding how maven calculates the classpath during building. Specifically, what controls when the "target/classes" is used and when the "jar" from a repository (local/remote) is used.</p>
  
  <p>I have a project on version 1.0.0-SNAPSHOT where the artifacts have NOT been installed/deployed so there is no "jar" in some repository (remote or local) to resolve them. I want to run "generate-sources" WITHOUT installing locally (no '<code>mvn install</code>' run).</p>
  
  <p>The structure looks like this:</p>
  
  <pre><code>parent-prj
  parent-prj/sub-prj
  parent-prj/gen-src-prj &lt;--- This depends on 'sub-prj'
  </code></pre>
  
  <p>When I run "<code>mvn -am -pl parent-prj/gen-src-prj generate-sources</code>" in order to just generate some java files, it does not work:</p>
  
  <pre><code>[ERROR] Failed to execute goal on project gen-src-prj: Could
   not resolve dependencies for project 
  mygrp:gen-src-prj:jar:1.0.0-SNAPSHOT: 
  Could not find artifact 
  mygrp:sub-prj:jar:1.0.0-SNAPSHOT -&gt; [Help 1]
  </code></pre>
  
  <p>Using debug output and adding "dependency:build-classpath" I can confirm that maven ignores the presence of "sub-prj" in the reactor and looks for a "jar" somewhere which it can't find. Yet the project is printed in the reactor summary:</p>
  
  <pre><code>[INFO] Reactor Summary:
  [INFO] 
  [INFO] parent-prj ..................................... SUCCESS [  0.625 s]
  [INFO] sub-prj ........................................ SUCCESS [  0.018 s]
  [INFO] gen-src-prj .................................... FAILURE [  0.040 s]
  </code></pre>
  
  <p>The interesting thing I noticed is that running the compile goal works fine! This uses <code>sub-prj/target/classes</code> (as shown by dependency:build-classpath) and has no trouble generating the sources and even compiling them: "<code>mvn -am -pl parent-prj/gen-src-prj compile</code>"</p>
  
  <p>So here are the points I want to understand:</p>
  
  <ol>
  <li>Why does the compile goal work but the generate-sources doesn't work?</li>
  <li>At what point does maven decide to use the output folder of previous projects on the reactor classpath instead of looking for a jar?</li>
  <li>Is there a way for generate-sources to run directly as I want it EVEN WITHOUT having its dependencies resolved?</li>
  </ol>
  
  <p>Regarding (3) my generation tool is a utility invoked by:</p>
  
  <pre><code>    &lt;plugin&gt;
          &lt;groupId&gt;org.codehaus.mojo&lt;/groupId&gt;
          &lt;artifactId&gt;exec-maven-plugin&lt;/artifactId&gt;
          &lt;version&gt;1.6.0&lt;/version&gt;
          &lt;executions&gt;
              &lt;execution&gt;
                  &lt;phase&gt;generate-sources&lt;/phase&gt;
                  &lt;goals&gt;
                      &lt;goal&gt;java&lt;/goal&gt;
                  &lt;/goals&gt;
              &lt;/execution&gt;
          &lt;/executions&gt;
  </code></pre>
  
  <p>The tool reads some XML in src/main/resources and generates Java files and does NOT need anything in its class-path (so there is no need for maven to resolve it).</p>
  
  <p>Also note that I would be interested to understand (1) and (2) even if a solution for (3) is provided.</p>
  
  <hr>
  
  <p>EDIT: Per comment request, adding full example</p>
  
  <p>parent-prj/pom.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &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;mygrp&lt;/groupId&gt;
      &lt;artifactId&gt;parent-prj&lt;/artifactId&gt;
      &lt;version&gt;1.0.0-SNAPSHOT&lt;/version&gt;
  
      &lt;packaging&gt;pom&lt;/packaging&gt;
  
      &lt;modules&gt;
          &lt;module&gt;sub-prj&lt;/module&gt;
      &lt;module&gt;gen-src-prj&lt;/module&gt;
      &lt;/modules&gt;
  
      &lt;build&gt;
          &lt;pluginManagement&gt;
              &lt;plugins&gt;
                  &lt;plugin&gt;
                      &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
                      &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
                      &lt;version&gt;3.3&lt;/version&gt;
                      &lt;configuration&gt;
                          &lt;source&gt;1.8&lt;/source&gt;
                          &lt;target&gt;1.9&lt;/target&gt;
                          &lt;encoding&gt;UTF-8&lt;/encoding&gt;
                      &lt;/configuration&gt;
                  &lt;/plugin&gt;
              &lt;/plugins&gt;
          &lt;/pluginManagement&gt;
      &lt;/build&gt;
  &lt;/project&gt;
  </code></pre>
  
  <p>parent-prj/sub-prj/pom.xml</p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
  &lt;project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
      xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&gt;
      &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
  
      &lt;parent&gt;
          &lt;groupId&gt;mygrp&lt;/groupId&gt;
          &lt;artifactId&gt;parent-prj&lt;/artifactId&gt;
          &lt;version&gt;1.0.0-SNAPSHOT&lt;/version&gt;
      &lt;/parent&gt;
  
      &lt;artifactId&gt;sub-prj&lt;/artifactId&gt;
  &lt;/project&gt;
  </code></pre>
  
  <p>parent-prj/gen-src-prj/pom.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &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;parent&gt;
          &lt;groupId&gt;mygrp&lt;/groupId&gt;
          &lt;artifactId&gt;parent-prj&lt;/artifactId&gt;
          &lt;version&gt;1.0.0-SNAPSHOT&lt;/version&gt;
      &lt;/parent&gt;
  
      &lt;artifactId&gt;gen-src-prj&lt;/artifactId&gt;
  
      &lt;dependencies&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;mygrp&lt;/groupId&gt;
              &lt;artifactId&gt;sub-prj&lt;/artifactId&gt;
              &lt;version&gt;${project.version}&lt;/version&gt;
          &lt;/dependency&gt;
      &lt;/dependencies&gt;
  
      &lt;build&gt;
          &lt;plugins&gt;
              &lt;plugin&gt;
                  &lt;groupId&gt;org.codehaus.mojo&lt;/groupId&gt;
                  &lt;artifactId&gt;exec-maven-plugin&lt;/artifactId&gt;
                  &lt;version&gt;1.6.0&lt;/version&gt;
                  &lt;executions&gt;
                      &lt;execution&gt;
                          &lt;phase&gt;generate-sources&lt;/phase&gt;
                          &lt;goals&gt;
                              &lt;goal&gt;java&lt;/goal&gt;
                          &lt;/goals&gt;
                      &lt;/execution&gt;
                  &lt;/executions&gt;
                  &lt;configuration&gt;
                      &lt;includeProjectDependencies&gt;false&lt;/includeProjectDependencies&gt;
                      &lt;includePluginDependencies&gt;true&lt;/includePluginDependencies&gt;
                      &lt;mainClass&gt;uk.co.real_logic.sbe.SbeTool&lt;/mainClass&gt;
                      &lt;systemProperties&gt;
                          &lt;systemProperty&gt;
                              &lt;key&gt;sbe.output.dir&lt;/key&gt;
                              &lt;value&gt;${project.build.directory}/generated-sources/java&lt;/value&gt;
                          &lt;/systemProperty&gt;
                          &lt;systemProperty&gt;
                              &lt;key&gt;sbe.validation.warnings.fatal&lt;/key&gt;
                              &lt;value&gt;true&lt;/value&gt;
                          &lt;/systemProperty&gt;
                      &lt;/systemProperties&gt;
                      &lt;arguments&gt;
                          &lt;argument&gt;${project.build.resources[0].directory}/Examples.xml&lt;/argument&gt;
                      &lt;/arguments&gt;
                      &lt;workingDirectory&gt;${project.build.directory}/generated-sources/java&lt;/workingDirectory&gt;
                  &lt;/configuration&gt;
                  &lt;dependencies&gt;
                      &lt;dependency&gt;
                          &lt;groupId&gt;uk.co.real-logic&lt;/groupId&gt;
                          &lt;artifactId&gt;sbe-tool&lt;/artifactId&gt;
                          &lt;version&gt;1.7.10&lt;/version&gt;
                      &lt;/dependency&gt;
                  &lt;/dependencies&gt;
              &lt;/plugin&gt;
  
              &lt;plugin&gt;
                  &lt;groupId&gt;org.codehaus.mojo&lt;/groupId&gt;
                  &lt;artifactId&gt;build-helper-maven-plugin&lt;/artifactId&gt;
                  &lt;version&gt;3.0.0&lt;/version&gt;
                  &lt;executions&gt;
                      &lt;execution&gt;
                          &lt;id&gt;add-source&lt;/id&gt;
                          &lt;phase&gt;generate-sources&lt;/phase&gt;
                          &lt;goals&gt;
                              &lt;goal&gt;add-source&lt;/goal&gt;
                          &lt;/goals&gt;
                          &lt;configuration&gt;
                              &lt;sources&gt;
                                  &lt;source&gt;target/generated-sources/java/&lt;/source&gt;
                              &lt;/sources&gt;
                          &lt;/configuration&gt;
                      &lt;/execution&gt;
                  &lt;/executions&gt;
              &lt;/plugin&gt;
          &lt;/plugins&gt;
      &lt;/build&gt;
  &lt;/project&gt;
  </code></pre>
  
  <hr>
  
  <p>EDIT: Armed with the knowledge from the answers I have come up with this workaround that allows one to achieve the desired behaviour. I list the dependencies in a profile that is active by default, then use another profile to run generate-sources with no dependencies active, like follows:</p>
  
  <p>parent-prj/gen-src-prj/pom.xml</p>
  
  <pre><code>&lt;profiles&gt;
      &lt;profile&gt;
          &lt;id&gt;default&lt;/id&gt;
          &lt;activation&gt;
              &lt;activeByDefault&gt;true&lt;/activeByDefault&gt;
          &lt;/activation&gt;
          &lt;dependencies&gt;
              &lt;dependency&gt;
                  &lt;groupId&gt;mygrp&lt;/groupId&gt;
                  &lt;artifactId&gt;sub-prj&lt;/artifactId&gt;
                  &lt;version&gt;${project.version}&lt;/version&gt;
              &lt;/dependency&gt;
          &lt;/dependencies&gt;
      &lt;/profile&gt;
  
      &lt;profile&gt;
          &lt;id&gt;excludeDependency&lt;/id&gt;
          &lt;dependencies&gt;
          &lt;/dependencies&gt;
      &lt;/profile&gt;
  &lt;/profiles&gt;
  </code></pre>
  
  <p>To generate sources with above, use: <code>mvn -PexcludeDependency generate-sources</code></p>