简体   繁体   中英

best way extend an existing java/maven project

I'm extending an existing java app, with both pure java modules and a war. For the war, I'm using the maven-war-overlay method, which automatically includes my own extension files over the existing files if they're named the same.

Now for the java module, I was thinking that I could extend this separately, but what's the best way? I have created a new java module project, and included the original project jar as a dependency. Is there a way to change existing properties/methods in that jar without creating subclasses? if i subclass, the existing class won't know about it...

I could exclude the files from the first project, but that will get a little cumbersome, there are many files that need small changes (different hibernate annotations for instance).

What's the best way to leave the original source code alone, but make the changes I need?

Technically, you could open the contents of the original JAR, compile new sources and assemble a new JAR replacing old sources with the new ones. However, this would be very bad practice and very error-prone, too. You risk wasting a lot of time and ending up with a mess of a project.

It is much better to modify the classes of the original project to allow replacing implementations of the classes. Having an IOC container in the project could make this process easy. You can do it without the container, too, like so:

class WantToOverride { ... }

class UserOfWantToOverride {
  WantToOverride wantToOverride;

  public void setWantToOverride(WantToOverride wantToOverride) {
     this.wantToOverride= wantToOverride;
  }
}

Now, during your project initialization, call

userOfWantToOverride.setWantToOverride(new OverridingClass(...))

, where your class

class OverridingClass extends WantToOverride { ... }

You can also inherit Hibernate classes but you need to carefully take care of Hibernate Inheritance

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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