Exception with surefire plugin with test phase in maven

13,650

Why not putting the versions into this pom, cause this pom is controlled by you and anyone else who ever will use your project will be astonished that it does not work, cause he missed to give the properties on command line. BTW: Have you checked the output via mvn -X -Dsurefire.version=2.12 etc. I think you will get the same error message.

I have rechecked and if you define the version in that way your project is not correct anymore if someone miss the options (-D...) on command line you will get things like this:

INFO] Scanning for projects...
[ERROR] The build could not read 1 project -> [Help 1]
[ERROR]   
[ERROR]   The project com.soebes.katas:code:1.0-SNAPSHOT (/home/katas/pom.xml) has 1 error
[ERROR]     'build.plugins.plugin.version' for org.apache.maven.plugins:maven-surefire-plugin must be a valid version but is '${surefire.version}'. @ line 36, column 20
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/ProjectBuildingException

In other words its a real bad idea to define plugin versions (or dependencies of it) via options on command line. I simply recommend you to define the version of all you need into your pom and make a correct use of unit- and integration tests via surefire and/or failsafe plugin. This will solve your problems.

Share:
13,650
bclarance
Author by

bclarance

Updated on June 08, 2022

Comments

  • bclarance
    bclarance over 1 year

    I need to use surefire v2.12, but I get the attached stack trace every time I run that version. If I run v2.10 I do not get the error. I need this version so I can annotate my test classes with @Category and label them as either UnitTests or IntegrationTests. Here's my command: mvn test -Dsurefire.version=2.12 -X

        [ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12:test (default-test) on project PROJECT: Execution default-test of goal org.apache.maven.plugins:maven-surefire-plugin:2.12:test failed: java.lang.reflect.InvocationTargetException; nested exception is java.lang.reflect.InvocationTargetException: null: ExceptionInInitializerError: Unexpected ClassNotFoundException looking up class 'org.apache.maven.cli.MavenCli' -> [Help 1]
    org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12:test (default-test) on project ASPEN: Execution default-test of goal org.apache.maven.plugins:maven-surefire-plugin:2.12:test failed: java.lang.reflect.InvocationTargetException; nested exception is java.lang.reflect.InvocationTargetException: null
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:225)
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
        at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:84)
        at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:59)
        at org.apache.maven.lifecycle.internal.LifecycleStarter.singleThreadedBuild(LifecycleStarter.java:183)
        at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:161)
        at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:320)
        at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:156)
        at org.apache.maven.cli.MavenCli.execute(MavenCli.java:537)
        at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:196)
        at org.apache.maven.cli.MavenCli.main(MavenCli.java:141)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:290)
        at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:230)
        at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:409)
        at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:352)
        Caused by: org.apache.maven.plugin.PluginExecutionException: Execution default-test of goal org.apache.maven.plugins:maven-surefire-plugin:2.12:test failed: java.lang.reflect.InvocationTargetException; nested exception is java.lang.reflect.InvocationTargetException: null
        at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:110)
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:209)
        ... 19 more
        Caused by: org.apache.maven.surefire.util.SurefireReflectionException: java.lang.reflect.InvocationTargetException; nested exception is java.lang.reflect.InvocationTargetException: null
        at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:197)
        at org.apache.maven.surefire.util.ReflectionUtils.invokeGetter(ReflectionUtils.java:69)
        at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.getSuites(ProviderFactory.java:148)
        at org.apache.maven.plugin.surefire.booterclient.ForkStarter.getSuitesIterator(ForkStarter.java:317)
        at org.apache.maven.plugin.surefire.booterclient.ForkStarter.runSuitesForkPerTestSet(ForkStarter.java:149)
        at org.apache.maven.plugin.surefire.booterclient.ForkStarter.run(ForkStarter.java:118)
        at org.apache.maven.plugin.surefire.AbstractSurefireMojo.executeProvider(AbstractSurefireMojo.java:199)
        at org.apache.maven.plugin.surefire.AbstractSurefireMojo.executeAllProviders(AbstractSurefireMojo.java:176)
        at org.apache.maven.plugin.surefire.AbstractSurefireMojo.executeAfterPreconditionsChecked(AbstractSurefireMojo.java:135)
        at org.apache.maven.plugin.surefire.AbstractSurefireMojo.execute(AbstractSurefireMojo.java:98)
        at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:101)
        ... 20 more
        Caused by: java.lang.reflect.InvocationTargetException
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189)
        ... 30 more
        Caused by: java.lang.ExceptionInInitializerError
        ... 2 more
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Class.java:169)
        at org.junit.runner.Description.getTestClass(Description.java:207)
        at org.apache.maven.surefire.common.junit48.FilterFactory$GroupMatcherCategoryFilter.shouldRun(FilterFactory.java:207)
        at org.apache.maven.surefire.junitcore.JUnitCoreProvider.getSuitesAsList(JUnitCoreProvider.java:169)
        at org.apache.maven.surefire.junitcore.JUnitCoreProvider.getSuites(JUnitCoreProvider.java:102)
        ... 35 more
        Caused by: java.lang.RuntimeException: Unexpected ClassNotFoundException looking up class 'org.apache.maven.cli.MavenCli'
        ... 12 more
    
        (deleted lines here)
    

    My pom.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>sample</artifactId>
        <groupId>sample</groupId>
        <version>1.0</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>webapp</artifactId>
    <packaging>war</packaging>
    <dependencies>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${org.slf4j-version}</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>${org.slf4j-version}</version>
        </dependency>
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-all</artifactId>
            <version>1.9.0</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.10</version>
        </dependency>
    </dependencies>
    <reporting>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>findbugs-maven-plugin</artifactId>
                <version>2.5.1</version>
            </plugin>
        </plugins>
    </reporting>
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-surefire-plugin</artifactId>
                <dependencies>
                    <dependency>
                        <groupId>org.apache.maven.surefire</groupId>
                        <artifactId>surefire-junit47</artifactId>
                        <version>${surefire.version}</version>
                        <scope>test</scope>
                    </dependency>
                </dependencies>
                <configuration>
                    <groups>com.webapp.api.annotation.type.${group.include}</groups>
                    <excludedGroups>com.webapp.api.annotation.type.${group.exclude}</excludedGroups>
                </configuration>
            </plugin>
        </plugins>
    </build>
    </project>
    

    Also, I was able to do this in a different project that I set up from scrap. But in this case as you can see I am overwriting the surefire plugin version.

    Any ideas?

    Thanks

    • gkamal
      gkamal over 11 years
      Which version of maven are you using?
    • khmarbaise
      khmarbaise over 11 years
      If you have integration tests maven-surefire-plugin is the wrong way. For integration test use the maven-failsafe-plugin instead.
    • bclarance
      bclarance over 11 years
      @gkamal I an using maven 3.0.4
    • bclarance
      bclarance over 11 years
      @khmarbaise I have both integration tests and unit tests and I want to label both. The ${group.exclude} and ${group.include} respectively identify which of the two labels are used for which. ie. -Dgroup.include=UnitTest
    • khmarbaise
      khmarbaise over 11 years
      The best to separate unit and integration test is to use the naming conventions (unit test: Test.java, Test.java, TestCase.java; integration tests: IT.java, *IT.java or *ITCase.java). If you are using the plugins (surefire for unit tests and failsafe for integration test) correct you don't need to use such a weird construction via group excludes etc. They are separated by the lifecycle.
    • khmarbaise
      khmarbaise over 11 years
      Furthermore the posted pom looks like an excerpt of your pom can you give more details?
    • bclarance
      bclarance over 11 years
      @khmarbaise Yeah I understand that would be best practice and the intention is probably to run the different tests in different lifecycles, but I am dealing with heavy duty legacy code where I cannot start renaming test classes. Any ideas of how to make this work would be appreciated?
    • bclarance
      bclarance over 11 years
      in regards to the pom. Yes you are correct. I cannot post the rest of the pom, but I will tell you that it inherits from some parent poms from which one of them have junit 3.8.2 as a dependency and the surefire version 2.10 from which I override the property using -Dsurefire.version=2.12
  • bclarance
    bclarance over 11 years
    I couldn't agree more, and that parent pom will be updated eventually. My problem is that the parent pom has many other dependents so I need to verify that it is working before I can apply that change/upgrade permanently. Yes I have run it with -X option, but don't see how that is helping me. If you know what to look for please advise.
  • khmarbaise
    khmarbaise over 11 years
    Simply put the version number in you pom and test it that way. Furthermore using versions of plugin and/or dependencies on command line does not work. It would have taken 50 seconds to change that pom and tested with the versions you have given. You can comment in the pom why you decided to use a different maven-surefire-pugin version instead of the inherited.
  • bclarance
    bclarance over 11 years
    Changed the project pom with version and still getting exception; mvn clean test -e -X. Here is some output to verify that the change took effect: [DEBUG] org.apache.maven.surefire:surefire-api:jar:2.12:compile (selected for compile) any other ideas?
  • khmarbaise
    khmarbaise over 11 years
    Can you post the output on pastebin etc.