运行 来自测试的主要方法 class 通过 maven

Running main method from test class via 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>

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

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

我想:

  1. 避免必须使用 -Dexec.classpathScope="test",但我不知道如何配置该插件以在测试 classpath 中查找。
  2. 为其他 class 编写更多插件(每个都有不同的配置),但现在我只能 运行 exec:java。有没有办法给这个插件贴上标签,这样我就可以通过那个标签来调用它,而不是只说 "run whatever is in exec:java"?
  3. 拉一个-javaagent属性。我在我的 pom.xml 中定义了这个 属性,它被测试用例使用。我的 "custom" 插件会获取这些属性吗?或者我需要做些什么来将它们拉进来吗?

这里是属性,直接定义在<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 的建议 (),这是我尝试过的方法:

<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'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)

我可以确认主要的 class 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>

尝试 2 使用配置文件和 mvn exec:exec

更新(2015 年 6 月 17 日,星期三,12:11:17 下午):在 之后,我现在可以按照自己的意愿工作:

<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

这种方法的优点:

我不确定您实际上要通过它完成什么,因为它对 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/

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