在多个JDK下进行开发的最佳实践 [英] Best practice for developing under multiple JDKs

查看:81
本文介绍了在多个JDK下进行开发的最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个正在开发的库,该库具有与JDK 6的目标兼容性。我喜欢利用Java 8功能,例如lambda。这些代码和API将从Java 8中受益匪浅。

I have a library which I am currently developing with target compatibility to JDK 6. I like to take advantage of Java 8 feature, like lambdas. The code and APIs will greatly profit from Java 8.

我想提供Java 6和Java 8的库版本,例如,其中只有一部分

I would like to offer versions of the library for Java 6 and Java 8, e.g., where a certain part is only available in the Java 8 version.

现在,有一些核心类可能具有不同的源版本(旧版Java 6版本和新Java 8版本)。

Now there are a few core classes which might have their different source version (the legacy Java 6 version and the new Java 8 version).

这样做的最佳实践是什么?
在Objective-C或C ++中,我会考虑使用预处理器指令。

What is the best practice of doing this? In Objective-C or C++ I would consider using a preprocessor directive.

推荐答案

我看到了两个选择:


  1. 打包不同版本的库,一个用于JDK6,另一个用于JDK8。用户有责任为其应用程序添加合适的应用程序。这将是一个有趣的构建。如果您使用的是Maven,我想您只会使用3个项目;一个具有所有公共类的公共项目,一个具有commons项目作为依赖项的JDK6项目,并构建JDK6 jar,然后一个具有commons项目作为依赖项的 JDK8项目,并构建JDK8 jar。这种方法的一个吸引人的地方是它显然可以工作,并且您无需进行任何维护。

  2. 将JDK6和JDK8版本打包在同一JAR中,然后创建一个运行时类型的类,负责根据运行的JDK在运行时创建库对象的正确版本。例如:

  1. Package different versions of your library, one for JDK6 and one for JDK8. Users are responsible for including the right one for their application. This will be an interesting build to do. If you're using Maven, I think you'll just use 3 projects; a "commons" project with all common classes, a "JDK6" project that has the commons project as a dependency and builds the JDK6 jar, and then a "JDK8" project that has the commons project as a dependency and builds the JDK8 jar. One attractive bit of this approach is that it obviously works, and you have no hard maintenance to do.
  2. Package both the JDK6 and the JDK8 version in the same JAR, and create a "Runtime"-type class that's responsible for creating the proper versions of your library's objects at runtime depending on the JDK it's running in. For example:

public abstract class Runtime {
    private static Runtime instance;

    public static Runtime getInstance() {
        if(instance == null) {
            try {
                if(System.getProperty("java.version").startsWith("8"))
                    instance = Class.forName("com.yourcompany.libname.runtime.Runtime8").newInstance();
                else
                    instance = Class.forName("com.yourcompany.libname.runtime.Runtime6").newInstance();
            }
            catch(Exception e) {
                throw new RuntimeException("Could not create runtime", e);
            }
        }
        return instance;
    }

    public abstract Framework getFramework();
}

使用这种方法,您只需要使用反射来加载初始的运行时对象,然后可以将本机Java用于其他所有内容。例如:

Using this approach, you would only need to use reflection to load the initial "Runtime" object, and then you could use native Java for everything else. For example:

 Framework framework=Runtime.getInstance().getFramework();
 ModelObject1 m=framework.newModelObject1();
 // ...

请确保您的 Runtime6 类并不引用JDK8类,即使通过导入来传递也是如此。这种方法的主要缺点是随着时间的推移会跟踪很少的花絮。

Just be SURE your Runtime6 class doesn't refer to a JDK8 class, even transitively via imports. The main drawback to this approach is tracking that little tidbit over time.

我认为您正在做的事情很有趣,我很想知道您如何选择完成它。请保持我们的状态!

I think what you're doing is interesting, and I'm very curious to hear how you choose to get it done. Please keep us posted!

这篇关于在多个JDK下进行开发的最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