Jenkins Deploying Java Applications to Tomcat Server

For Java programs, Jenkins requires the use of build tools such as maven, ant, etc., with maven being more popular. Here we use maven to implement the deployment of Java applications.

Prepare Java code in GitLab

gitlab

Deploy Tomcat and configure it

apt-get -y install tomcat9 tomcat9-admin
cp -r /usr/share/tomcat9-admin/* /var/lib/tomcat9/webapps/

Open /var/lib/tomcat9/conf/tomcat-users.xml and do the following configuration.

Add the following three lines to create users and authorize them. manager-script for remote script execution privileges
<role rolename="manager-gui"/>
<role rolename="manager-script"/>
<user username="tomcat" password="tomcat" roles="manager-gui,manager-script"/>

Open /var/lib/tomcat9/webapps/manager/META-INF/context.xml and make the following configuration.

Comment the following line. By default, only 127.0.0.1 can be accessed locally, comment it out so that it can be accessed remotely
<! -- 
 <Valve className="org.apache.catalina.valves.RemoteAddrValve"
 allow="127\. \d+\. \d+\. \d+|::1|0:0:0:0:0:0:0:0:1" />
-->

Restart tomcat9 afterwards

Installing maven and tomcat plugins

Maven plugin can implement maven style tasks maven

Deploy to container plugin to connect to tomcat deploy

Jenkins server to install maven and configure image acceleration

apt-get -y install maven

Open the /etc/maven/settings.xml file and do the following configuration.

#Add the following between <mirrors>...</mirrors>
<mirror>
 <id>nexus-aliyun</id>
 <mirrorOf>*</mirrorOf>
 <name>Nexus aliyun</name>
 <url>http://maven.aliyun.com/nexus/content/groups/public</url
</mirror>

Viewing the values of the relevant variables

variables

Jenkins global tools to configure jdk and maven

configure1

configure2

configure3

Creating global credentials for tomcat

Create users and privileges for jenkins to connect to tomcat according to the user privileges configuration of tomcat

credentials

Create task

Enter maven’s build option clean package -Dmaven.test.skip=true

task

Build verification

verification