title: How do I compile a JUnit test with Maven that uses the Java Compiler API?
body: <p>Running the Java Compiler from a JUnit test is pretty simple using the <a href="http://download.oracle.com/javase/6/docs/api/javax/tools/JavaCompiler.html" rel="nofollow">Java Compiler API</a>. The test checks that I can load new code using a temporary classloader and that the cleanup works, etc</p>
  
  <p>My problem is compiling and/or running the test using <a href="http://maven.apache.org/" rel="nofollow">Maven</a>.</p>
  
  <p>If I add the java compiler using a dependency, I must set the scope to <code>test</code>. If I do that, the <code>tools.jar</code> will be added to the classpath when the tests are being compiled which means that there will be <strong>two</strong> Java compilers on the classpath.</p>
  
  <p>The same is true for the scope <code>runtime</code> but that will also add the Java compiler to my artifact, making it leak. All other scopes are even worse.</p>
  
  <p>How can I solve this?</p>