简体   繁体   English

Java条件编译同时支持1.4 / 1.6

[英]Java conditional compilation to support 1.4/1.6 simultaneously

I'm working on a large legacy codebase that currently only compiles with java 1.4. 我正在研究一个目前只能用java 1.4编译的大型遗留代码库。 One of the things that I need to do is to get it working with 1.6 (well probably 1.7 now). 我需要做的一件事就是让它与1.6一起使用(现在可能是1.7)。

The head build doesn't currently compile with 1.6. 头部版本目前不用1.6编译。 This is for lots of reasons - most are easy to fix, like using enum as a keyword, but we're struggling with Oracle/Sun updating the JDBC (Connection interface) to support types that are not available in java 1.4. 这有很多原因 - 大多数都很容易修复,比如使用enum作为关键字,但我们正在努力使用Oracle / Sun更新JDBC(连接接口)以支持java 1.4中不可用的类型。 This means if I make the changes to work with 1.6, the main production builds break as classes like NClob break as they aren't in the 1.4 release; 这意味着如果我使用1.6进行更改,主要的生产构建会像NClob这样的类中断,因为它们不在1.4版本中; if I don't make the changes, I can't compile with the 1.6 compiler. 如果我不进行更改,我无法使用1.6编译器进行编译。

Are there any patterns to support conditional compilation/build in java? 是否有任何模式支持java中的条件编译/构建? My only plan I've thus far come up with was to fiddle with the ant build to conditionally swap in/out classes depending on the build. 到目前为止,我唯一的计划就是根据构建来调整ant构建以有条件地交换进/出类。 This feels pretty horrible, hence asking the community here for thoughts. 这感觉非常可怕,因此在这里向社区征求意见。

Again, the boundaries on the problem are: 同样,问题的边界是:

  • Need to be able to continue to compile HEAD on 1.4 (no 1.6 with 1.4 compatibility mode I'm afraid) 需要能够继续在1.4上编译HEAD (我担心没有1.6兼容1.4模式)
  • Also need a separate head build that compiles with 1.6 - the assumption is that this will take some time (as it's a large codebase), hence the first bullet point to allow others to continue to work and deliver other changes while we prepare the head build for 1.6 compatibility. 还需要一个单独的头部构建,用1.6编译 - 假设这需要一些时间(因为它是一个大的代码库),因此第一个要点是允许其他人继续工作并在我们准备头部构建时提供其他更改1.6兼容性。
  • it's one massive code tree; 它是一个巨大的代码树; this means none of our code is a library dependancy, and we can't easily make it so (remember: legacy code base :( ) 这意味着我们的代码都不是库依赖,我们不能轻易做到(请记住:遗留代码库:(
  • We don't allow branching (for reason's I won't go into unless I Really have to) 我们不允许分支(因为除非我真的需要,否则我不会进入)

Many Thanks in advance. 提前谢谢了。

This is why makefiles/pom.xml/build.xml get checked in. Use your revision control system to create a branch. 这就是检查makefiles / pom.xml / build.xml的原因。使用修订控制系统创建分支。 In one branch, make all the changes needed for 1.6 compatibility. 在一个分支中,进行1.6兼容性所需的所有更改。 In the other branches, don't. 在其他分支机构,不要。 Any given branch will either safely compile for 1.4 or safely compile for 1.6, with no frankenbranches that try to do both at once, which is a Bad Idea(tm). 任何给定的分支将安全地编译为1.4或安全编译1.6,没有frankenbranches尝试同时执行这两个,这是一个坏主意(tm)。

Make a port14.jar and port16.jar with same classes, and same methods, but different implementations. 使用相同的类和相同的方法创建port14.jarport16.jar ,但实现方式不同。 And make two separate builds. 并制作两个单独的版本。

This means double work, but you can use the same code basis at the same time. 这意味着双重工作,但您可以同时使用相同的代码基础。 And the differences are relative "minor." 差异是相对“次要的”。

I use Maven and I would have module which defines the interfaces that both versions share. 我使用Maven,我会有模块,它定义了两个版本共享的接口。 Then you have two modules, one with the 1.4 code and one with the version Java 6 (or 7 or 8). 然后你有两个模块,一个是1.4代码,另一个是Java 6(或7或8)。 This way you can build them both and select the appropriate implementation at runtime. 这样您就可以构建它们并在运行时选择适当的实现。

There is a previous SO answer that covers this, essentially using ant's replace task to make a quick and dirty version of IFDEF blocks. 之前的SO答案涵盖了这一点,主要是使用ant的replace任务来制作快速而脏的IFDEF块版本。 That would probably be easier than swapping out whole class files, and would make it easier to go full 1.6 whenever you can (just remove all of the 1.4 IFDEF blocks) 这可能比交换整个类文件更容易,并且可以让你更容易完全1.6(只需删除所有1.4 IFDEF块)

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

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