Mark VY
Mark VY

Reputation: 1673

maven enforcer enforce class path order

Suppose I have a maven module M which declares a direct dependency on modules X and Y. I want the classes from X to come before classes from Y in the class path. Is there any easy way to add a rule in maven enforcer to ensure that if I don't accidentally swap the order sometime in the future?

The question itself is now complete. Everything below this line is me trying to explain why I want this.

We have a multi-module maven project. Most modules depend on protobuf-java:2.4.1 but some depend on 2.5 instead of 2.4. The ones that depend on 2.5 also depend on those that need 2.4. Sadly, the protobuf maintainers at Google made a big mistake, which they claim they will not repeat again. The mistake was that they decided that breaking ABI is okay, as long they don't break API.

In particular, in version 2.4 some methods in the class TextFormat take an argument of type Message whereas in 2.5 they take an argument of type MessageOrBuilder which is more general. This means that if 2.5 is on the runtime class path, any module compiled against 2.4 will crash with NoSuchMethodError if it tries to call such a method.

My solution was to create another module which contains my own copy of TextFormat.java (and doesn't contain anything else). My copy adds back the methods that take Message which were removed in 2.5. I'm adding that module as a runtime dependency to those modules which need 2.5, so as "simulate" a version of 2.5 which is backwards-compatible with 2.4.

I want to add a maven enforcer rule to ensure that this hacky "compat" module always comes before the official module in the class path, so that my fix gets picked up.

Upvotes: 0

Views: 141

Answers (0)

Related Questions