Executing the main method from the test class via maven

I have the following in my pom.xml:

<build>
   ...
   <plugins>
   ...
      <plugin>
          <groupId>org.codehaus.mojo</groupId>
          <artifactId>exec-maven-plugin</artifactId>
          <version>1.2.1</version>
          <configuration>
              <mainClass>com.myorg.MyClass</mainClass>
          </configuration>
      </plugin>
   </plugins>
</build>

The class com.myorg.MyClassis in my original source directory, and I can run it with the following command:

mvn -e exec:java -Dexec.classpathScope="test"

I want to:

  • avoid using it -Dexec.classpathScope="test", but I cannot decide how to configure this plugin to search in the test path.
  • write more plugins for other classes (each of which will have different configurations), but now I can only run exec:java. Is there a way to mark this plugin so that I call it through this shortcut, and not just "run everything that is in exec: java"?
  • -javaagent. , pom.xml, . "" - , ?

, <project>.

<properties>
   <spring.version>3.2.6.RELEASE</spring.version>
   <atomikos.version>3.9.2</atomikos.version>
   <loadTimeWeaverArgLine>-javaagent:"${settings.localRepository}/org/springframework/spring-agent/2.5.6/spring-agent-2.5.6.jar"</loadTimeWeaverArgLine>
</properties>

@Michal (qaru.site/questions/1593174/...), :

<profile>
   <id>run-importer</id>
   <properties>
      <loadTimeWeaverArgLine>-javaagent:"${settings.localRepository}/org/springframework/spring-agent/2.5.6/spring-agent-2.5.6.jar"</loadTimeWeaverArgLine>
   </properties>
   <build>
      <plugins>
         <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>1.2.1</version>
            <configuration>
               <executable>java</executable>
                  <!--
                     None of these three options work.
                     <commandlineArgs>-javaagent:C:/Users/robbram/.m2/repository/org/springframework/spring-agent/2.5.6/spring-agent-2.5.6.jar</commandlineArgs>
                     <commandlineArgs>${loadTimeWeaverArgLine}</commandlineArgs>
                     <commandlineArgs>-javaagent:"${settings.localRepository}/org/springframework/spring-agent/2.5.6/spring-agent-2.5.6.jar"</commandlineArgs>
                     <argLine>${loadTimeWeaverArgLine}</argLine>
                  -->
               <classpathScope>test</classpathScope>
               <mainClass>com.myorg.MyClass</mainClass>
            </configuration>
         </plugin>
      </plugins>
   </build>
</profile>

:

mvn -e exec:java -Prun-importer 

:

[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 agent: -javaagent:org.springframework.instrument.jar -> [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)

, com.myorg.MyClass. . , loadTimeWeaverArgLine . :

<profile>
   <id>integration-tests</id>
   <build>
      <plugins>
         <!-- Integration tests require additional loadtime Spring argument -->
         <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.16</version>
            <configuration>
               <forkMode>once</forkMode>
               <argLine> ${loadTimeWeaverArgLine}</argLine>
               <skip>false</skip>
            </configuration>
         </plugin>
      </plugins>
   </build>
</profile>

2 mvn exec: exec

Upodate (, 17 2015 , 12:11:17 PM): , :

<profile>
   <id>run-importer</id>
   <properties>
      <loadTimeWeaverArg>-javaagent:"${settings.localRepository}/org/springframework/spring-agent/2.5.6/spring-agent-2.5.6.jar"</loadTimeWeaverArg>
      <log4JConfigArg>-Dlog4j.configuration=file:${project.build.directory}/path/to/log4j.properties</log4JConfigArg>
      <mainClassArg>com.myorg.MyClass</mainClassArg>
      <arg1>foo</arg1>
   </properties>
   <build>
      <plugins>
         <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>1.2.1</version>
            <goals>
               <goal>exec</goal>
            </goals>
            <configuration>
               <executable>java</executable>
               <classpathScope>test</classpathScope>
               <arguments>
                  <argument>${log4JConfigArg}</argument>
                  <argument>${loadTimeWeaverArg}</argument>
                  <argument>-classpath</argument>
                  <classpath />
                  <argument>${mainClassArg}</argument>
                  <argument>${arg1}</argument>
               </arguments>
            </configuration>
         </plugin>
      </plugins>
   </build>
</profile>

:

mvn -e exec:exec -Prun-importer

:

  • - " ", , src test src.
    • , , . ​​(, , ), .
  • , - , "" , mvn -e exec:exec.
  • java agent, log4j , , pom.
  • -Darg1="bar"
+4
1

, , Maven. :

1/

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>1.2.1</version>
    <configuration>
        <mainClass>com.myorg.MyClass</mainClass>
        <classpathScope>test</classpathScope>
    </configuration>
</plugin>

2/

. :

mvn -e exec:java -Pmy-first-profile

, . - , , .

3/

.

+3

All Articles