How do you use Maven to share source code for two projects?

后端 未结 3 1689
陌清茗
陌清茗 2021-02-13 04:13

I have a large Java Web Application project using Maven and I need to start a new project that will share most of the same code (so I don\'t have to repeat work), but not all of

3条回答
  •  离开以前
    2021-02-13 04:53

    Create a maven project which contains all your shared code. Keep packaging of this project (in the main pom.xml) as jar. This would help make this project kind of library for your usage.

    In all the projects which access the shared code, add dependency for this project according to your needs. (compile, provided).

    Now package and install the shared project before you build any of the dependent projects. This will add the shared project to your local repository which can be then used by your dependent projects.

    Adding sample pom.xml for shared and dependent projects.

    Shared project pom.

      
      4.0.0  
        
        com.myspace.test  
        com.myspace  
        0.0.1-SNAPSHOT  
        
      com.myspace  
      shared  
      0.0.1-SNAPSHOT  
      shared-module  
      shared module which contains code shared by other modules.  
    
    

    Dependent project's pom.

      
      4.0.0  
        
        com.myspace.test  
        com.myspace  
        0.0.1-SNAPSHOT  
        
      com.myspace  
      dependent-module  
      0.0.1-SNAPSHOT  
      dependent-module  
      Dependent module.  
        
          
          com.myspace  
          shared  
          0.0.1-SNAPSHOT  
          provided  
          
        
    
    

    Parent project can be added optionally in case such organization is required. Hope this helps.

提交回复
热议问题