Build Spring Boot-Maven Project with Jenkins and copy the artifact in to local directory.

Iroshan Aberathne
5 min readJun 6, 2019

--

Here we are going to discuss the way of generation .jar or .war file out of spring boot project with Jenkins from remote repository and copy the final artifact either in .jar or .war extension file into another location by Jenkins.

Pre-requisites

  • Jenkins should be installed in server : http://{ip}:{port}
  • Remote git repository with spring boot source code.

1. Jenkins Login Page

Jenkins-Login

2. Configure Jenkins

  • Configure JDK.
  • Configure Maven.
  • Bit-bucket plugin management.

2.1 Configure JDK

After login to the jenkins go to,

ManageJenkins -> GlobalToolConfiguration -> JDK

  1. Unchecked the Install automatically field.
  2. Enter any name in Name text box as you wish like Jenkin-Java
  3. Enter the JAVA_HOME path of your server location here. In my case JDK has been installed at /usr/lib/jvm/java-8-openjdk-amd64
JDK Config

2.2 Configure Maven

After login to the jenkins go to,

ManageJenkins -> GlobalToolConfiguration -> Maven

  1. Unchecked the Install automatically field.
  2. Enter any name in Name text box as you wish like Jenkin-Maven
  3. Enter the MAVEN_HOME path of your server location here. In my case Maven has been installed at /usr/lib/mvn
Maven Config

2.3 Bit-bucket Plugin Management

After login to the jenkins go to,

ManageJenkins -> ManagePlugins -> Available

  1. Then type “bitbucket” in search box.
  2. You will be seen Bitbucket plugin once you scroll down little bit.
  3. Select the plugin and click on Install without restart button.
Bitbucket plugin select

You will be redirected to following UI and then checked the check box once you see the success message as shown in the screen shot.

Plugin Install done and Jenkin Restart

3. Create New Item/Job

Now we are going to configure our existing spring boot project available in bitbucket repository so that project build automatically.

3.1 Create New Item

After login to the jenkins go to New Item

  1. Enter item name.
  2. Select Maven Project.
  3. Then OK.
Item/ Job Creation

3.2 Configure New Item

After login to the jenkins

  1. You will be seen Adaptor-Service in the dashboard.
  2. Select Configure.
Item Configure

3.3 New Item-General

You can configure general details of the job under the General tab as shown in the image. Note that selecting Discard old builds option is optional

General Details

3.4 New Item-Source Code Management

This is the place where you can manage your source code to generate the final build.

  1. Select the Git option
  2. Then you have to configure git repository url and credential in order to get the source code by jenkins.
Git UI

3.5 New Item Bitbucket Credentials

Enter your bitbucket username in username text box and password in Password text box.

Bitbucker repo credintials

3.5 Bitbucket URL and Credentials

Final stage of the source code management tab should be as follows. Note that you can configure the branch that jenkins going to engage with under the Branch Specifier field. The default one is /master branch but it is better to keep separate branch dedicated for releases. In our case that branch would be /TestRelease

Final state of Source code management tab

3.6 New Item-Build

No need to configure Build Trigger, Build Environments and PreSteps at first place. So leave them with default values.

In the Build tab you can set the Goals and Options of your final build generated by jenkins using bitbucket resource.

Some available options for Goals and Options as follows.

  1. clean package -> clean the project and package as .jar or .war based on the pom.xml file in the project.
  2. clean package -Dmaven.test.skip=true -> Same output will be generated but skip the test cases.
Build file and command

3.7 New Item-Post Step

You can use this place to configure some additional thins to the final output after completion of the build.

As a example I want to move final artifact from /workspace to another directory(location) because by auto deployment process is pointing to that place.

In order to achieve this, you can use custom shellscript or bat to give those instruction as follows.

Custom configuration after build success

4. Build Newly Created Item

Now all the necessary configurations are done. Then navigate to main dashboard after apply and save all the changes in new item configuration.

Select the configured job and click on Build Now.

5. Final output

You can see final output file in configured location (refer 3.7). In our case final output is a .jar file and is available in following location.

Final jar file

Have a Nice Day !!!!!

--

--

Responses (2)