# Jenkins自动构建部署maven项目的WEB项目

## 参考文档：<https://www.jianshu.com/p/60fb231a160e>

* **Jenkins安装过程略，参考**[**Jenkins环境构建**  ](https://tuonioooo-notebook.gitbook.io/engineering-management/jenkins-ji-cheng/jenkinshuan-jing-gou-jian)
* **配置系统工具**

![](https://324204342-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Ltduzp_tx6Kzyq5YwdK%2F-Ltdv0j8Ne-IlZchzDgP%2F-Ltdv6q10COLfbE_0ygW%2Fimport-jenkins-21.png?generation=1573735138271005\&alt=media)

配置maven配置文件地址，就在maven的conf目录下的settings.xml文件

![](https://324204342-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Ltduzp_tx6Kzyq5YwdK%2F-Ltdv0j8Ne-IlZchzDgP%2F-Ltdv6q35eelOS7k65Eg%2Fimport-jenkins-22.png?generation=1573735138566499\&alt=media)

配置JDK位置

![](https://324204342-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Ltduzp_tx6Kzyq5YwdK%2F-Ltdv0j8Ne-IlZchzDgP%2F-Ltdv6q5-Z6B7jet4Lqi%2Fimport-jenkins-23.png?generation=1573735138467986\&alt=media)

配置git位置，git是安装的，可以使用下述命令获取git位置

![](https://324204342-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Ltduzp_tx6Kzyq5YwdK%2F-Ltdv0j8Ne-IlZchzDgP%2F-Ltdv6q7B2uTCQgRV4tC%2Fimport-jenkins-24.png?generation=1573735138142758\&alt=media)

配置maven位置

![](https://324204342-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Ltduzp_tx6Kzyq5YwdK%2F-Ltdv0j8Ne-IlZchzDgP%2F-Ltdv6q9HDLZ-9U4OCaf%2Fimport-jenkins-25.png?generation=1573735141888551\&alt=media)

* **安装插件**

目标是将项目打包后发布到tomcat中去，所以现在我们需要安装一个发布到tomcat的jenkins插件点击系统管理，选择管理插件

![](https://324204342-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Ltduzp_tx6Kzyq5YwdK%2F-Ltdv0j8Ne-IlZchzDgP%2F-Ltdv6qBZ74XPaOkdudt%2Fimport-jenkins-26.png?generation=1573735137891051\&alt=media)

点击可选插件，然后在右上方搜索框搜索container，选择Deploy to container Plugin，点击安装即可

![](https://324204342-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Ltduzp_tx6Kzyq5YwdK%2F-Ltdv0j8Ne-IlZchzDgP%2F-Ltdv6qD2nAsAPo5Iz10%2Fimport-jenkins-27.png?generation=1573735141010692\&alt=media)

* **jenkins从Git自动下载项目，通过maven编译部署到Tomcat**

#### 1.新建项目

点击左上方新建，新建构建项目

![](https://324204342-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Ltduzp_tx6Kzyq5YwdK%2F-Ltdv0j8Ne-IlZchzDgP%2F-Ltdv6qF4Lsu5ocKCge3%2Fimport-jenkins-28.png?generation=1573735141952362\&alt=media)

填写项目名，选择自由风格

![](https://324204342-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Ltduzp_tx6Kzyq5YwdK%2F-Ltdv0j8Ne-IlZchzDgP%2F-Ltdv6qH0p-pKSblcLqd%2Fimport-jenkins-30.png?generation=1573735141833309\&alt=media)

配置源码管理

![](https://324204342-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Ltduzp_tx6Kzyq5YwdK%2F-Ltdv0j8Ne-IlZchzDgP%2F-Ltdv6qJcI2Ua1bPV_vI%2Fimport-jenkins-31.png?generation=1573735139064887\&alt=media)构建配置

![](https://324204342-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Ltduzp_tx6Kzyq5YwdK%2F-Ltdv0j8Ne-IlZchzDgP%2F-Ltdv6qLVBdqE9IhmX-u%2Fimport-jenkins-32.png?generation=1573735139696353\&alt=media)添加tomcat凭证

![](https://324204342-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Ltduzp_tx6Kzyq5YwdK%2F-Ltdv0j8Ne-IlZchzDgP%2F-Ltdv6qNBE06C9Z9UCdy%2Fimport-jenkins-43.png?generation=1573735137805324\&alt=media)同时，也需要在E:\tomcat\apache-tomcat-8.0.35\conf\tomcat-users.xml添加权限

```
<role rolename="manager"/>　  
<role rolename="manager-gui"/>　  
<role rolename="admin"/>　  
<role rolename="admin-gui"/>　  
<role rolename="manager-script"/>  
<user username="tomcat" password="tomcat" roles="admin-gui,admin,manager-gui,manager,manager-script"/>
```

构建后步骤 选择发布war包到一个容器中

![](https://324204342-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Ltduzp_tx6Kzyq5YwdK%2F-Ltdv0j8Ne-IlZchzDgP%2F-Ltdv6qPJhuMke2cukvG%2Fimport-jenkins-41.png?generation=1573735138097010\&alt=media)

* **开始构建**

点击左方的立即构建，下方会出现构建队列

![](https://324204342-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Ltduzp_tx6Kzyq5YwdK%2F-Ltdv0j8Ne-IlZchzDgP%2F-Ltdv6qRQmotqrWdld9x%2Fimport-jenkins-35.png?generation=1573735138471658\&alt=media)

点击进入队列，然后点击Console output 可以看到构建过程中打印的日志信息，可以用来在构建失败时排错

![](https://324204342-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Ltduzp_tx6Kzyq5YwdK%2F-Ltdv0j8Ne-IlZchzDgP%2F-Ltdv6qTbow8Av7qSlTf%2Fimport-jenkins-36.png?generation=1573735140602510\&alt=media)构建之后，会看到输出提示

![](https://324204342-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Ltduzp_tx6Kzyq5YwdK%2F-Ltdv0j8Ne-IlZchzDgP%2F-Ltdv6qV1KUF1Bb_uRyO%2Fimport-jenkins-37.png?generation=1573735138941596\&alt=media)

看一看是否成功发布了

我的tomcat地址是[http://localhost:8082/apidoc](http://localhost/apidoc)，之前填写contextpath是apidoc

![](https://324204342-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Ltduzp_tx6Kzyq5YwdK%2F-Ltdv0j8Ne-IlZchzDgP%2F-Ltdv6qXT5bhVvOeKxPq%2Fimport-jenkins-40.png?generation=1573735138273224\&alt=media)

> 注意：tomcat服务一定要是开着的，否则会报如下错误
>
> ```
> Deploying D:\Tools\jenkins\workspace\springboot-apidoc\target\springboot-apidoc-0.0.1-SNAPSHOT.war to container Tomcat 8.x Remote with context apidoc
> ```
>
> ```
> ERROR: Build step failed with exception
> org.codehaus.cargo.container.ContainerException: Failed to redeploy [D:\Tools\jenkins\workspace\springboot-apidoc\target\springboot-apidoc-0.0.1-SNAPSHOT.war]
>     at org.codehaus.cargo.container.tomcat.internal.AbstractTomcatManagerDeployer.redeploy(AbstractTomcatManagerDeployer.java:192)
>     at hudson.plugins.deploy.CargoContainerAdapter.deploy(CargoContainerAdapter.java:77)
>     at hudson.plugins.deploy.CargoContainerAdapter$DeployCallable.invoke(CargoContainerAdapter.java:147)
>     at hudson.plugins.deploy.CargoContainerAdapter$DeployCallable.invoke(CargoContainerAdapter.java:117)
>     at hudson.FilePath.act(FilePath.java:1076)
>     at hudson.FilePath.act(FilePath.java:1059)
>     at hudson.plugins.deploy.CargoContainerAdapter.redeploy(CargoContainerAdapter.java:114)
>     at hudson.plugins.deploy.PasswordProtectedAdapterCargo.redeploy(PasswordProtectedAdapterCargo.java:93)
>     at hudson.plugins.deploy.DeployPublisher.perform(DeployPublisher.java:64)
>     at hudson.tasks.BuildStepMonitor$3.perform(BuildStepMonitor.java:45)
>     at hudson.model.AbstractBuild$AbstractBuildExecution.perform(AbstractBuild.java:744)
>     at hudson.model.AbstractBuild$AbstractBuildExecution.performAllBuildSteps(AbstractBuild.java:690)
>     at hudson.model.Build$BuildExecution.post2(Build.java:186)
>     at hudson.model.AbstractBuild$AbstractBuildExecution.post(AbstractBuild.java:635)
>     at hudson.model.Run.execute(Run.java:1840)
>     at hudson.model.FreeStyleBuild.run(FreeStyleBuild.java:43)
>     at hudson.model.ResourceController.execute(ResourceController.java:97)
>     at hudson.model.Executor.run(Executor.java:429)
> Caused by: java.net.ConnectException: Connection refused: connect
>     at java.net.DualStackPlainSocketImpl.connect0(Native Method)
>     at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
>     at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
>     at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
>     at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
>     at java.net.PlainSocketImpl.connect(Unknown Source)
>     at java.net.SocksSocketImpl.connect(Unknown Source)
>     at java.net.Socket.connect(Unknown Source)
>     at java.net.Socket.connect(Unknown Source)
>     at sun.net.NetworkClient.doConnect(Unknown Source)
>     at sun.net.www.http.HttpClient.openServer(Unknown Source)
>     at sun.net.www.http.HttpClient.openServer(Unknown Source)
>     at sun.net.www.http.HttpClient.
> <
> init
> >
> (Unknown Source)
>     at sun.net.www.http.HttpClient.New(Unknown Source)
>     at sun.net.www.http.HttpClient.New(Unknown Source)
>     at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(Unknown Source)
>     at sun.net.www.protocol.http.HttpURLConnection.plainConnect0(Unknown Source)
>     at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source)
>     at sun.net.www.protocol.http.HttpURLConnection.connect(Unknown Source)
>     at org.codehaus.cargo.container.tomcat.internal.TomcatManager.invoke(TomcatManager.java:561)
>     at org.codehaus.cargo.container.tomcat.internal.TomcatManager.list(TomcatManager.java:876)
>     at org.codehaus.cargo.container.tomcat.internal.TomcatManager.getStatus(TomcatManager.java:889)
>     at org.codehaus.cargo.container.tomcat.internal.AbstractTomcatManagerDeployer.redeploy(AbstractTomcatManagerDeployer.java:173)
>     ... 17 more
> ```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://tuonioooo-notebook.gitbook.io/engineering-management/jenkins-ji-cheng/jenkinszi-dong-gou-jian-bu-shu-maven-xiang-mu.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
