日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

tomcat自动运行磁盘任意位置上的项目、使用Maven对tomcat进行自动部署

發布時間:2024/9/27 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 tomcat自动运行磁盘任意位置上的项目、使用Maven对tomcat进行自动部署 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.


對于非Mavenweb項目,有時候我們想不時常通過打war包、拷貝war包、啟動tomcat來運行項目、這時候我們可以通過以下方式來進行配置:

1.1:創建web工程。工程結構如下:

1.2、其中index.jsp的內容如下:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>

?

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

? <head>

??? <base href="<%=basePath%>">

???

??? <title>My JSP 'index.jsp' starting page</title>

?? <meta http-equiv="pragma" content="no-cache">

?? <meta http-equiv="cache-control" content="no-cache">

?? <meta http-equiv="expires" content="0">???

?? <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">

?? <meta http-equiv="description" content="This is my page">

?? <!--

?? <link rel="stylesheet" type="text/css" href="styles.css">

?? -->

? </head>

?

? <body>

??? 測試一下 <br>

? </body>

</html>

1.3進入tomcat下的\conf\Catalina\localhost,創建一個web.xml,這個名稱可以隨便起。

編寫內容如下:

內容:

<Context?path="/web"?reloadable="true"?docBase="E:\workspace\CMSWORKSPACE\web\ WebRoot "/>?

注意:docBase后面一定要加上WebRoot,在瀏覽器上輸入:http://127.0.0.1:8080/web/

?

以上可以參考:http://www.cnblogs.com/xiohao/p/3689832.html

?

2:針對maven項目,若想讓項目也能夠自動化部署到tomcat中,需要通過maven自動化部署項目到tomcat中,配置方式:

2.1:第一步:配置tomcat訪問權限配置是tomcat安裝目錄下conf文件夾中的tomcat-user.xml文件中配置,

具體配置如下:

<?xml version='1.0' encoding='utf-8'?>

<!--

? Licensed to the Apache Software Foundation (ASF) under one or more

? contributor license agreements.? See the NOTICE file distributed with

??this work for additional information regarding copyright ownership.

? The ASF licenses this file to You under the Apache License, Version 2.0

? (the "License"); you may not use this file except in compliance with

? the License.? You may obtain a copy of the License at

?

????? http://www.apache.org/licenses/LICENSE-2.0

?

? Unless required by applicable law or agreed to in writing, software

? distributed under the License is distributed on an "AS IS" BASIS,

? WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

? See the License for the specific language governing permissions and

? limitations under the License.

-->

<tomcat-users>

? <role rolename="admin"/>

? <role rolename="manager"/>

? <role rolename="manager-gui"/>

? <role rolename="manager-script"/>

? <user username="admin" password="admin" roles="admin,manager,manager-gui,manager-script"/>

</tomcat-users>

?

2.2:在maven配置文件中配置tomcatserver

安裝過maven的朋友們應該都知道maven的配置文件,找到mavensettings.xml配置文件,找到servers,然后配置tomcatserver,具體配置如下:

內容:

<servers>

??? <!-- server

???? | Specifies the authentication information to use when connecting to a particular server, identified by

???? | a unique name within the system (referred to by the 'id' attribute below).

???? |

???? | NOTE: You should either specify username/password OR privateKey/passphrase, since these pairings are

???? |?????? used together.

???? |

???????? ?-->

??? <server>

???? <id>tomcat</id>

???? <username>admin</username>

???? <password>admin</password>

??? </server>

</servers >

2.3:項目的pom.xml中配置tomcat-maven-plugin插件:

打開項目下的pom.xml配置文件,找到plugins標簽,我們需要在那里配置tomcat-maven-plugin插件,從網上搜索了一些配置方法,發現有兩種不同的配置,分界點是根據tomcat的版本來區分的,tomcat7需要使用新版本,新版本的groupIdorg.codehaus.mojo改為org.apache.tomcat.maven,新版本同時也支持tomcat6,接下來的配置我就使用了新版本的配置,具體參考:

<build>

????? <plugins>

???????? <plugin>

??????????? <artifactId>maven-compiler-plugin</artifactId>

??????????? <version>2.0.2</version>

??????????? <configuration>

??????????????? <source>1.5</source>

??????????????? <target>1.5</target>

??????????????? <fork>true</fork>

??????????????? <meminitial>128m</meminitial>

??????????????? <maxmem>512m</maxmem>

??????????????? <encoding>UTF-8</encoding>

??????????? </configuration>

???????? </plugin>

???????? <plugin>

??????????? <artifactId>maven-eclipse-plugin</artifactId>

??????????? <version>2.5.1</version>

??????????? <configuration>

??????????????? <additionalProjectnatures>

?????????????????? <projectnature>

?????????????????? ?? org.springframework.ide.eclipse.core.springnature

?????????????????? </projectnature>

??????????????? </additionalProjectnatures>

??????????????? <additionalBuildcommands>

?????????????????? <buildcommand>

?????????????????? ?? org.springframework.ide.eclipse.core.springbuilder

?????????????????? </buildcommand>

??????????????? </additionalBuildcommands>

??????????????? <downloadSources>false</downloadSources>

??????????????? <downloadJavadocs>false</downloadJavadocs>

