通过maven从测试类运行main方法

Rob*_*ram 6 maven-plugin maven

我的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>
Run Code Online (Sandbox Code Playgroud)

该类com.myorg.MyClass位于我的测试源目录中,我可以使用以下命令运行它:

mvn -e exec:java -Dexec.classpathScope="test"
Run Code Online (Sandbox Code Playgroud)

我想要:

  1. 避免必须使用-Dexec.classpathScope="test",但我无法弄清楚如何配置该插件在测试类路径中查找.
  2. 为其他类编写更多的插件(每个类都有不同的配置),但是现在我只能运行exec:java.有没有办法标记这个插件,以便我通过该标签调用它,而不是只是说"运行在exec:java中的任何东西"?
  3. 拉入一处-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>
Run Code Online (Sandbox Code Playgroud)

尝试配置文件

继@ Michal的建议(/sf/answers/2158787711/)之后,这就是我的尝试:

<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>
Run Code Online (Sandbox Code Playgroud)

我运行它:

mvn -e exec:java -Prun-importer 
Run Code Online (Sandbox Code Playgroud)

我使用以下任一选项获得以下异常:

[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 -> [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)
Run Code Online (Sandbox Code Playgroud)

我可以确认com.myorg.MyClass正在执行主类.我可以看到它的其他输出.我也可以确认loadTimeWeaverArgLine这个POM的其他部分有用.它已成功用于集成测试:

<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>
Run Code Online (Sandbox Code Playgroud)

尝试使用profile和mvn exec:exec

Upodate(2015年6月17日星期三,下午12:11:17):根据Michal的最新评论,我现在按照我的意愿工作:

<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>
Run Code Online (Sandbox Code Playgroud)

我运行它:

mvn -e exec:exec -Prun-importer
Run Code Online (Sandbox Code Playgroud)

这种方法的优点:

  • 此配置文件的整个目的是运行"特殊代码",该代码永远不应该部署,但需要在src和test src中使用代码.
    • 我注意到Michal的建议,这应该是一个单独的模块.如果这个代码库还没有那么完善(大,旧,复杂),我会认真考虑这个.
  • 它留下了空间以防万一需要复制,所以没有"竞争"关于什么运行mvn -e exec:exec.
  • 我可以使用pom中已存在的变量来指定java代理,log4j和许多其他配置.
  • 我可以在命令行上覆盖任何这些参数 -Darg1="bar"

Mic*_*ski 4

我不确定你实际上要通过这个完成什么,因为它对 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>
Run Code Online (Sandbox Code Playgroud)

2/

您可以使用配置文件,并将不同的插件配置放在不同的配置文件中。然后你打电话:

mvn -e exec:java -Pmy-first-profile
Run Code Online (Sandbox Code Playgroud)

不过我觉得这真的很奇怪。我的建议是重新考虑您的案例,也许选择另一种方法或工具。

3/

经过作者编辑后问题本身的最终解决方案。