Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Build only affected modules in Jenkins / Maven

We are using Git, Jenkins and Artifactory as CI/CD pipeline. We have a large code base and each time the full build takes a lot of time to process. For example, we have a aggregated pom project containing 100+ modules. The parent pom.xml file looks like the following:

<project>
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.sample</groupId>
    <artifactId>project</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>pom</packaging>

    <modules>
        <module>module-1</module>
        <module>module-2</module>
        <module>module-n</module>
    </modules>
</project>

We use this structure because some modules depend on some others. Also, we want to centralize the common dependencies, dependenciesManagement & pluginManagement in this parent pom file instead of maintaining 100+ pom files per module.

Because changes may not happen on every modules, we want to speed up the build process by only building the affected modules since last successful build. This can be done by maven.

Example: mvn clean install -pl module1,module2,module3 -amd -am

It is easy if the developer knows what modules has been changed. If not, some maven plugin can help the developer to detect the uncommitted changes compared with git repository. Or some people may do mvn install -amd -pl $(git status | grep -E "modified:|deleted:|added:" | awk '{print $2}' | cut -f1 -d"/")

But these solutions do not fit to our Jenkins pipeline. The Jenkins server always pull the latest version of the project from git and start the build. I don't know if Jenkins has the capability to detect what modules has been changed from git history and from its build history (any state info?). Besides, we are running multiple Jenkins slaves process. The build job is not always stick to the same machine workspace.

I searched one Jenkins plugin seem can solve my problem. https://wiki.jenkins.io/display/JENKINS/Maven+Project+Plugin

I tried to install a fresh latest version of Jenkins with this plugin but still unable to find the feature "Incremental build - only build changed modules".

Could somebody suggest some ways (by plugin or manuel scripting) to help to build only changed modules in Jenkins? I have been searching for many days and still could not find a workable solution. I would be very grateful if somebody could give me some insight or direction how we should do it. Thanks a lot!

like image 832
Frankie Hung Avatar asked Jun 26 '18 07:06

Frankie Hung


People also ask

How to install Maven in Jenkins?

On the ‘Build Executor Status’, click the progress bar. You will notice Jenkins install Maven. Check your project. The output should look something as shown below. After the project is built, you can navigate to project page->Modules link on the left.

How to build Java project with Maven?

To build Java project with maven we need to install some plugins like, Now search for “GitHub Integration Plugin” and click on Download now and install after restart. once the plugins are downloaded successfully. So now click on Restart Jenkins checkbox. #3. Global Tool configuration in Jenkins

How to enter Project details in Jenkins pipeline?

Enter Project details in Jenkins Pipeline Provide some description for the newly created job in General Section, Click on GitHub project and provide the GitHub URL. you can use Sample Java Project on GitHub with jenkinsfile Now we will set the Build Triggers, Select Build whenever a SNAPSHOT and GitHub hook trigger for GITScm polling as triggers.

How do I change the default branch in Jenkins?

By default Jenkins provide branch as Master but you can change it to dev, main, or to whatever your branch is. (if you don’t know you can check the branch by going to your GitHub repository OR by running git branch command on your shell).


1 Answers

I know this is an old question but if anyone is still looking for an answer, I created a Jenkinsfile for a Multi Branch Pipeline which builds affected modules only for a maven multi-module project.

Jenkinsfile

like image 149
Ryan Guamos Avatar answered Sep 18 '22 15:09

Ryan Guamos