??????????????? <wtpversion>1.5</wtpversion>

??????????? </configuration>

???????? </plugin>

???????? <!-- 要加上下面的一句,否則執行:mvn package -Dmaven.test.skip=true的時候會報錯 -->

???????? <plugin>

??????????? <artifactId>maven-war-plugin</artifactId>

??????????? <version>2.1.1</version>

???????? </plugin>

???????? <plugin>

???????? ??? <groupId>org.apache.tomcat.maven</groupId>

???????? ??? <artifactId>tomcat6-maven-plugin</artifactId>

???????? ??? <version>2.0</version>

???????? ??? <configuration>

???????? ??????? <url>http://127.0.0.1:8080/manager</url>

???????? ??????? <server>tomcat</server>

???????? ??????? <username>admin</username>

??????? ???????? <password>admin</password>

??????? ???????? <!-- <update>true</update> -->

???????? ??????? <path>/app-tpl-webapp</path>

???????? ??? </configuration>

???????? </plugin>

????? </plugins>

?? </build>

注意:這里的path的值就是最后發布后的文件名稱。

????Tomcat6url配置必須為http://localhost:8080/manager?后面不能加html或者text,不然報403錯誤。

?Tomcat7url配置必須為http://127.0.0.1:8080/manager/text?text不能替換為html,不然報403錯誤。

?

配置參見:http://portlandgo.blog.163.com/blog/static/218936024201433032857104/

?

最后進入上面pom.xml所在位置,打開所屬pom.xml下的cmd命令行窗口,在命令中輸入:mvn tomcat6:redeploy

?

最后發現:

?

?

其它參考網站:http://www.cnblogs.com/xyb930826/p/5725340.html

?

其它參考內容:http://www.cnblogs.com/AloneSword/p/4100072.html

?

?

Maven已經是Java的項目管理標配,如何在JavaEE開發使用Maven調用Web應用,是很多同學關心的問題。本文將介紹,Maven如何介紹Tomcat插件。

Maven Tomcat插件現在主要有兩個版本,tomcat-maven-plugintomcat7-maven-plugin,使用方式基本相同。

tomcat-maven-plugin 插件官網:http://mojo.codehaus.org/tomcat-maven-plugin/plugin-info.html。

tomcat7-maven-plugin 插件官網:http://tomcat.apache.org/maven-plugin.html

?

tomcat-maven-plugin? 插件使用

配置

pom.xm 加入以下xml

?????????? ?<plugin> ????????? ??????<groupId>org.codehaus.mojo</groupId> ?????????????? ?<artifactId>tomcat-maven-plugin</artifactId> ?????????????? ?<version>1.1</version> ?????????????? ?<configuration> ?????????????????? ?<path>/wp</path> ?????????????????? ?<port>8080</port> ????? ??????????????<uriEncoding>UTF-8</uriEncoding> ?????????????????? ?<url>http://localhost:8080/manager/html</url> ?????????????????? ?<server>tomcat6</server> ?????????????? ?</configuration> ?????????? ?</plugin>

?

簡要說明一下:

path? 是訪問應用的路徑

port tomcat 的端口號

uriEncoding?URLUTF-8進行編碼,這樣就解決了中文參數亂碼。

Server指定tomcat名稱。

配置就這么簡單,基本搞掂,下面看看如何使用。

插件運行

?

如果Eclipse安裝了Maven插件,選 pom.xml文件,擊右鍵——>選擇 Run As——> Maven build

?

如果是第一次運行,會彈出下面對話框。在Goals框加加入以下命令: tomcat:run

?

這樣Tomcat 插件就可以運行。

下面介紹幾個常用的Goal

命令

描述

tomcat:deploy

部署一個web war

tomcat:reload

重新加載web war

tomcat:start

啟動tomcat

tomcat:stop

停止tomcat

tomcat:undeploy

停止一個war

tomcat:run

啟動嵌入式tomcat ,并運行當前項目

tomcat7-maven-plugin 使用

配置

?

兩個插件使用方法基本一樣,同樣需要在pom.xml引用該插件,需要增加以下配置

?

?????????? ?<plugin> ?????????????? ?<groupId>org.apache.tomcat.maven</groupId> ?????????????? ?<artifactId>tomcat7-maven-plugin</artifactId> ?????????????? ?<version>2.1</version> ?????????????? ?<configuration> ?????????????????? ?<port>9090</port> ?????????????????? ?<path>/mgr</path> ?????????????????? ?<uriEncoding>UTF-8</uriEncoding> ?????????? ?????????<finalName>mgr</finalName> ?????????????????? ?<server>tomcat7</server> ?????????????? ?</configuration> ?????????? ?</plugin>

?

具體配置一樣。

插件使用?

在這里要注意一下,該插件命名方式有些不同,比如啟動tomcat,對應的目標命令是: tomcat7:run ,同樣,其它命令也是這樣,需要更改為:tomcat7<插件執行點>

?

?

OK,配置就這么簡單,如果需要在tomcat 跟蹤聯調,可以用Dubug 方式啟動maven命令。如下圖

?

?

?

?

?

總結

以上是生活随笔為你收集整理的tomcat自动运行磁盘任意位置上的项目、使用Maven对tomcat进行自动部署的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。