0

I've found a lot of information about how to coax Maven to compile Java-9 source code, but I haven't run into compiler issues. Instead, I have trouble running under Java-9. Running mvn clean install will compile my code fine, but the build still fails with an exception when it runs unit tests. I can't figure out how to run either the unit tests or the application, under Java 9. I suspect that the --add-modules java.xml.bind is getting added to the compiler options but not the runtime options.

I have a simple Spring-Boot 2.0.1 project with one small java source file, one empty java test source, and a pom.xml file. Here's the exception:

java.lang.IllegalStateException: Failed to load ApplicationContext
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError: javax/xml/bind/JAXBException
Caused by: java.lang.NoClassDefFoundError: javax/xml/bind/JAXBException
Caused by: java.lang.ClassNotFoundException: javax.xml.bind.JAXBException

I already know that I can add a jaxb-api dependency to fix this, but that doesn't solve the more general problem of using Maven with Java 9 modules.

Version Info
When I type mvn -v, this is what I get:

Apache Maven 3.5.2 (138edd61fd100ec658bfa2d307c43b76940a5d7d; 2017-10-18T00:58:13-07:00)
Maven home: /custom/apache-maven-3.5.2
Java version: 9.0.1, vendor: Oracle Corporation
Java home: /Library/Java/JavaVirtualMachines/jdk-9.0.1.jdk/Contents/Home
Default locale: en_US, platform encoding: UTF-8
OS name: "mac os x", version: "10.11.6", arch: "x86_64", family: "mac"

(I've read that I need at least maven version 3.5, so this is supposed to work.)

What doesn't work
Here's what I've already tried, that doesn't work:

Neither of these works:

export MAVEN_OPTS="--add-modules java.xml.bind"
export MAVEN_OPTS="--add-modules=java.xml.bind"

Adding a .mvn/jvm.config file with --add-modules java.xml.bind didn't work.

I didn't expect it to help to add the maven compiler plug-in to the pom.xml file, because compiling the code isn't an issue. I tried it anyway, but it didn't help. This is what I added to the project/build/plugins portion of the pom.xml file:

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <compilerArgs>
                        <arg>--add-modules=java.xml.bind</arg>
                    </compilerArgs>
                    <source>1.9</source>
                    <target>1.9</target>
                    <fork>true</fork>
                </configuration>
                <version>3.7.0</version>
            </plugin>

Building under Java 10 didn't help at all.

Source Code
I have an empty application.properties file.

My source file looks like this:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class AngularSpringBootApplication {

    public static void main(String[] args) {
        SpringApplication.run(AngularSpringBootApplication.class, args);
    }
}

My test file looks like this, in the same package on the test tree:

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class AngularSpringBootApplicationTests {

    @Test
    public void contextLoads() {
    }

}

Here's my pom.xml file, which comes from the Spring Boot generator at http://start.spring.io/ (Some of this isn't necessary. This is a stripped-down version of a slightly larger project.)

<?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">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.neptunedreams.tutorial</groupId>
    <artifactId>SpringBootBuildBug</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>

    <name>SpringBootBuildBug</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>9</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-rest</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!--This is the solution to the Jax bug at execution time, but I don't like it. -->
        <!--<dependency>-->
            <!--<groupId>javax.xml.bind</groupId>-->
            <!--<artifactId>jaxb-api</artifactId>-->
            <!--<version>2.3.0</version>-->
        <!--</dependency>-->
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <compilerArgs>
                        <arg>--add-modules=java.xml.bind</arg>
                    </compilerArgs>
                    <source>1.9</source>
                    <target>1.9</target>
                    <fork>true</fork>
                </configuration>
                <version>3.7.0</version>
            </plugin>
        </plugins>
    </build>


</project>
MiguelMunoz
  • 4,548
  • 3
  • 34
  • 51
  • from an overview:= compiler plugin is not responsible for unit testing, – Naman Apr 13 '18 at 09:22
  • Yeah. I didn't expect it to solve the problem, but other posts I've read on this subject highlight that as part of the solution, so I had to say that I already tried it. – MiguelMunoz Apr 13 '18 at 09:40

1 Answers1

0

It turns out there was a simple fix. I didn't have the latest version of Maven. When I upgraded from 3.5.2 to 3.5.3, the problem went away.

MiguelMunoz
  • 4,548
  • 3
  • 34
  • 51