title: Running main method from test class via maven
body: <p>I have the following in my pom.xml:</p>
  
  <pre><code>&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.2.1&lt;/version&gt;
            &lt;configuration&gt;
                &lt;mainClass&gt;com.myorg.MyClass&lt;/mainClass&gt;
            &lt;/configuration&gt;
        &lt;/plugin&gt;
     &lt;/plugins&gt;
  &lt;/build&gt;
  </code></pre>
  
  <p>The class <code>com.myorg.MyClass</code> is in my test source directory, and I am able to run it with the following command:</p>
  
  <pre><code>mvn -e exec:java -Dexec.classpathScope="test"
  </code></pre>
  
  <p>I want to:</p>
  
  <ol>
  <li>avoid having to use <code>-Dexec.classpathScope="test"</code>, but I cannot work out how configure that plugin to look in the test classpath.</li>
  <li>write more plugins for other classes (which will each have different configurations), but right now I can only run <code>exec:java</code>. Is there a way to label this plugin so that I call it via that label rather than just say "run whatever is in exec:java"?</li>
  <li>pull in a <code>-javaagent</code> property. I have this property defined in my pom.xml, which is being used by test cases. Will my "custom" plugin pick up these properties or do I need to do anything to pull them in?</li>
  </ol>
  
  <p>Here is the properties, which is defined directly under <code>&lt;project&gt;</code>.</p>
  
  <pre><code>&lt;properties&gt;
     &lt;spring.version&gt;3.2.6.RELEASE&lt;/spring.version&gt;
     &lt;atomikos.version&gt;3.9.2&lt;/atomikos.version&gt;
     &lt;loadTimeWeaverArgLine&gt;-javaagent:"${settings.localRepository}/org/springframework/spring-agent/2.5.6/spring-agent-2.5.6.jar"&lt;/loadTimeWeaverArgLine&gt;
  &lt;/properties&gt;
  </code></pre>
  
  <h1>Attempt with profile</h1>
  
  <p>Following on @Michal's suggestion (<a href="https://stackoverflow.com/a/30839824/257233">https://stackoverflow.com/a/30839824/257233</a>) this is what I have tried:</p>
  
  <pre><code>&lt;profile&gt;
     &lt;id&gt;run-importer&lt;/id&gt;
     &lt;properties&gt;
        &lt;loadTimeWeaverArgLine&gt;-javaagent:"${settings.localRepository}/org/springframework/spring-agent/2.5.6/spring-agent-2.5.6.jar"&lt;/loadTimeWeaverArgLine&gt;
     &lt;/properties&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.2.1&lt;/version&gt;
              &lt;configuration&gt;
                 &lt;executable&gt;java&lt;/executable&gt;
                    &lt;!--
                       None of these three options work.
                       &lt;commandlineArgs&gt;-javaagent:C:/Users/robbram/.m2/repository/org/springframework/spring-agent/2.5.6/spring-agent-2.5.6.jar&lt;/commandlineArgs&gt;
                       &lt;commandlineArgs&gt;${loadTimeWeaverArgLine}&lt;/commandlineArgs&gt;
                       &lt;commandlineArgs&gt;-javaagent:"${settings.localRepository}/org/springframework/spring-agent/2.5.6/spring-agent-2.5.6.jar"&lt;/commandlineArgs&gt;
                       &lt;argLine&gt;${loadTimeWeaverArgLine}&lt;/argLine&gt;
                    --&gt;
                 &lt;classpathScope&gt;test&lt;/classpathScope&gt;
                 &lt;mainClass&gt;com.myorg.MyClass&lt;/mainClass&gt;
              &lt;/configuration&gt;
           &lt;/plugin&gt;
        &lt;/plugins&gt;
     &lt;/build&gt;
  &lt;/profile&gt;
  </code></pre>
  
  <p>I run it with:</p>
  
  <pre><code>mvn -e exec:java -Prun-importer 
  </code></pre>
  
  <p>I am getting the following exception with either of the options:</p>
  
  <pre><code>[ERROR] Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.2.1:java (default-cli) on project TOLTAT-Model: An exception occured while executing the Java class. null: InvocationTargetException: Error creating bean with name 'loadTimeWeaver' defined in class org.springframework.context.annotation.LoadTimeWeavingConfiguration: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public org.springframework.instrument.classloading.LoadTimeWeaver org.springframework.context.annotation.LoadTimeWeavingConfiguration.loadTimeWeaver()] threw exception; nested exception is java.lang.IllegalStateException: ClassLoader [java.net.URLClassLoader] does NOT provide an 'addTransformer(ClassFileTransformer)' method. Specify a custom LoadTimeWeaver or start your Java virtual machine with Spring's agent: -javaagent:org.springframework.instrument.jar -&gt; [Help 1]
  org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.2.1:java (default-cli) on project TOLTAT-Model: An exception occured while executing the Java class. null
     at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:216)
     at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
     at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
  </code></pre>
  
  <p>I can confirm that the main class <code>com.myorg.MyClass</code> is being executed. I can see other output from it. I can also confirm that <code>loadTimeWeaverArgLine</code> works in other parts of this POM. It is used successfully for Integration tests: </p>
  
  <pre><code>&lt;profile&gt;
     &lt;id&gt;integration-tests&lt;/id&gt;
     &lt;build&gt;
        &lt;plugins&gt;
           &lt;!-- Integration tests require additional loadtime Spring argument --&gt;
           &lt;plugin&gt;
              &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
              &lt;artifactId&gt;maven-surefire-plugin&lt;/artifactId&gt;
              &lt;version&gt;2.16&lt;/version&gt;
              &lt;configuration&gt;
                 &lt;forkMode&gt;once&lt;/forkMode&gt;
                 &lt;argLine&gt; ${loadTimeWeaverArgLine}&lt;/argLine&gt;
                 &lt;skip&gt;false&lt;/skip&gt;
              &lt;/configuration&gt;
           &lt;/plugin&gt;
        &lt;/plugins&gt;
     &lt;/build&gt;
  &lt;/profile&gt;
  </code></pre>
  
  <h1>Attempt 2 with profile and mvn exec:exec</h1>
  
  <p>Upodate (Wednesday, 17th of June 2015, 12:11:17 PM): Following on <a href="https://stackoverflow.com/questions/30839272/running-main-method-from-test-class-via-maven#comment49779516_30839824">Michal's latest comment</a> I now have this working as I want:</p>
  
  <pre><code>&lt;profile&gt;
     &lt;id&gt;run-importer&lt;/id&gt;
     &lt;properties&gt;
        &lt;loadTimeWeaverArg&gt;-javaagent:"${settings.localRepository}/org/springframework/spring-agent/2.5.6/spring-agent-2.5.6.jar"&lt;/loadTimeWeaverArg&gt;
        &lt;log4JConfigArg&gt;-Dlog4j.configuration=file:${project.build.directory}/path/to/log4j.properties&lt;/log4JConfigArg&gt;
        &lt;mainClassArg&gt;com.myorg.MyClass&lt;/mainClassArg&gt;
        &lt;arg1&gt;foo&lt;/arg1&gt;
     &lt;/properties&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.2.1&lt;/version&gt;
              &lt;goals&gt;
                 &lt;goal&gt;exec&lt;/goal&gt;
              &lt;/goals&gt;
              &lt;configuration&gt;
                 &lt;executable&gt;java&lt;/executable&gt;
                 &lt;classpathScope&gt;test&lt;/classpathScope&gt;
                 &lt;arguments&gt;
                    &lt;argument&gt;${log4JConfigArg}&lt;/argument&gt;
                    &lt;argument&gt;${loadTimeWeaverArg}&lt;/argument&gt;
                    &lt;argument&gt;-classpath&lt;/argument&gt;
                    &lt;classpath /&gt;
                    &lt;argument&gt;${mainClassArg}&lt;/argument&gt;
                    &lt;argument&gt;${arg1}&lt;/argument&gt;
                 &lt;/arguments&gt;
              &lt;/configuration&gt;
           &lt;/plugin&gt;
        &lt;/plugins&gt;
     &lt;/build&gt;
  &lt;/profile&gt;
  </code></pre>
  
  <p>And I run it with:</p>
  
  <pre><code>mvn -e exec:exec -Prun-importer
  </code></pre>
  
  <p>The advantages of this approach:</p>
  
  <ul>
  <li>The whole purpose of this profile is to run "special code" that should never be deployed but needs to make use of code in src and test src.
  
  <ul>
  <li>I note Michal's suggestion that this deserves to be a module by itself. If this code base wasn't already so well established (large, old, complicated), I would seriously consider this.</li>
  </ul></li>
  <li>It leaves room in case this ever needs to be duplicated, so there is no "competition" regarding what gets run with <code>mvn -e exec:exec</code>.</li>
  <li>I can specify java agent, log4j and lots of other config using variables that already exist in the pom.</li>
  <li>I can override any of these arguments on the command line with <code>-Darg1="bar"</code></li>
  </ul>