尝试使用maven从命令行运行Java7 Hello World Project。如果我在eclipse中运行代码,我的项目运行正常,但如果我尝试执行“mvn packege”包,我会收到以下错误..
这是我的来源:
public class App
{
public static void main( String[] args )
{
System.out.println( "Hello World!, Tesing Java 7" );
printDay("Wednesday");
}
public static void printDay(String dayOfWeek) {
switch (dayOfWeek)
{
case "Sunday": System.out.println("Dimanche"); break;
case "Monday": System.out.println("Lundi"); break;
case "Tuesday": System.out.println("Mardi"); break;
case "Wednesday": System.out.println("Mercredi"); break;
case "Thursday": System.out.println("Jeudi"); break;
case "Friday": System.out.println("Vendredi"); break;
case "Saturday": System.out.println("Samedi"); break;
default: System.out.println("Error: '"+ dayOfWeek +"' is not a day of the week"); break;
}
}
}
这是我的pom.xml文件:
<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>
<groupId>org.smith</groupId>
<artifactId>TestJava7</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>TestJava7</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
这是错误:
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] /home/jsmith/Documents/workspace-juno/TestJava7/src/main/java/org/smith/TestJava7/App.java:[16,13] error: strings in switch are not supported in -source 1.5
[INFO] 1 error
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.169s
[INFO] Finished at: Wed Jan 16 09:48:26 EST 2013
[INFO] Final Memory: 10M/100M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.3.2:compile (default-compile) on project TestJava7: Compilation failure
[ERROR] /home/jsmith/Documents/workspace-juno/TestJava7/src/main/java/org/smith/TestJava7/App.java:[16,13] error: strings in switch are not supported in -source 1.5
[ERROR] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException
答案 0 :(得分:9)
设置编译器插件的源和目标版本:
http://maven.apache.org/plugins/maven-compiler-plugin/
<project>
[...]
<build>
[...]
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
[...]
</build>
[...]
</project>
答案 1 :(得分:2)
错误显然是在说:
strings in switch are not supported in -source 1.5
您需要将maven-compiler-plugin
配置为指向JDK 7,默认情况下它将指向1.5或1.6
尝试Here for more information了解如何配置相同内容。
答案 2 :(得分:1)
将maven插件添加到POM.xml中,清理并重新运行..
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>