10

我正在尝试编译一个 maven 项目,源代码使用 Java 1.5 的泛型和其他功能,从而导致我的构建失败

在我的POM.xml我已经为源和目标属性配置了针对 1.5 的构建配置,但这并不能解决我的问题

我是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>
    <name>MyClass</name>
    <groupId>uk.co.mydomain</groupId>
    <artifactId>MyClass</artifactId>
    <version>1.0</version>

    <build>
      <finalName>MyClass</finalName>
      <plugins>
        <plugin>
          <artifactId>maven-assembly-plugin</artifactId>
          <configuration>
            <source>1.5</source>
            <target>1.5</target>
            <descriptors>
              <descriptor>src/main/resources/dist.xml</descriptor>
            </descriptors>
            <archive>
              <manifestFile>src/main/resources/META-INF/MANIFEST.MF</manifestFile>
            </archive>
          </configuration>
        </plugin>
      </plugins>
    </build>

    <repositories>
        <repository>
            <id>sun-repo-2</id>
            <url>http://download.java.net/maven/2/</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>
</project>

尝试构建时的输出

generics are not supported in -1.3 (use -source 5 or higher to enable generics)
4

2 回答 2

25

您必须设置一些属性才能使用 java 1.5 进行编译

<properties>
    <!-- maven-compiler-plugin configuration -->
    <maven.compiler.source>1.5</maven.compiler.source>
    <maven.compiler.target>1.5</maven.compiler.target>
</properties>
于 2010-08-20T13:33:38.260 回答
16

您使用有关源/目标的一些信息配置了程序集插件,但是要配置编译,您需要以正确的方式配置编译器插件

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

更新: 这应该与 maven-enforcer-plugin 结合使用,以强制真正使用 JDK 1.5,而不是仅使用 javac 的源/目标选项。

于 2010-08-20T13:33:48.923 回答