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

后端 未结 3 1695
陌清茗
陌清茗 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.

    <project xmlns="http://maven.apache.org/POM/4.0.0"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">  
      <modelVersion>4.0.0</modelVersion>  
      <parent>  
        <artifactId>com.myspace.test</artifactId>  
        <groupId>com.myspace</groupId>  
        <version>0.0.1-SNAPSHOT</version>  
      </parent>  
      <groupId>com.myspace</groupId>  
      <artifactId>shared</artifactId>  
      <version>0.0.1-SNAPSHOT</version>  
      <name>shared-module</name>  
      <description>shared module which contains code shared by other modules.</description>  
    </project>
    

    Dependent project's pom.

    <project xmlns="http://maven.apache.org/POM/4.0.0"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">  
      <modelVersion>4.0.0</modelVersion>  
      <parent>  
        <artifactId>com.myspace.test</artifactId>  
        <groupId>com.myspace</groupId>  
        <version>0.0.1-SNAPSHOT</version>  
      </parent>  
      <groupId>com.myspace</groupId>  
      <artifactId>dependent-module</artifactId>  
      <version>0.0.1-SNAPSHOT</version>  
      <name>dependent-module</name>  
      <description>Dependent module.</description>  
      <dependencies>  
        <dependency>  
          <groupId>com.myspace</groupId>  
          <artifactId>shared</artifactId>  
          <version>0.0.1-SNAPSHOT</version>  
          <scope>provided</scope>  
        </dependency>  
      </dependencies>  
    </project>
    

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

    0 讨论(0)
  • 2021-02-13 05:00

    Install the shared code, using your current packaging setting of jar:

    mvn install
    

    Then, you can create a dependency in the child pom.xml (the code that is not shared) from whatever repository you are installing to.

    This is good practice in general not only to avoid repeating work but also in case you want to change the implementation of the shared resources. You can change the logic in one place, install it to the repository, and other projects that depend on that code will use the new code the next time they are compiled.

    0 讨论(0)
  • 2021-02-13 05:09

    You should refactor your projects.

    1. Identify the common code
    2. Extract that into its own maven module
      2.1. usually web-apps are multi module, so if you are going to share the common library across two web-apps, then separate the common library out into its own group-id
    3. Build and install the jar file into your repository
    4. change the poms of the web-apps to depend on your new library
    0 讨论(0)
提交回复
热议问题