使用retrolambda将Java 1.8代码转换为1.6

时间:2017-04-10 20:18:57

标签: java maven intellij-idea

我需要将Java 1.8库转换为使用lambda表达式,方法引用和默认方法的1.6。我找到了后端项目https://github.com/orfjackal/retrolambda。我需要构建的项目使用Maven,我在pom.xml中添加了插件

<build>
<plugins>
  <plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
      <source>1.8</source>
      <target>1.8</target>
    </configuration>
  </plugin>

  <plugin>
    <groupId>net.orfjackal.retrolambda</groupId>
    <artifactId>retrolambda-maven-plugin</artifactId>
    <version>2.5.1</version>
    <executions>
      <execution>
        <goals>
          <goal>process-main</goal>
          <goal>process-test</goal>
        </goals>
      </execution>
    </executions>
    <configuration>
      <defaultMethods>true</defaultMethods>
      <target>1.6</target>
    </configuration>
  </plugin>
</plugins>

我从IntelliJ IDEA构建.jar - &gt;构建 - &gt;构建工件。但是项目仍在生成Java 1.8库。我是maven和java的新手,我如何构建为java 1.6库?

编辑1 现在项目似乎生成了正确的.jar版本,但lib现在抛出异常。

Exception in thread "main" java.lang.NoClassDefFoundError: java/nio/file/attribute/FileAttribute
at com.morpherltd.dawg.MReader.<init>(MReader.java:15)
at com.morpherltd.dawg.SingleWordAdjectivizer.<init>(SingleWordAdjectivizer.java:17)
at com.morpherltd.dawg.Adjectivizer.<init>(Adjectivizer.java:4)
at LibTest.main(LibTest.java:8)
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 com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
Caused by: java.lang.ClassNotFoundException: 
java.nio.file.attribute.FileAttribute
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
... 9 more

1 个答案:

答案 0 :(得分:0)

将此插件添加到您的项目pom - execute maven goal from Intelli

process-main目标会处理您的主要课程;

process-test目标将处理测试类。

目标执行后,target/classes目录

中应该提供生成的工件