Maven无法编译(“无效的目标发布”)

时间:2015-01-18 18:55:49

标签: java maven

我尝试使用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

这是我使用的pom.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机器有什么区别?为什么不能编译?

2 个答案:

答案 0 :(得分:3)

您设置了错误的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文件等)的混合和合并。

答案 1 :(得分:2)

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