netbeans Maven编译错误

igetnqfo  于 2022-11-10  发布在  Maven
关注(0)|答案(9)|浏览(214)

当我在Netbeans中构建和运行我的程序时,它可以正常工作。但是当我尝试“mvn compile”时,使用相同的pom.xml文件时,我得到以下错误:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.0.2:compile (default-compile) on project hadoop-test: Compilation failure
[ERROR] /home/metin/NetBeansProjects/hadoop-test/src/main/java/com/citusdata/hadoop/HadoopTest.java:[53,8] error: generics are not supported in -source 1.3

我的java版本不是1.3,这里是“mvn -version”的结果

Apache Maven 3.0.4
Maven home: /usr/share/maven
Java version: 1.7.0_03, vendor: Oracle Corporation
Java home: /usr/lib/jvm/java-7-openjdk-amd64/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "3.2.0-27-generic", arch: "amd64", family: "unix"

并且这是线53:

Token<BlockTokenIdentifier> token = locatedBlock.getBlockToken();
b09cbbtk

b09cbbtk1#

问题是Maven2中的maven-compiler-plugin默认使用-source 1.3target 1.3
您可以通过将以下内容添加到您的pom中来修复它:

<plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-compiler-plugin</artifactId>
      <configuration>
        <compilerVersion>1.5</compilerVersion>
        <source>1.5</source>
        <target>1.5</target>
      </configuration>
    </plugin>

实际上可以把它放在你最上面的父pom的pluginManagement部分,这样你的派生pom就不需要关心这个了。

gkn4icbw

gkn4icbw2#

您必须在pom.xml中添加一些信息,例如:

<plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>2.3.2</version>
        <configuration>
             <source>1.6</source>
             <target>1.6</target>
        </configuration>
  </plugin>
sz81bmfz

sz81bmfz3#

在您的日志中,我看到这一行:

generics are not supported in -source 1.3

检查maven编译器插件的源和目标配置,并将其更新到1.5。

wsxa1bj1

wsxa1bj14#

看来你的maven编译器插件版本是1.3。
请查看此链接,强制编译器使用JDK 1.7,http://maven.apache.org/plugins/maven-compiler-plugin/examples/compile-using-different-jdk.html运行

prdp8dxp

prdp8dxp5#

看来你的maven java home指向了一个jre而不是jdk。

Java home: /usr/lib/jvm/java-7-openjdk-amd64/jre

你得把它换成jdk。
试试java -version看看有什么
使用/usr/sbin/alternatives --config java来更改java版本。

50pmv0ei

50pmv0ei6#

Here is a slightly different way to fix the same error in Eclipse Indigo.

  • Open your (maven) project in Eclipse.
  • right click on pom.xml
  • select ...Maven...Add Plugin
  • Enter:
- groupId: org.apache.maven.plugins
  - artifactId: maven-compiler-plugin
  - version: 2.3.2

This will insert a new section in your pom.xml and open a window for editing.
Then you can add the source and target xml elements as described above to select the correct JRE.
The section looks like this in my project:

<build>
  <plugins>
     <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>2.3.2</version>
        <configuration>
           <source>1.6</source>
           <target>1.6</target>
        </configuration>
     </plugin>
  </plugins>
</build>

Note the extra tags for "build" and "plugins" absent in some of the other posts.
Once you save the file, Eclipse will notify you that your pom is out of synch with your project.
You can then right click the project ...choose Maven..update project
A key insight for me was that Maven will automatically re-set the build path in Eclipse. Which you can verify by Project...build path..configure build path...Libraries.
So in essence this means you should not have to mess with directly configuring the build path in Eclipse if you are using a Maven project.
HTH

qyzbxkaa

qyzbxkaa7#

请从eclipse执行Maven Run,然后从命令提示符运行mvn测试。在我的情况下,所有问题都通过此步骤解决。

vnzz0bqm

vnzz0bqm8#

将此添加到Pom.xml

<properties>
    <maven.compiler.target>1.8</maven.compiler.target>
    <maven.compiler.source>1.8</maven.compiler.source>
</properties>
mwg9r5ms

mwg9r5ms9#

如果您使用Java 8和Sping Boot 运行应用程序,则可以通过将以下行添加到pom.xml文件中来解决此问题

<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>
                            <version>2.3.2</version>
                            <configuration>
                                 <source>1.8</source>
                                 <target>1.8</target>
                            </configuration>
                    </plugin>
            </plugins>
    </build>

相关问题