java 如何强制 maven 针对 1.5 打包我的项目?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3531260/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 02:16:23  来源:igfitidea点击:

How can I force maven to package my project against 1.5?

javamaven-2maven-assembly-plugin

提问by Jimmy

I am trying to compile a maven project, the source code uses Generics and other featuers of Java 1.5, thus causing my build to fail

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

In my POM.xmlI have configured the build configuration against 1.5 for the source and target properties, but this doesn't solve my issue

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

Is my POM.xmlcorrect, or am I missing something?

我是POM.xml正确的,还是我错过了什么?

Thanks

谢谢

<?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>

Output when attemtping to build

尝试构建时的输出

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

回答by Colin Hebert

You have to set some properties to compile with java 1.5

您必须设置一些属性才能使用 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>

回答by khmarbaise

You configured the assembly-plugin with some information about source/target but to configure the compiling you need to configure the compiler-pluginin the correct way.

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

<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>

Update:This should be combined with maven-enforcer-plugin to force really using of JDK 1.5 instead of only using source/target option of the javac.

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