title: How do I include a dependency's test jar into a Maven project's deployment?
body: <p>I have two projects, <code>foo</code> and <code>foo-web</code> under the <code>com.example</code> group. <code>foo-web</code> depends on <code>foo</code>.</p>
  
  <p>To be able to develop the UI part of the application without depending on external services,  dummy DAOs were implemented in <code>foo</code> (they return static data so we don't have to connect to databases etc). </p>
  
  <p>We were required to move the dummy classes to <code>src/test/java</code>. This means that they don't get deployed with <code>foo.jar</code> to the war built from the web project. I found <strong><a href="http://maven.apache.org/guides/mini/guide-attached-tests.html" rel="noreferrer">these instructions</a></strong> on the maven site, but they don't seem to work for me.</p>
  
  <p>In <code>foo</code>'s <code>pom.xml</code> I have:</p>
  
  <pre><code>        &lt;plugin&gt;
            &lt;artifactId&gt;maven-jar-plugin&lt;/artifactId&gt;
            &lt;executions&gt;
              &lt;execution&gt;
                &lt;id&gt;test-jar&lt;/id&gt;
                &lt;phase&gt;test-compile&lt;/phase&gt;
                &lt;goals&gt;
                  &lt;goal&gt;test-jar&lt;/goal&gt;
                &lt;/goals&gt;
              &lt;/execution&gt;
            &lt;/executions&gt;
          &lt;/plugin&gt;
  </code></pre>
  
  <p>When running <code>mvn install</code> on <code>foo-web</code>, in the target of <code>foo</code> I'd get two jars: <code>foo-1.0.0-SNAPSHOT.jar</code> and <code>foo-1.0.0-SNAPSHOT-tests.jar</code>. They both get installed fine in the local maven repository.</p>
  
  <p>Before, the <code>foo-web</code> dependency looked like this:</p>
  
  <pre><code>&lt;dependency&gt;
      &lt;groupId&gt;com.example&lt;/groupId&gt;
      &lt;artifactId&gt;foo&lt;/artifactId&gt;
      &lt;version&gt;1.0.0-SNAPSHOT&lt;/version&gt;
  &lt;/dependency&gt;
  </code></pre>
  
  <p>And that would trigger the deployment of <code>foo-1.0.0-SNAPSHOT.jar</code> in the war. Now, I want to also deploy the <code>-tests</code> jar, preferably only for a "local" profile.</p>
  
  <p>I tried in various ways to do this:</p>
  
  <pre><code>&lt;profile&gt;
      &lt;id&gt;local&lt;/id&gt;
      &lt;dependencies&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;com.example&lt;/groupId&gt;
              &lt;artifactId&gt;foo&lt;/artifactId&gt;
              &lt;version&gt;1.0.0-SNAPSHOT&lt;/version&gt;
              &lt;type&gt;test-jar&lt;/type&gt;
          &lt;/dependency&gt;
      &lt;/dependencies&gt;
  &lt;/profile&gt;
  </code></pre>
  
  <p>This causes the source jar to be deployed with a different name: <code>com.example-foo.jar</code> and does not deploy the test jar. I also tried using <code>&lt;classifier&gt;</code> instead of <code>&lt;type&gt;</code> in the dependency, but it still does the same. I tried using the above dependency outside of the profile (alongside the others), but it still behaves the same.</p>
  
  <p>If I add the <code>&lt;type&gt;</code> to the main dependency (without adding the other dependency) I get the test jar deployed (with the same name as above), but the source, naturally, does not get deployed.</p>
  
  <p>The only difference from what's written in the documentation is the fact that the scope is not specified for the test dependency. Does it only work for the <code>test</code> scope? Can I somehow deploy the test classes differently.</p>
  
  <p>I know the question's a bit convoluted, please let me know if there's something I can clarify.</p>
  
  <p>Thanks!</p>
  
  <hr>
  
  <h2>Update:</h2>
  
  <p>I tried it in several more ways but it still won't work.</p>
  
  <p>I added another execution to the maven-jar-plugin in the <code>foo</code> project (the dependency, not the main web project) in which I hoped to force maven to compile the test classes in the same jar as the main ones and reference the big bundle by a different classifier. I couldn't get it to work:</p>
  
  <pre><code>&lt;execution&gt;
    &lt;id&gt;local-build&lt;/id&gt;
    &lt;phase&gt;package&lt;/phase&gt;
    &lt;goals&gt;
      &lt;goal&gt;jar&lt;/goal&gt;
    &lt;/goals&gt;
    &lt;configuration&gt;
      &lt;classifier&gt;batman&lt;/classifier&gt;
      &lt;directory&gt;${basedir}/src/test/java&lt;/directory&gt; &lt;!-- tried several variations here --&gt;
      &lt;includes&gt;
          &lt;include&gt;**&lt;/include&gt;
      &lt;/includes&gt;
    &lt;/configuration&gt;
  &lt;/execution&gt;
  </code></pre>
  
  <p>The jar was generated with the <code>batman</code> classifier, but I couldn't find any way to get it to include test classes in the <code>jar</code> goal.</p>
  
  <p>Doing this, I realized that this does not depend on the <code>test-jar</code> type/<code>tests</code> classifier/<code>test</code> scope relationship. When I tried to specify the new jar I'm building besides the main one, I got the same behavior as when trying to include the <code>-tests</code> jar. I checked the local maven repository and both all jars from the dependent project are getting installed fine, so the problem is the main project's dependency resolution.</p>
  
  <h2>tl;dr</h2>
  
  <p>Everything boils down to the question if you can include the same dependency with multiple classifiers. From what I saw until now, the answer is no - I always get the <code>com.example-foo</code> jar when specifying the same dependency multiple times with different classifiers.</p>