Maven AspectJ plugin fails to build with Java 9 due to missing tools.jar

旧巷老猫 提交于 2019-12-20 12:36:11

问题


I switched my JDK version from 8 to 9 and the AspectJ plugin no longer works due to missing tools.jar:

Execution default of goal org.codehaus.mojo:aspectj-maven-plugin:1.10:compile failed: Plugin org.codehaus.mojo:aspectj-maven-plugin:1.10 or one of its dependencies could not be resolved: Could not find artifact com.sun:tools:jar:9.0.1 at specified path C:\Program Files\Java\jdk-9.0.1/../lib/tools.jar

I understand that tools.jar (and rt.jar) were removed from Java 9 JDK. I am wondering if there a way to get Maven AspectJ plugin to work with Java 9 without tools.jar?

Here is my plugin definition with version info:

    <plugin>
      <groupId>org.codehaus.mojo</groupId>
      <artifactId>aspectj-maven-plugin</artifactId>
      <version>1.10</version>
      <configuration>       
      <encoding>${project.build.sourceEncoding}</encoding>
      <complianceLevel>1.9</complianceLevel>
      <showWeaveInfo>true</showWeaveInfo>
      <XnoInline>true</XnoInline>         
      </configuration>
      <executions>
        <execution>
          <goals>
            <goal>compile</goal>
              <goal>test-compile</goal>
          </goals>
        </execution>
      </executions>     
      <dependencies>
       <dependency>
       <groupId>org.aspectj</groupId>
       <artifactId>aspectjrt</artifactId>
       <version>1.9.0.RC2</version>
      </dependency> 
      <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjtools</artifactId>
        <version>1.9.0.RC2</version>
       </dependency>          
      </dependencies>
    </plugin>

回答1:


Until version 1.11.1 is released into Maven Central by org.codehaus.mojo use the snapshot build instead:

            <groupId>com.github.m50d</groupId>
            <artifactId>aspectj-maven-plugin</artifactId>
            <version>1.11.1</version>



回答2:


I just found an ugly trick to make aspectj working with Java 9, just point com.sun:tools to the pom.xml and the compiler just run.

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>aspectj-maven-plugin</artifactId>
    <version>1.11</version>
    <executions>
        <execution>
            <goals>
                <goal>compile</goal>
                <goal>test-compile</goal>
            </goals>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
                <complianceLevel>1.8</complianceLevel>
                <encoding>UTF-8</encoding>
                <verbose>true</verbose>
                <weaveDependencies>
                    <weaveDependency>
                        <groupId>io.grpc</groupId>
                        <artifactId>grpc-netty</artifactId>
                    </weaveDependency>
                </weaveDependencies>
                <showWeaveInfo>true</showWeaveInfo>
                <XnoInline>true</XnoInline>
            </configuration>
        </execution>
    </executions>
    <dependencies>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>${aspectj.version}</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjtools</artifactId>
            <version>${aspectj.version}</version>
        </dependency>
        <dependency>
            <groupId>com.sun</groupId>
            <artifactId>tools</artifactId>
            <version>${java.version}</version>
            <scope>system</scope>
            <systemPath>${project.basedir}/pom.xml</systemPath>
        </dependency>
    </dependencies>
</plugin>



回答3:


Ran into the same issue, the problem is that this transitive dependency is active by default in the aspectj-maven-plugin.

Fixed it for me with this PR https://github.com/mojohaus/aspectj-maven-plugin/pull/35




回答4:


Have you checked the newest plugin version?

<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.11</version>


来源:https://stackoverflow.com/questions/48173963/maven-aspectj-plugin-fails-to-build-with-java-9-due-to-missing-tools-jar

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