Maven 编译失败 ( "invalid target release" )



我尝试使用Maven编译一个Java项目。同样的项目在我的 Windows 机器上编译得很好,但在我的 Linux 服务器上出错:

# mvn package                                                                 :(
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building X 1.0
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.3:resources (default-resources) @ X ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 15 resources
[INFO]
[INFO] --- maven-compiler-plugin:2.4:compile (default-compile) @ X ---
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 19 source files to /root/X/target/classes
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] Failure executing javac, but could not parse the error:
javac: invalid target release: 2.4
Usage: javac <options> <source files>
use -help for a list of possible options
[INFO] 1 error

这是我使用的绒球.xml

# cat pom.xml                                                                
<?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>
    <packaging>jar</packaging>
    <properties>
        <jdk.version>1.8</jdk.version>
    </properties>
    <name>X</name>
    <repositories>
        <repository>
            <id>clojars</id>
            <url>http://clojars.org/repo/</url>
        </repository>
    </repositories>
    <groupId>X</groupId>
    <artifactId>X</artifactId>
    <version>1.0</version>
    <dependencies>
        <dependency>
            <groupId>de.sciss</groupId>
            <artifactId>weblaf-ui</artifactId>
            <version>1.28</version>
        </dependency>
        <dependency>
            <groupId>kryonet</groupId>
            <artifactId>kryonet</artifactId>
            <version>2.21</version>
        </dependency>
        <dependency>
            <groupId>com.esotericsoftware</groupId>
            <artifactId>kryo</artifactId>
            <version>3.0.0</version>
        </dependency>
        <dependency>
            <groupId>com.intellij</groupId>
            <artifactId>forms_rt</artifactId>
            <version>5.0</version>
        </dependency>
        <dependency>
            <groupId>net.coobird</groupId>
            <artifactId>thumbnailator</artifactId>
            <version>[0.4, 0.5)</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <source>2.4</source>
                    <target>2.4</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>1.6</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <!-- Build an executable JAR -->
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>src/</classpathPrefix>
                            <mainClass>MainServer</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
        <resources>
            <resource>
                <directory>res</directory>
            </resource>
        </resources>
    </build>
</project>

我认为这与maven-jar-plugin有关,因为这是pom.xml中唯一版本号为 2.4 的东西。这里与我的Windows机器有什么区别?为什么编译失败?

您设置了错误的 Java 源版本和目标版本:

            <configuration>
                <source>2.4</source>
                <target>2.4</target>
            </configuration>

在那里你告诉 Java,你的源代码是针对语言 2.4 版的:)

只需将其更改为

            <configuration>
                <source>1.7</source>
                <target>1.7</target>
            </configuration>

或者与您安装的 jdk 和您正在使用的语言功能相匹配的任何内容。

顺便说一下,作为一般规则,你放入POM的内容并不是Maven将使用的全部;Maven使用的最终POM是超级POM,层次结构中的POM以及各种默认值来源(即settings.xml文件等)的混合和合并。

java编译器的源版本和目标版本(maven-compiler-plugin)是指java版本,它仅支持1.4 - 1.9。你需要用一个至少和目标一样新的Java来运行maven。如果您不知道它的用途,我会简单地删除此插件的<configuration>

相关内容

最新更新