Failed to execute maven-compiler-plugin:3.6.1:testCompile when using java 9

最后都变了- 提交于 2019-12-09 17:17:57

问题


I know that there are a lot question with similar error. I would appreciate before point as duplicate take in account that it only happens with Java 9.

I do have java 9 installed

C:\_pocs\ws_j9\java9-http-client>java -version
java version "9.0.1"
Java(TM) SE Runtime Environment (build 9.0.1+11)
Java HotSpot(TM) 64-Bit Server VM (build 9.0.1+11, mixed mode)

C:\_pocs\ws_j9\java9-http-client>echo %JAVA_HOME%
C:\Program Files\Java\jdk-9.0.1

To make simple the exemplification, if i download a very simple example https://examples.javacodegeeks.com/core-java/java-9-httpclient-example/ and I try/

mvn clean install package -e

[ERROR] Failed to parse module-info:
[ERROR] With qdox: null
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.925 s
[INFO] Finished at: 2018-01-01T23:43:32+01:00
[INFO] Final Memory: 13M/44M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.6.1:testCompile (default-testCompile) on project http_client: Execution default-testCompile of goal org.apache.maven.plugins:maven-compiler-plugin:3.6.1:testCompile failed: Failed to parse module-info -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.6.1:testCompile (default-testCompile) on project http_client: Execution default-testCompile of goal org.apache.maven.plugins:maven-compiler-plugin:3.6.1:testCompile failed: Failed to parse module-info
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:213)

pom.xml

<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.javacodegeeks.java9</groupId>
    <artifactId>http_client</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>Java9HttpClient</name>
    <description>Java Http Client example</description>
    <properties>
        <java-version>1.9</java-version>

        <maven-compiler-plugin-version>3.6.1</maven-compiler-plugin-version>
        <maven-shade-plugin-version>3.0.0</maven-shade-plugin-version>
    </properties>

    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>${maven-compiler-plugin-version}</version>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-shade-plugin</artifactId>
                    <version>${maven-shade-plugin-version}</version>
                </plugin>
            </plugins>
        </pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>${java-version}</source>
                    <target>${java-version}</target>
                    <verbose>true</verbose>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <transformers>
                                <transformer
                                    implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>com.javacodegeeks.java9.http_client.Main</mainClass>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

I am bit confused what could be causing this issue. On top of this, I am facing another challenge that make it a bit harder for someone trying to use Java 9 for first time: my Eclipse isn't properly accepting Java 9. I tried install Java 9 Support in Eclipse Oxygen which I just downloaded it but I am getting "No repository found at http://download.eclipse.org/eclipse/updates/none". And I can't make this mvn plugin update the Java version to 9 (it used to work properly for Java < 1.8:

    <properties>
        <java-version>1.9</java-version>
...
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>${java-version}</source>
                    <target>${java-version}</target>
                    <verbose>true</verbose>
                </configuration>
            </plugin>

That is the reason I am trying to compile straich from command line.

I read that Java 9 came with new concept of modularity which will help us better organize the packages but I am very limeted if this could drive me to fix my issue. It might be some obvious solution whe someone sees "Failed to parse module-info" but I can't imagine anything usefull to try.

My straight question is what I could check in order to compile. An additional trick regard making Eclipse Oxygen current version compatible with Java 9 will be appreciatte as well.

  • Edited

Eclipse version downloaded for Windows less than 24 hours ago:

Eclipse Java EE IDE for Web Developers.

Version: Oxygen.2 Release (4.7.2) Build id: 20171218-0600

  • Edited second time

After I changed from 1.9 to 9 the red error icon was gone and I have now Java 9. Nevertheless, now I got this error:

[ERROR] COMPILATION ERROR : 
[INFO] -------------------------------------------------------------
[ERROR] No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
[INFO] 1 error
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.542 s
[INFO] Finished at: 2018-01-02T05:19:30+01:00
[INFO] Final Memory: 9M/30M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.7.0:compile (default-compile) on project http_client: Compilation failure
[ERROR] No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?

Which doesn't make sense to me because:

  • Edited third time


回答1:


In short, add a missing/misplaced class named module-info.java to include your project's module declaration at your project's source root. (e.g src/main/java)

Additionally, note to upgrade to the version 3.7.0 of the maven-compiler-plugin in case you have a comment included in your module-info class. [The logs seems pretty relevant to this.]


Or the other way, if you wish to compile your project with a previous version of Java then use the release flag as:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.7.0</version>
    <configuration>
        <release>8</release>
        <verbose>true</verbose>
    </configuration>
</plugin>

Concepts

With Java9 marking the completion of the project Jigsaw therein introducing the Java Platform Module System (JPMS), projects build on Java version 9 and above would be modularised. This would include a primary compiled class called module-info.class which is that module's declaration.

The initial draft and eventually The State of the Module System describes best over the concepts followed and practiced.


An additional trick regard making Eclipse Oxygen current version compatible with Java 9

AFAIK the latest Oxygen builds (Oxygen.2 at this point in time) from Eclipse are meant to be Java9 compatible without any additional plugin support anymore.


Additional Useful Resources:

  • JDK9 Migration Guide

  • Maven compiler plugin's example over module-info inclusion and exclusion




回答2:


in the second edit, you have to add jdk to your build path. Go to windows->Preferences->Java->installed jre's and add the jdk, and check it.



来源:https://stackoverflow.com/questions/48053904/failed-to-execute-maven-compiler-plugin3-6-1testcompile-when-using-java-9

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!