Imran
Imran

Reputation: 1902

Intellij maven module inheritence

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?

Upvotes: 0

Views: 50

Answers (3)

Imran
Imran

Reputation: 1902

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>

Upvotes: 0

jakub.petr
jakub.petr

Reputation: 3031

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).

Upvotes: 1

romainbsl
romainbsl

Reputation: 544

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.

Upvotes: 1

Related Questions