Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - make a library and import optional

I have a library that I'm using in an Java application - it's important for certain functionality, but it's optional. Meaning that if the JAR file is not there, the program continues on without issue. I'd like to open source my program, but I can not include this library, which is necessary to compile the source code as I have numerous import statements to use the API. I don't want to maintain two code sets. What is the best way to remove the physical jar file from open source release, but still maintain the code to support it where other people could still compile it?

like image 251
ElJeffe Avatar asked Jan 14 '23 06:01

ElJeffe


1 Answers

the typical approach taken is to define the wrapper API (i.e. interfaces) and include those interfaces in the open sourced code, and then provide configuration options where one can specify class names of classes that implement certain interfaces.

You will import API interfaces instead of importing classes directly into your open sourced code. This way, you are open sourcing the API but not the implementation of the parts that you do not want to open source or you cannot open source.

There are many examples, but take a look at JDBC API (interfaces) and JDBC drivers (implementation classes) for starters.

like image 150
smallworld Avatar answered Jan 16 '23 17:01

smallworld