Run your Java project on Jenkins with a bat file

Yunus Emre YILDIZ
3 min readFeb 16, 2023

--

Let’s think that you have a Java test project on your local, you are not using Git and you store your demo project on your local, you use TestNG but also you need to use Jenkins too.

In cases like this, we have a proper solution.
“Preparing a .bat file”

We all know what is a bat file. A bat file is a type of script file used in Windows to automate tasks and run commands. It contains a series of commands that are executed by the command prompt (cmd.exe).

Why do we prepare a .bat file to run our code on Jenkins?
Because we have the option to build a project on Jenkins for cases like this, and it’s “Execute Windows batch command”

We have a Java project, test cases that we wrote with TestNG. If we want to run this project from our local Windows machine, In such a case, we create a bat file and put this file in the main directory of the project.

In this example, AppTest is our test class. As you can see, we created a bat file in the main directory.

So we create this batch file to run our Java test project. First, we set our project location into the variable. With the cd command we can go to the file directory that we set before, then with the mvn compile test, we can run our test project.

set projectLocation=C:\Users\yunusemre\IdeaProjects\batTest
cd %projectLocation%
mvn compile test
pause

Now, let’s create a new project on Jenkins.

We can choose the freestyle project option, and after that, we can configure our project.

We should select the “Use custom workspace” option from the advanced configuration options and set our project directory there.

Since we have specified the project’s path, it means that we will be able to access the bat file as well. After accessing the file, we need to give the name of our bat file, which is in the main directory of our project, in the Build Step section.

After saving the changes, we can build successfully.

Where can we use this?

This is how to save test reports with another project that using .bat

We can further expand the usage of bat files with an example.

Let’s say we have a test project and we run it through Jenkins at certain intervals. It also generates a report every time we run it.
We want to stock these reports in a simple path for the directory.
In such a case, we can create a test project like the example above and write a method that copies files into it. We add this bat file to our main file as a downstream project. This means that our test project with the bat file will start working after our main project is running. This project will copy and stock the report as we wish.

--

--