简体   繁体   中英

Spring boot multi module maven projects without repository

I have multiple spring boot projects, every project is independent and has its own CICD pipeline.

These projects need some common classes, I have created a separate project with common classes.

Parent
   pom.xml (with packaging)
   lib-project
      pom.xml
   project-1
      pom.xml
   project-2
      pom.xml
    

I can build project easily from the parent directory, it builds all the projects

parent$ mvn clean package

it generates all the jar files and put them in their respective target folder of projects

My problem is I can't initiate this at the parent level, this has to be initiated from within each project from its own pipeline.

and

I cannot use any local or remote repository, to put the dependent jar in m2 using mvn clean install and then refer to it as dependency

I want to build it from the relavent project directory

parent/project-1$ mvn clean package

it shows following error:

Could not resolve dependencies for project com.test.multiple:project-1:jar:0.0.1-SNAPSHOT: Could not find artifact com.test.multiple:lib-project:jar:0.0.1-SNAPSHOT

My expectation stepwise on compilation of project-1

  1. Check if there is a dependency for project-1
  2. Go to../lib-project
  3. Compile and build it in target folder (or anywhere relative to our project)
  4. Add this generated jar to "project-1" dependency
  5. Compile and build the "project-1" jar file.

Parent Pom Configurations

<project ...>
   <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.3</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>com.test.multiple</groupId>  
    <artifactId>parent</artifactId>  
    <version>0.0.1-SNAPSHOT</version>  

    <packaging>pom</packaging>
    
    <modules>
        <module>lib-project</module>
        <module>project-1</module>
    </modules>
</project>

** Lib project pom **

<project ...>
    
    <parent>  
        <groupId>com.test.multiple</groupId>  
        <artifactId>parent</artifactId>  
        <version>0.0.1-SNAPSHOT</version>  
    </parent>
    
    <groupId>com.test.multiple</groupId>
    <artifactId>lib-project</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>lib-project</name>
    <properties>
        <java.version>11</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

Project-1 pom

<project ...>
    
    <parent>  
        <groupId>com.test.multiple</groupId>  
        <artifactId>parent</artifactId>  
        <version>0.0.1-SNAPSHOT</version>  
    </parent>
    
    <groupId>com.test.multiple</groupId>
    <artifactId>project-1</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>project-1</name>
    <properties>
        <java.version>11</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        
        <dependency>
            <groupId>com.test.multiple</groupId>
            <artifactId>lib-project</artifactId>
            <version>${project.version}</version>
        </dependency>
    </dependencies>
    
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

I have multiple spring boot projects, every project is independent and has its own CICD pipeline.

These projects need some common classes, I have created a separate project with common classes.

Congratulations, your projects are not independent any more!

Given the definitions above, here are the dependencies:

  • lib-project depends on parent ;
  • project-1 depends on parent ;
  • project-1 depends on lib-project .

Please check Introduction to the POM and Guide to Working with Multiple Modules for the discussion on the dependencies in Maven projects.


I cannot use any local or remote repository, to put the dependent jar in m2 using mvn clean install and then refer to it as dependency

Given this limitation, and dependencies listed above, the POMs and source files of all the projects have to be present on the disk for build purposes. The build process has to start from the parent folder. The build process has to build all modules at once.

Also, please consider using mvn clean verify instead of mvn clean install to avoid populating the local repository with the artifacts you are building.

A maven project isn't designed to build its dependencies on demand. However, jenkins can be configured to build downstream projects when changes are pushed to an upstream dependency.

I have also worked around this by using the -pl option on a parent pom in the relevant jenkinsfile to build a subset of the child projects

Jenkinsfile

clone parent project
mvn clean package -pl core,deployable

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM