使用maven的编译错误:-source 1.3中不支持“注释”

时间:2013-01-03 11:44:14

标签: rest maven

我使用Eclipse创建了一个Restful Web服务简单项目。当我尝试编译它时,我收到以下错误。

不确定为什么不支持注释,我使用NetBeans IDE做了同样的事情,
它没有任何问题,工作正常。

**[INFO] Compilation failure
C:\Users\gopc\workspace\RestfulService\src\restfu\Hello.java:[18,1] error: annotations are not supported in -source 1.3
[INFO] ------------------------------------------------------------------------
[DEBUG] Trace
org.apache.maven.BuildFailureException: Compilation failure
C:\Users\gopc\workspace\RestfulService\src\restfu\Hello.java:[18,1] error: annotations are not supported in -source 1.3
    at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoals(DefaultLifecycleExecutor.java:715)
    at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoalWithLifecycle(DefaultLifecycleExecutor.java:556)
    at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoal(DefaultLifecycleExecutor.java:535)
    at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoalAndHandleFailures(DefaultLifecycleExecutor.java:387)
    at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeTaskSegments(DefaultLifecycleExecutor.java:348)
    at org.apache.maven.lifecycle.DefaultLifecycleExecutor.execute(DefaultLifecycleExecutor.java:180)
    at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:328)
    at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:138)
    at org.apache.maven.cli.MavenCli.main(MavenCli.java:362)
    at org.apache.maven.cli.compat.CompatibleMain.main(CompatibleMain.java:60)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at org.codehaus.classworlds.Launcher.launchEnhanced(Launcher.java:315)
    at org.codehaus.classworlds.Launcher.launch(Launcher.java:255)
    at org.codehaus.classworlds.Launcher.mainWithExitCode(Launcher.java:430)
    at org.codehaus.classworlds.Launcher.main(Launcher.java:375)
Caused by: org.apache.maven.plugin.CompilationFailureException: Compilation failure
C:\Users\gopc\workspace\RestfulService\src\restfu\Hello.java:[18,1] error: annotations are not supported** in -source 1.3

这是我的pom文件结构:

POM文件

<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>RestfulService</groupId>
  <artifactId>RestfulService</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <build>
    <sourceDirectory>${basedir}/src</sourceDirectory>
    <outputDirectory>${basedir}/build/classes</outputDirectory>
  <pluginManagement>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.0</version>
        <configuration>
          <source>1.6</source>
          <target>1.6</target>
        </configuration>
      </plugin>
    </plugins>
</pluginManagement>

</build>

<dependencyManagement>
<dependencies>

  <dependency>
    <groupId>jersey-server</groupId>
    <artifactId>jersey-server</artifactId>
    <version>1.4</version>
    <scope>system</scope>
    <systemPath>${basedir}/lib/jersey-server-1.4.jar</systemPath>
  </dependency>

<dependency>
        <groupId>javax-ws</groupId>
        <artifactId>javax-ws</artifactId>
        <version>1.4</version>
        <scope>system</scope>
        <systemPath>${basedir}/lib/javax.ws.rs.jar</systemPath>
</dependency>
</dependencies>
</dependencyManagement>
</project>

Hello.java

package restfu;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

// Plain old Java Object it does not extend as class or implements 
// an interface

// The class registers its methods for the HTTP GET request using the @GET annotation. 
// Using the @Produces annotation, it defines that it can deliver several MIME types,
// text, XML and HTML. 

// The browser requests per default the HTML MIME type.

//Sets the path to base URL + /hello
@Path("/hello")
public class Hello {

  // This method is called if TEXT_PLAIN is request
  @GET
  @Produces(MediaType.TEXT_PLAIN)
  public String sayPlainTextHello() {
    return "Hello Jersey";
  }

  // This method is called if XML is request
  @GET
  //@Produces(MediaType.TEXT_XML)
  @Produces("application/xml")
  public String sayXMLHello() {
    return "<?xml version=\"1.0\"?>" + "<hello> Hello Jersey" + "</hello>";
  }

  // This method is called if HTML is request
  @GET
  @Produces(MediaType.TEXT_HTML)
  public String sayHtmlHello() {
    return "<html> " + "<title>" + "Hello Jersey" + "</title>"
        + "<body><h1>" + "Hello Jersey" + "</body></h1>" + "</html> ";
  }

  // This method is called if JSON is request
  @GET
  //@Produces("appplication/json")
  @Produces(MediaType.APPLICATION_JSON)
  public String sayJsonHello() {
    return "{ \"HTML\": { \"title\": \"Hello Jersey\", \"body\": { \"h1\": \"Hello Jersey\" }}" ;
  }    
} 

3 个答案:

答案 0 :(得分:8)

使用以下内容增强您的pom:

<project>
  [...]
  <build>
    [...]
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.0</version>
        <configuration>
          <source>1.5</source>
          <target>1.5</target>
        </configuration>
      </plugin>
    </plugins>
    [...]
  </build>
  [...]
</project>

你需要至少提供1.5,但更好的是1.6。

我建议不要更改源代码的默认位置。最好将源代码移动到maven的默认位置:src / main / java并从pom中删除配置:

<build>
   <sourceDirectory>${basedir}/src</sourceDirectory>
   <outputDirectory>${basedir}/target/classes</outputDirectory>
</build>

答案 1 :(得分:7)

pluginManagement中添加java版 1.5或以上(如果可能,在父pom中):

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

答案 2 :(得分:0)

谢谢我在basedir中创建了lib文件夹,然后将库文件复制到其中,然后按如下方式添加依赖项,然后解决编译问题 它创建了jar文件和类文件。

<dependencies>
  <dependency>
    <groupId>jersey-server</groupId>
    <artifactId>jersey-server</artifactId>
    <version>1.4</version>
    <scope>system</scope>
    <systemPath>${basedir}/lib/jersey-server-1.4.jar</systemPath>
  </dependency>

    <dependency>
    <groupId>javax-ws</groupId>
    <artifactId>javax-ws</artifactId>
    <version>1.4</version>
    <scope>system</scope>
    <systemPath>${basedir}/lib/javax.ws.rs.jar</systemPath>
  </dependency>