0

I have gone through the documentation, but still not able to achieve what I am trying to. The requirement is very simple. I have two maven projects. ProjectA and ProjectB. ProjectB requires to reuse some common configs and code from ProjectA. I don't want to just copy and paste them as this is will require to updates if anything changes. So, what are the options now? How can I achieve this?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Imran
  • 1,732
  • 3
  • 21
  • 46

3 Answers3

1

You might have two options.

  • Use your ProjectA has parent of your projectB

    http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0

    <parent>
        <groupId>com.example</groupId>
        <artifactId>projectA</artifactId>
        <version>1.0</version>
    </parent>
    
       <artifactId>projectB</artifactId>
    

    Thus, your projectB will inherit from the first one, with all its dependencies build / dependency management.

  • Use your ProjectA has dependency of your projectB

    http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0

       <artifactId>projectB</artifactId>
    
    <dependencies>
     <dependency>
        <groupId>com.example</groupId>
        <artifactId>projectA</artifactId>
        <version>1.0</version>
        </dependency>
    </dependencies>
    

In this case, projectB wil inherit from projectA all the sources and dependencies.

romainbsl
  • 534
  • 3
  • 10
  • Thank you so much for the info. The first approach did not work. Adding module as dependency worked, but when I run mvn install, it says package com.email.model does not exist, which is available at compile time in intellij. – Imran Aug 06 '17 at 10:58
1

I don't think there is a silver bullet for this but we use combination of these two aproaches:

  1. Multi-module project (best if projects are connected and you want to change common parts a lot): https://maven.apache.org/guides/mini/guide-multiple-modules.html
  2. Extract parts you want to reuse from projectA and make it a standalone artifact which you publish to maven repo (local/private/public - depending on your use case).
jakub.petr
  • 2,951
  • 2
  • 23
  • 34
0

I created a multi module maven project but it was always compiling that the packages not found. Eventually, I figured out the issue here. Maven Multi-module dependency package not found

<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
    <execution>
        <goals>
            <goal>repackage</goal>
        </goals>
        <configuration>
            <classifier>exec</classifier>
        </configuration>
    </execution>
</executions>

Imran
  • 1,732
  • 3
  • 21
  • 46