Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Importing only a few class from a dependency

I'm developing a modular web-app where I'm using maven. The skeleton of my application is the following:

enter image description here

The skeleton of my application with the share-services module

enter image description here

Now I want to import some classes of Service-App1 within Service-App2 but without the Service-App1 dependencies( like Module-App1, Util-App1 and the external dependencies).

Summarazing, since in the App2 I have a composite persistence unit Composite Persistence Unit and I want to share some business logic between applications. For example if in App1 I have a spring service like this:

public interface ContractService {

    void processSomethings();

}

@Service
public class ContractServiceImpl implements ContractService  {

    public void processSomethings(){
            //Manage some share entities

    }


}

I would want use the method

processSomethings()

also in App1 How can I realize this?

like image 968
Skizzo Avatar asked Oct 21 '22 06:10

Skizzo


1 Answers

You need to create a project (jar output) that contains just the classes that are shared between the two consumers. This may seem like an overhead, but it leads to clean well-organised code. If you can't do this, it is an indication that you have a confused dependency hierarchy. I do this regularly to implement web service clients and services, with a shared jar that contains the classes in common (i.e. the client should know nothing about DAO classes, but does need to know the signature of the web service operations. It also needs to have access to the POJO classes that define the data and message structure).

Also, do you need the hamcrest dependency at runtime? My experience is that it can usually have its scope set to test, so it never turns up in your compiled artifacts.

I'm not a convert to parent poms, and certainly would not consider the two-level parent pom structure you show. My objections are:

  • If you update a parent pom, you then need to update all the child poms that use it, negating the advantage of centralising your dependency information.
  • Putting module declarations in a parent pom seems to produce a circular reference. If you change the parent pom, it tries to compile the modules, which immediately refer back to the parent, which is not yet available in the repository as it hasen't finished building. I've seen systems where there is a "dependency parent" and a "compile all modules parent" to work around this. I do use parent poms sparingly, but only to set the compiler version and to define the versions of external dependencies like Spring and Apache commons jars.

I'd like to hear reasoned argument on this topic.

like image 61
kiwiron Avatar answered Oct 24 '22 10:10

kiwiron