Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven compiler plugin null pointer

Tags:

java

maven

I have a problem during run of my tests by terminal. I need to run mvn install, but I get:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.7.0:compile (default-compile) on project ProjectName: Fatal error compiling: CompilerException: NullPointerException -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.7.0:compile (default-compile) on project ProjectName: Fatal error compiling
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:213)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:154)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:146)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:81)
at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:56)
at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:128)
at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:305)
at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:192)
at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:105)
at org.apache.maven.cli.MavenCli.execute (MavenCli.java:956)
at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:290)
at org.apache.maven.cli.MavenCli.main (MavenCli.java:194)
at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
at jdk.internal.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke (Method.java:564)
at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:289)
at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:229)
at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:415)
at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:356)

I tried to run it with different versions of compiler. When running tests by testNG in Eclipse everything works fine. The problem occurs when running through terminal. These are cucumber tests of mobile application. Runner uses Appium.

like image 614
szumi Avatar asked Apr 20 '18 10:04

szumi


2 Answers

I was also getting Fatal error compiling: CompilerException: NullPointerException when running a maven 3.3.9 compile under JDK 11. I switched to JDK 8 and no longer got the NullPointerException.

like image 175
roj Avatar answered Oct 13 '22 18:10

roj


It happens when maven compiler plugin can't find some required part of your source code.

Let me tell you my case:

  1. I created a spring boot application and wanted to test it
  2. So I created another maven module to create an API for automated GUI tests
  3. spring-boot-maven-plugin overrides the way jars are created so the directories inside the .jar change so I had to tune the maven-jar-plugin to generate the jar for me
  4. I forgot to include a package (service) in this plugin so the same error you have appeared

So... I included the package and it worked

     <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>3.1.2</version>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <mainClass>inventarios.Inventarios</mainClass>
                    </manifest>
                </archive>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>jar</goal>
                    </goals>
                    <phase>package</phase>
                    <configuration>
                        <!--to be imported on other projects-->
                        <classifier>app-to-import</classifier>
                        <includes>
                            <include>**/desktop/*</include>
                            <include>**/desktop/navigation/*</include>
                            <include>**/service/*</include> <!--the package I forgot-->
                            <include>**/util/*</include>
                        </includes>
                    </configuration>
                </execution>
            </executions>
        </plugin>

Another suggestion I found while searching for the bug in JDK classes was to add on the compiler plugin the configuration option forceJavacCompilerUse

<plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-compiler-plugin</artifactId>
   <version>3.8.1</version>
   <configuration>
      <forceJavacCompilerUse>true</forceJavacCompilerUse>
   </configuration>
</plugin>
like image 6
Ruslan López Avatar answered Oct 13 '22 17:10

Ruslan López