如何在 Jenkins 自动执行 war 个文件?
How do I automatically execute war files at Jenkins?
我将使用Jenkins
自动执行项目。该项目已上传到 github
并使用 Jenkins
成功构建了 War
个文件。 War
文件被移动到 Tomcat 的 Webapps
文件,并且 Tomcat 构建了它,但是 Tomcat 运行s 正确,但是项目没有t 运行 项目。
设置
init_server.sh
echo "> Check the currently running spring server pid "
CURRENT_PID=$(ps -ef | grep java | grep tomcat | awk '{print }')
echo "$CURRENT_PID"
if [ -z $CURRENT_PID ]; then
echo "> No applications are currently running and will not be shut down."
else
echo "> kill -9 $CURRENT_PID"
kill -9 $CURRENT_PID
sleep 10
fi
rm -rf /usr/local/tomcat9/webapps/ji**t /usr/local/tomcat9/webapps/ji**t .war
echo nohup mv /root/jenkinsproject/deploy/*.war /usr/local/tomcat9/webapps/jinheeproject.war >> /root/jenkinsproject/logs/springtestprojectSys.log &
mv /root/jenkinsproject/deploy/*.war /usr/local/tomcat9/webapps/ji**t.war
sleep 10
cd /usr/local/tomcat9/bin
./startup.sh
echo "Execution complete."
application.yml
运行 在 Jenkins 中构建,按照 Shell 文件中的说明,关闭旧的 Tomcat 和 运行 新的 Tomcat。然后我输入 'serverIP:8080',然后 Tomcat 服务器启动。但是如果我输入 'serverIP:8080/helloworld',我会看到 404 错误。
NOTE: I tested my project in the local environment. It was successful.
example: localhost:8080/helloworld
server.xml 在云端服务器
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true" >
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log" suffix=".txt"
pattern="%h %l %u %t "%r" %s %b" />
<Context path="" docBase="ji**t" reloadable="false" />
</Host>
我的卡塔琳娜日志
03-Mar-2020 16:55:23.226 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server version name: Apache Tomcat/9.0.31
03-Mar-2020 16:55:23.229 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server built: Feb 5 2020 19:32:12 UTC
03-Mar-2020 16:55:23.230 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server version number: 9.0.31.0
03-Mar-2020 16:55:23.230 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Name: Linux
03-Mar-2020 16:55:23.230 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Version: 4.4.0-59-generic
03-Mar-2020 16:55:23.230 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Architecture: amd64
03-Mar-2020 16:55:23.230 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Java Home: /usr/lib/jvm/java-8-openjdk-amd64/jre
03-Mar-2020 16:55:23.231 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Version: 1.8.0_242-8u242-b08-0ubuntu3~16.04-b08
03-Mar-2020 16:55:23.231 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Vendor: Private Build
03-Mar-2020 16:55:23.232 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_BASE: /usr/local/tomcat9
03-Mar-2020 16:55:23.232 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_HOME: /usr/local/tomcat9
03-Mar-2020 16:55:23.233 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.config.file=/usr/local/tomcat9/conf/logging.properties
03-Mar-2020 16:55:23.233 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager
03-Mar-2020 16:55:23.233 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djdk.tls.ephemeralDHKeySize=2048
03-Mar-2020 16:55:23.233 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.protocol.handler.pkgs=org.apache.catalina.webresources
03-Mar-2020 16:55:23.234 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dorg.apache.catalina.security.SecurityListener.UMASK=0027
03-Mar-2020 16:55:23.234 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dignore.endorsed.dirs=
03-Mar-2020 16:55:23.234 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.base=/usr/local/tomcat9
03-Mar-2020 16:55:23.234 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.home=/usr/local/tomcat9
03-Mar-2020 16:55:23.235 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.io.tmpdir=/usr/local/tomcat9/temp
03-Mar-2020 16:55:23.235 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [/usr/java/packages/lib/amd64:/usr/lib/x86_64-linux-gnu/jni:/lib/x86_64-linux-gnu:/usr/lib/x86_64-linux-gnu:/usr/lib/jni:/lib:/usr/lib]
03-Mar-2020 16:55:23.451 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["http-nio-8080"]
03-Mar-2020 16:55:23.498 INFO [main] org.apache.catalina.startup.Catalina.load Server initialization in [518] milliseconds
03-Mar-2020 16:55:23.528 INFO [main] org.apache.catalina.core.StandardService.startInternal Starting service [Catalina]
03-Mar-2020 16:55:23.528 INFO [main] org.apache.catalina.core.StandardEngine.startInternal Starting Servlet engine: [Apache Tomcat/9.0.31]
03-Mar-2020 16:55:27.202 INFO [main] org.apache.jasper.servlet.TldScanner.scanJars At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
03-Mar-2020 16:55:27.296 INFO [main] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [/usr/local/tomcat9/webapps/host-manager]
03-Mar-2020 16:55:27.328 INFO [main] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/usr/local/tomcat9/webapps/host-manager] has finished in [31] ms
03-Mar-2020 16:55:27.328 INFO [main] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [/usr/local/tomcat9/webapps/manager]
03-Mar-2020 16:55:27.352 INFO [main] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/usr/local/tomcat9/webapps/manager] has finished in [24] ms
03-Mar-2020 16:55:27.352 INFO [main] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [/usr/local/tomcat9/webapps/ROOTBEFORE]
03-Mar-2020 16:55:27.365 INFO [main] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/usr/local/tomcat9/webapps/ROOTBEFORE] has finished in [12] ms
03-Mar-2020 16:55:27.365 INFO [main] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [/usr/local/tomcat9/webapps/docs]
03-Mar-2020 16:55:27.379 INFO [main] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/usr/local/tomcat9/webapps/docs] has finished in [14] ms
03-Mar-2020 16:55:27.383 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["http-nio-8080"]
03-Mar-2020 16:55:27.391 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in [3,892] milliseconds
我缺少了一些东西。忘记继承SpringBootServletInitializer设置了
application.java
@SpringBootApplication
public class Application extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
我将使用Jenkins
自动执行项目。该项目已上传到 github
并使用 Jenkins
成功构建了 War
个文件。 War
文件被移动到 Tomcat 的 Webapps
文件,并且 Tomcat 构建了它,但是 Tomcat 运行s 正确,但是项目没有t 运行 项目。
设置
init_server.sh
echo "> Check the currently running spring server pid "
CURRENT_PID=$(ps -ef | grep java | grep tomcat | awk '{print }')
echo "$CURRENT_PID"
if [ -z $CURRENT_PID ]; then
echo "> No applications are currently running and will not be shut down."
else
echo "> kill -9 $CURRENT_PID"
kill -9 $CURRENT_PID
sleep 10
fi
rm -rf /usr/local/tomcat9/webapps/ji**t /usr/local/tomcat9/webapps/ji**t .war
echo nohup mv /root/jenkinsproject/deploy/*.war /usr/local/tomcat9/webapps/jinheeproject.war >> /root/jenkinsproject/logs/springtestprojectSys.log &
mv /root/jenkinsproject/deploy/*.war /usr/local/tomcat9/webapps/ji**t.war
sleep 10
cd /usr/local/tomcat9/bin
./startup.sh
echo "Execution complete."
application.yml
运行 在 Jenkins 中构建,按照 Shell 文件中的说明,关闭旧的 Tomcat 和 运行 新的 Tomcat。然后我输入 'serverIP:8080',然后 Tomcat 服务器启动。但是如果我输入 'serverIP:8080/helloworld',我会看到 404 错误。
NOTE: I tested my project in the local environment. It was successful.
example: localhost:8080/helloworld
server.xml 在云端服务器
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true" >
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log" suffix=".txt"
pattern="%h %l %u %t "%r" %s %b" />
<Context path="" docBase="ji**t" reloadable="false" />
</Host>
我的卡塔琳娜日志
03-Mar-2020 16:55:23.226 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server version name: Apache Tomcat/9.0.31
03-Mar-2020 16:55:23.229 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server built: Feb 5 2020 19:32:12 UTC
03-Mar-2020 16:55:23.230 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server version number: 9.0.31.0
03-Mar-2020 16:55:23.230 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Name: Linux
03-Mar-2020 16:55:23.230 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Version: 4.4.0-59-generic
03-Mar-2020 16:55:23.230 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Architecture: amd64
03-Mar-2020 16:55:23.230 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Java Home: /usr/lib/jvm/java-8-openjdk-amd64/jre
03-Mar-2020 16:55:23.231 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Version: 1.8.0_242-8u242-b08-0ubuntu3~16.04-b08
03-Mar-2020 16:55:23.231 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Vendor: Private Build
03-Mar-2020 16:55:23.232 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_BASE: /usr/local/tomcat9
03-Mar-2020 16:55:23.232 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_HOME: /usr/local/tomcat9
03-Mar-2020 16:55:23.233 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.config.file=/usr/local/tomcat9/conf/logging.properties
03-Mar-2020 16:55:23.233 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager
03-Mar-2020 16:55:23.233 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djdk.tls.ephemeralDHKeySize=2048
03-Mar-2020 16:55:23.233 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.protocol.handler.pkgs=org.apache.catalina.webresources
03-Mar-2020 16:55:23.234 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dorg.apache.catalina.security.SecurityListener.UMASK=0027
03-Mar-2020 16:55:23.234 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dignore.endorsed.dirs=
03-Mar-2020 16:55:23.234 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.base=/usr/local/tomcat9
03-Mar-2020 16:55:23.234 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.home=/usr/local/tomcat9
03-Mar-2020 16:55:23.235 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.io.tmpdir=/usr/local/tomcat9/temp
03-Mar-2020 16:55:23.235 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [/usr/java/packages/lib/amd64:/usr/lib/x86_64-linux-gnu/jni:/lib/x86_64-linux-gnu:/usr/lib/x86_64-linux-gnu:/usr/lib/jni:/lib:/usr/lib]
03-Mar-2020 16:55:23.451 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["http-nio-8080"]
03-Mar-2020 16:55:23.498 INFO [main] org.apache.catalina.startup.Catalina.load Server initialization in [518] milliseconds
03-Mar-2020 16:55:23.528 INFO [main] org.apache.catalina.core.StandardService.startInternal Starting service [Catalina]
03-Mar-2020 16:55:23.528 INFO [main] org.apache.catalina.core.StandardEngine.startInternal Starting Servlet engine: [Apache Tomcat/9.0.31]
03-Mar-2020 16:55:27.202 INFO [main] org.apache.jasper.servlet.TldScanner.scanJars At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
03-Mar-2020 16:55:27.296 INFO [main] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [/usr/local/tomcat9/webapps/host-manager]
03-Mar-2020 16:55:27.328 INFO [main] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/usr/local/tomcat9/webapps/host-manager] has finished in [31] ms
03-Mar-2020 16:55:27.328 INFO [main] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [/usr/local/tomcat9/webapps/manager]
03-Mar-2020 16:55:27.352 INFO [main] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/usr/local/tomcat9/webapps/manager] has finished in [24] ms
03-Mar-2020 16:55:27.352 INFO [main] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [/usr/local/tomcat9/webapps/ROOTBEFORE]
03-Mar-2020 16:55:27.365 INFO [main] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/usr/local/tomcat9/webapps/ROOTBEFORE] has finished in [12] ms
03-Mar-2020 16:55:27.365 INFO [main] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [/usr/local/tomcat9/webapps/docs]
03-Mar-2020 16:55:27.379 INFO [main] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/usr/local/tomcat9/webapps/docs] has finished in [14] ms
03-Mar-2020 16:55:27.383 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["http-nio-8080"]
03-Mar-2020 16:55:27.391 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in [3,892] milliseconds
我缺少了一些东西。忘记继承SpringBootServletInitializer设置了
application.java
@SpringBootApplication
public class Application extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}