Jenkins packages the java project into a jar package and copies it to multiple servers

background

I need to package a java maven project into a jar package and copy the jar package to multiple windows servers. It is windows, not Linux.

Implemented functions

1. When the code is pushed to the gitlab warehouse, the Jenkins job will be automatically triggered.

2. Jenkins job will automatically package the maven project into a jar package

3. Copy the jar package to the remote windows server

step details

1. Create a new maven project 

If you cannot find the maven project option, you need to download the plugin Maven Integration plugin

2. Don’t rush to write the job configuration. Configure other tools first.

Go to Manage Jenkins --> Global Tool Configuration --> Maven

I pointed to the maven I installed myself. Of course, you can also choose Jenkins to automatically install maven for you.

去到Manage Jenkins --> Global Tool Configuration --> Maven Configuration

You can write the path inside according to your own situation. You can write it wherever the setting file is placed.

Go to Manage Jenkins --> System Configuration --> Publish over SSH

What is configured here is the server where the file needs to be copied. In other words, which server you want to copy the file to, configure which server here.

If you can't find it, it means you need to install the plug-in Publish Over SSH . If you can't find it and see that others have it, you need to install the plug-in.

I didn’t fill in anything for the Jenkins SSH key. I spent a lot of time Baidu to see how to fill in these options, but I can do it without filling it in. If you can’t, then Baidu how to fill in these options.

The name here of SSH server can be written casually, but what I wrote is the same as the hostname for easy identification. The hostname is the hostname of the server, the username is the account name used to log in to the server, and the remote directory can only be written \, if it is On a Windows machine, I can only write \. I don’t know why. If I write anything else, an error will be reported. If you want to put the file in a specific place, you can read my other blog. https://blog.csdn.net/fenger_c/article/details/116059438 . You can add as many servers as you want, and you can add them as you like.

3. Write the configuration in the maven project you just created

Configure the git warehouse

Configuring webhooks, how to configure it will not be introduced in detail here. Baidu has many. It should be noted that the webhook url written on Jenkins is localhost, but we need to .

The purpose of this step is to automatically trigger this Jenkins job when the code is pushed to the gitlab warehouse.

Packaged into a jar package

Goals and options do not need to be filled in, but I want stability, so I wrote clean install. You can also try to see if leaving it filled out meets your needs.

Copy files to remote machine

在Post-build Actions加Send build artifacts over SSH 

SSH Sever is the server you just configured. Source files refer to the files you want to copy. What I want to copy is the jar package, and the jar package is always in the target directory, so generally writing target/*.jar is correct. of.

For Remove prefix, I wrote target, because in the previous step, Source files and even the target directory were copied, but I don’t want this directory, I just want the jar package, so I fill in target here.

Remote directory is filled with \. I mentioned before that this cannot be changed, but I want to transfer the jar package to another directory, so I wrote a bat script in exec commond. The exec commond option is not necessary. Come according to your needs.

All the functions implemented above have been implemented. After running, the jar package will be placed in the testauto directory of the accessory SSH Sever written by myself.

If you want to put it in another directory, please refer to my other blog: https://blog.csdn.net/fenger_c/article/details/116059438

Guess you like

Origin blog.csdn.net/fenger_c/article/details/116060057