简体   繁体   English

在多个JDK下进行开发的最佳实践

[英]Best practice for developing under multiple JDKs

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. 我有一个正在开发的库,该库具有与JDK 6的目标兼容性。我喜欢利用Java 8功能,例如lambda。 The code and APIs will greatly profit from Java 8. 这些代码和API将极大地受益于Java 8。

I would like to offer versions of the library for Java 6 and Java 8, eg, where a certain part is only available in the Java 8 version. 我想提供Java 6和Java 8库的版本,例如,某些部分仅在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). 现在,有几个核心类可能具有不同的源版本(旧版Java 6版本和新版Java 8版本)。

What is the best practice of doing this? 最佳做法是什么? In Objective-C or C++ I would consider using a preprocessor directive. 在Objective-C或C ++中,我将考虑使用预处理程序指令。

I see a couple of options: 我看到几个选择:

  1. Package different versions of your library, one for JDK6 and one for JDK8. 打包库的不同版本,一个用于JDK6,一个用于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; 如果您使用的是Maven,我想您将只使用3个项目。 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. 一个具有所有公共类的“ commons”项目,一个以commons项目为依赖项并构建JDK6 jar的“ JDK6”项目,然后一个以commons项目为依赖项并构建JDK8 jars的“ JDK8”项目。 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: 将JDK6和JDK8版本打包在同一JAR中,并创建一个“运行时”类型的类,该类负责在运行时根据运行的JDK创建库对象的正确版本。例如:

     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(); } 

    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. 使用这种方法,您只需要使用反射来加载初始的“运行时”对象,然后就可以对其他所有内容使用本机Java。 For example: 例如:

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

    Just be SURE your Runtime6 class doesn't refer to a JDK8 class, even transitively via imports. 只是要Runtime6您的Runtime6类不会引用JDK8类,即使通过导入来传递也是如此。 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! 请随时通知我们!

我认为您可以使用系统属性( http://docs.oracle.com/javase/tutorial/essential/environment/sysprop.html )来发现使用了哪个Java版本,然后可以使应用程序适应运行因此。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM