0

I have Git repository in which one repository contains two project as below.

Repository 
   - Project A
   - Project B

My question is how can I build single project (Project A) through Jenkins.

When I use to git clone SSH URL in jenkinsfile it builds to whole repository but I want to build only Project A. So, could you please help me how can I do the same? Please check below my jenkinsfile code:

 node {
      stage ('Build') {
        git url: 'git@github.com:abc/sample.git'
        withMaven {
          sh "mvn clean install"
        } 
      }    
   }
Matt
  • 12,848
  • 2
  • 31
  • 53
Reedima
  • 1
  • 2
  • Welcome, please share your `Jenkinsfile` so we can further help you – Gaël J Jun 20 '21 at 16:22
  • Hi @GaëlJ, I have edit my question with ` jenkinsfile ` code – Reedima Jun 20 '21 at 16:48
  • #1 Does Project A & Project B have its own pom.xml ? #2 are you using a parent pom in which you add your child modules: https://gist.github.com/jrichardsz/a65ece2c96cf291bda0e48bfc76436c5#file-maven-parent-pom-xml ? – JRichardsz Jun 20 '21 at 16:58
  • Yes @JRichardsz Project A & Project B have own pom.xml – Reedima Jun 20 '21 at 17:02
  • #1 Are you using parent pom? #2 Are they independent projects? #3 Are they libraries or apps (web or api) ? – JRichardsz Jun 20 '21 at 17:04
  • @JRichardsz #1. No I m not using parent pom. #2. Yes both are independent project. #3: One project is api and another is web – Reedima Jun 20 '21 at 17:08
  • There are several options but one very easy would be to `sh "cd projectA"` before running Maven – Gaël J Jun 20 '21 at 17:16

1 Answers1

0

Is not a best practice to have several apps or libraries in one git repository. I advise you to use a git repository for each project. Also don't use spaces in sub projects name.

According to your comments, you will be able to compile your artifacts with one of the follow approaches:

Parent pom

As a extremely summary, you could build several maven projects with one hit. In your case you must add one pom.xml at root.

my-git-repository
├── pom.xml
├── project-a
    ├── pom.xml
├── project-b
    ├── pom.xml

This new pom.xml must be something like this:

?xml version="1.0"?>
<project
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
    xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <modelVersion>4.0.0</modelVersion>

    <groupId>acme.foo</groupId>
    <artifactId>bar</artifactId>
    <version>1.0.0</version>
    <packaging>pom</packaging>

    <dependencies>
        ...
    </dependencies>

    <modules>
        <module>../project-a</module>
        <module>../project-b</module>
    </modules>

    <!-- Build -->
    <build>
        ...
    </build>
</project>

Finally, at root execute mvn clean package. As you will see, all the registered project in <modules> will be compiled.

More details about maven submodules here:

dir step

You are using node in your script, so it's not a declarative pipeline in which jenkinsfile is used. You are using a scripted pipeline

No matter if you are using scripted pipeline or declarative pipeline, a step called dir is the solution

node {
  stage('Build') {
    git url: 'git@github.com:abc/sample.git'
    dir("Project-A") {
      withMaven {
        sh "mvn clean package"
      }
    }

    dir("Project-B") {
      withMaven {
        sh "mvn clean package"
      }
    }
  }
}

More details about dir step and jenkins pipelines type here:

JRichardsz
  • 14,356
  • 6
  • 59
  • 94