Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make jenkins trigger a build on a dependent projects when a dependency is build

I have what seems to be a simple question regarding how to setup Jenkins and maven 3.

For the sake of simplicity lets say that we have four projects:

Model which has no dependencies
Commons which depends on Model
Server which depends on Common and Model
Frontend which depends on Model

What I want to achieve is that a success build on Model triggers new build on all projects which in its pom has a dependency on Model (here Common, Server and Frontend)

If Common fails then there is no need to build Server.

In the above case it seems that I can fix this by manually setting up dependent projects on Model and Commons but this information can already be found in the pom files for each project (Server and Frontend) which makes me conclude/wish that Jenkins should be able to figure this out by it self.

Can Jenkins deduce the dependency tree by parsing the pom on each project defined in Jenkins and do some kind of optimal build order / dependency handling automatically or do I have to setup "Block build when upstream project is building" and "Build after other projects are built [List of dependent projects]" as described above ?

I am using Jenkins version 1.473 and Maven 3.
Our maven project pom's do not use the parent or module tag.

/Benjamin

like image 537
Benjamin Avatar asked Aug 29 '12 08:08

Benjamin


3 Answers

Assuming you want to stick with 4 individual builds rather than a multi-module build, then the way to do it is in each of the 4 projects check the box under build triggers

"Build whenever a SNAPSHOT dependency is built"

Then you do a checkin to Model, jenkins starts a build of that project based on SVN polling, when the build completes, it see that common has a dependency on model, and so it triggers a build of common to see if it still builds against the updated model.

The jenkins jobs must be created as maven jobs for this option to be present. It is not applicable to free-style projects, even if they run a script which happens to invoke maven.

like image 106
dan carter Avatar answered Oct 21 '22 02:10

dan carter


The best solution for such thing is to create a multi-module build which results in the following structure:

root (pom.xml)
  +--- model (pom.xml)
  +--- commons (pom.xml)
  +--- server (pom.xml)
  +--- frontend (pom.xml)

In each module for example in commons you define a dependency to the model module etc. The build order will be automatically done by maven.

With such kind of project you can simply go to the root and do:

mvn clean package

You can also use:

mvn --pl --am server

which will build server but also all depending modules automatically.

like image 4
khmarbaise Avatar answered Oct 21 '22 03:10

khmarbaise


Two solutions.

  1. Create a Module pom above the other projects that only declare the build order (model, commons, server and frontend). And just install this project into Jenkins

  2. In each job, there is check box to activate in the form "what is triggering the build" --> build after projects. (The name of the option are probably not exactly what I wrote since my Jenkins is configured in French...)

like image 3
YMomb Avatar answered Oct 21 '22 02:10

YMomb