本文介绍了Maven - 使用JDK 7编译JVM 5的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直试图让它工作一段时间但是还没有运气。

I've been trying to get this to work for a while now but no luck yet.

我想用 JAVA_HOME 指向JDK7,但我想为JVM 5编译一个项目。我读过,我发现,但它们似乎都没有在我的设置中工作。

I want to run with JAVA_HOME pointing to JDK7 but I want to compile a project for JVM 5. I've read through documentation, I've found similar posts on SO, but none of them seem to work in my setup.

我是第一个尝试设置目标,但我收到错误:

I first tried with setting just target and source but I got an error:

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



据我所知,该类已在JDK 7中更新,并且添加了引发错误的额外方法。我需要使用具有旧实现的JDK 5的运行时,一切都应该正常工作。所以我这样做:

As far as I understood that class was updated in JDK 7 and the extra method that's throwing the error was just added. I need to use the runtime of JDK 5 that has the old implementation and everything should work fine. So I do this:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
        <verbose>true</verbose>
        <source>1.5</source>
        <target>1.5</target>
        <compilerArguments>
            <bootclasspath>${env.JAVA5_HOME}/jre/lib/rt.jar</bootclasspath>
        </compilerArguments>
    </configuration>
</plugin>

我在系统上正确设置了JAVA5_HOME,我可以看到它在日志中加载了正确的类,但是我遇到了另一个错误:

I have JAVA5_HOME set correctly on my system, I can see it loading the correct classes in the log, but I hit another error:

这是公平的,因为我没有在 jce.jar (加密类)中包含启动类路径。但是有一件事让我很困惑。即使 bootclasspath 仅包含Java 5运行时,我在类路径中也有很多来自JRE7的库。它们没有在任何地方指定。

Which is fair enough, since I didn't include jce.jar (cryptography classes) in the bootclasspath. There is a thing that puzzles me, though. Even though the bootclasspath contains only the Java 5 runtime, I have a lot of libraries from JRE7 in the classpath. They are not specified anywhere.

如果我尝试添加jce.jar(来自JRE5),我会回到第一个错误:

If I try and add jce.jar (from JRE5), I get back to the first error:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
        <verbose>true</verbose>
        <source>1.5</source>
        <target>1.5</target>
        <compilerArguments>
            <bootclasspath>${env.JAVA5_HOME}/jre/lib/rt.jar${path.separator}${env.JAVA5_HOME}/jre/lib/jce.jar</bootclasspath>
        </compilerArguments>
    </configuration>
</plugin>



我也看不到 rt.jar 正在加载,但我没有得到 java.lang 找不到错误,所以在类路径上加载了一些类。

I also see no trace of rt.jar being loaded, but I don't get a java.lang not found error, so there are some classes being loaded on the classpath.

我会在构建之前制作一个覆盖 JAVA_HOME 的批处理脚本并暂时修复它,但我真的希望这样做是正确的办法。这似乎不是一个极端的用例。 :)

I'll fix it temporarily by making a batch script that overwrites JAVA_HOME before building and sets it back afterwards, but I really want this done the right way. This doesn't seem as such an extreme use-case. :)

我在这里做错了什么?

推荐答案

这个答案是针对问题的标题,而不是针对问题的具体问题。

我最终使用了这个解决方案我的项目,允许我通过激活maven配置文件选择性地使用自定义引导类路径。我强烈建议为此使用配置文件,否则它会使没有设置环境变量的任何人的构建失败(非常糟糕,特别是对于开源项目)。
我只在我的IDE中激活此配置文件以进行清理和构建操作。

I ended up using this solution in my project, which allows me to use the custom bootstrap classpath selectively, by activating a maven profile. I strongly recommend using a profile for this, because otherwise it makes the build fail for anyone that does not have the environment variable set (very bad, especially for an open source project).I only activate this profile in my IDE for the "Clean & Build" action.

    <profile>
        <id>compileWithJava5</id>
        <!--
            NOTE
            Make sure to set the environment variable JAVA5_HOME
            to your JDK 1.5 HOME when using this profile.
        -->
        <properties>
            <java.5.home>${env.JAVA5_HOME}</java.5.home>
            <java.5.libs>${java.5.home}/jre/lib</java.5.libs>
            <java.5.bootclasspath>${java.5.libs}/rt.jar${path.separator}${java.5.libs}/jce.jar</java.5.bootclasspath>
        </properties>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <configuration>
                        <source>1.5</source>
                        <target>1.5</target>
                        <compilerArguments>
                            <bootclasspath>${java.5.bootclasspath}</bootclasspath>
                        </compilerArguments>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </profile>

这篇关于Maven - 使用JDK 7编译JVM 5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-26 19:11
查看更多