博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
tomcat7-maven-plugin-用于部署WAR的Tomcat Maven插件
阅读量:2547 次
发布时间:2019-05-11

本文共 10783 字,大约阅读时间需要 35 分钟。

Today we will look into the Tomcat Maven Plugin (tomcat7-maven-plugin) that helps us in deploying our WAR files to Tomcat easily.

今天,我们将研究Tomcat Maven插件(tomcat7-maven-plugin),该插件可帮助我们轻松地将WAR文件部署到Tomcat。

Tomcat Maven部署 (Tomcat Maven Deploy)

Recently I was developing a and after doing the build, I used to copy the generated WAR file to Tomcat webapps directory. It was time-consuming, boring and sometimes I used to miss the updated WAR file to copy and notice it later on.

最近,我正在开发一个并在完成构建后,将复制的WAR文件复制到Tomcat webapps目录中。 这很耗时,很无聊,有时我曾经错过了更新的WAR文件来复制并在以后注意到它。

Then I decided to look for some configurations through which I can configure Maven build to deploy the generated WAR file into tomcat automatically.

然后,我决定寻找一些配置,通过这些配置可以配置Maven构建,以将生成的WAR文件自动部署到tomcat中。

My first approach was to use Maven plugins to copy the generated WAR file to tomcat deploy directory.

我的第一种方法是使用Maven插件将生成的WAR文件复制到tomcat部署目录。

For example, below was the pom.xml file for my simple web application, I have removed all the unwanted stuff to keep it clear.

例如,以下是我的简单Web应用程序的pom.xml文件,我删除了所有不需要的内容以使其清晰可见。

4.0.0
MyWebapp
MyWebapp
0.0.1-SNAPSHOT
war
src
maven-war-plugin
2.3
WebContent
false
maven-compiler-plugin
3.1
1.7
1.7

I used maven-antrun-plugin to add another task for the integration-test phase where I am copying the WAR file to my local tomcat directory.

我使用maven-antrun-pluginintegration-test阶段添加了另一个任务,在该阶段,我将WAR文件复制到本地tomcat目录中。

4.0.0
MyWebapp
MyWebapp
0.0.1-SNAPSHOT
war
/Users/pankaj/Dev_Softwares/apache-tomcat-7.0.32/webapps
src
maven-war-plugin
2.3
WebContent
false
maven-compiler-plugin
3.1
1.7
1.7
maven-antrun-plugin
integration-test
run
${project.artifactId}

The change was really simple, create a property to define the tomcat deploy directory and add finalName configuration to skip version details from the WAR file.

更改非常简单,创建一个属性以定义tomcat的部署目录,并添加finalName配置以跳过WAR文件中的版本详细信息。

mvn clean integration-test package – it will generate the WAR file and deploy into tomcat too.

mvn clean Integration-test软件包 –它将生成WAR文件并也部署到tomcat中。

mvn clean package – It will just build the project, will not try to deploy it into tomcat container.

mvn clean package –它只会构建项目,不会尝试将其部署到tomcat容器中。

Above approach solved my problem, but there were two shortcomings;

上面的方法解决了我的问题,但是有两个缺点。

  1. Since tomcat deploy folder is hardcoded in the pom.xml, if I change tomcat server location then I would have to change it here too. Also because it’s my user configuration, I can’t check in this code into repository.

    由于tomcat deploy文件夹是硬编码在pom.xml中的,因此,如果我更改了tomcat服务器位置,则也必须在此处进行更改。 另外,由于这是我的用户配置,因此无法将此代码检入到存储库中。
  2. I can’t use this approach to deploy the application into any remote server.

    我无法使用这种方法将应用程序部署到任何远程服务器中。

Tomcat7 Maven插件 (Tomcat7 Maven Plugin)

Because of above shortcomings, I looked online for better solutions and found that there is a plugin already for this job – .

由于上述缺点,我在网上寻找更好的解决方案,并发现已经有一个用于此工作的 。

We can use this plugin with some configuration settings to deploy our application into Tomcat server using their management console API.

我们可以将此插件与某些配置设置一起使用,以使用其管理控制台API将我们的应用程序部署到Tomcat服务器中。

Tomcat Maven插件 (Tomcat Maven Plugin)

Before we can use Apache Tomcat Maven Plugin, we need to perform some configuration settings. Let’s look at these configurations one by one. Note that I am using Tomcat-7, if you are using any other version then some configurations might need to be changed accordingly.

在使用Apache Tomcat Maven插件之前,我们需要执行一些配置设置。 让我们一一看一下这些配置。 请注意,我使用的是Tomcat-7,如果您使用的是其他版本,则可能需要相应地更改某些配置。

  1. Adding Manager Roles and User in Tomcat 7: We need to add manager-gui and manager-script roles in tomcat-users.xml file and create a user with these roles. Just add below lines to your tomcat-users.xml file and restart the server.

    TOMCAT-HOME/conf/tomcat-users.xml

    在Tomcat 7中添加管理员角色和用户 :我们需要在tomcat-users.xml文件中添加manager-guimanager-script角色,并使用这些角色创建一个用户。 只需将以下几行添加到您的tomcat-users.xml文件中,然后重新启动服务器。

    TOMCAT-HOME/conf/tomcat-users.xml

  2. Adding server to Maven settings.xml file: Next step is to add a server in maven settings.xml file, as shown below.

    $HOME/.m2/settings.xml

    Tomcat
    tomcat
    tomcat

    Notice that username and password should be same as configured in the earlier step.

    将服务器添加到Maven settings.xml文件 :下一步是在maven settings.xml文件中添加服务器,如下所示。

    $HOME/.m2/settings.xml

    请注意,用户名和密码应与上一步中配置的相同。

  3. tomcat7-maven-plugin示例 (tomcat7-maven-plugin example)

  4. Maven Project pom.xml changes: Final step is to configure Tomcat Maven Plugin in the web application pom.xml file. Just add below lines to your pom.xml plugins section.
    org.apache.tomcat.maven
    tomcat7-maven-plugin
    2.2
    https://localhost:9090/manager/text
    Tomcat
    /MyWebapp

    tomcat6-maven-plugin示例 (tomcat6-maven-plugin example)

    If you are using Tomcat-6, below configurations should be used instead.

    Maven项目pom.xml更改 :最后一步是在Web应用程序pom.xml文件中配置Tomcat Maven插件。 只需将以下行添加到pom.xml插件部分。
    org.apache.tomcat.maven
    tomcat7-maven-plugin
    2.2
    https://localhost:9090/manager/text
    Tomcat
    /MyWebapp

    如果您使用的是Tomcat-6,则应改用以下配置。

  5. That’s it, now we can use below commands to deploy, undeploy, redeploy our application through maven command.

    就是这样,现在我们可以使用以下命令通过maven命令部署,取消部署和重新部署应用程序。

    Tomcat 7 Maven插件部署命令 (Tomcat 7 Maven Plugin Deploy Commands)

  • mvn tomcat7:deploy

    mvn tomcat7:部署
  • mvn tomcat7:undeploy

    mvn tomcat7:取消部署
  • mvn tomcat7:redeploy

    mvn tomcat7:重新部署

Tomcat 6 Maven插件部署命令 (Tomcat 6 Maven Plugin Deploy Commands)

For Tomcat-6, you should use below commands.

对于Tomcat-6,应使用以下命令。

  • mvn tomcat6:deploy

    mvn tomcat6:部署
  • mvn tomcat6:undeploy

    mvn tomcat6:取消部署
  • mvn tomcat6:redeploy

    mvn tomcat6:重新部署

Below is the sample output produced by the deploy command for tomcat7-maven-plugin.

以下是deploy命令为tomcat7-maven-plugin生成的示例输出。

pankaj:MyWebapp pankaj$ mvn tomcat7:deploy[INFO] Scanning for projects...[INFO]                                                                         [INFO] ------------------------------------------------------------------------[INFO] Building MyWebapp 0.0.1-SNAPSHOT[INFO] ------------------------------------------------------------------------[INFO] [INFO] >>> tomcat7-maven-plugin:2.2:deploy (default-cli) @ MyWebapp >>>[INFO] [INFO] --- maven-resources-plugin:2.5:resources (default-resources) @ MyWebapp ---[debug] execute contextualize[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent![INFO] skip non existing resourceDirectory /Users/pankaj/Documents/workspace/j2ee/MyWebapp/src/main/resources[INFO] [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ MyWebapp ---[INFO] Nothing to compile - all classes are up to date[INFO] [INFO] --- maven-resources-plugin:2.5:testResources (default-testResources) @ MyWebapp ---[debug] execute contextualize[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent![INFO] skip non existing resourceDirectory /Users/pankaj/Documents/workspace/j2ee/MyWebapp/src/test/resources[INFO] [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ MyWebapp ---[INFO] No sources to compile[INFO] [INFO] --- maven-surefire-plugin:2.10:test (default-test) @ MyWebapp ---[INFO] No tests to run.[INFO] Surefire report directory: /Users/pankaj/Documents/workspace/j2ee/MyWebapp/target/surefire-reports------------------------------------------------------- T E S T S-------------------------------------------------------Results :Tests run: 0, Failures: 0, Errors: 0, Skipped: 0[INFO] [INFO] --- maven-war-plugin:2.3:war (default-war) @ MyWebapp ---[INFO] Packaging webapp[INFO] Assembling webapp [MyWebapp] in [/Users/pankaj/Documents/workspace/j2ee/MyWebapp/target/MyWebapp-0.0.1-SNAPSHOT][INFO] Processing war project[INFO] Copying webapp resources [/Users/pankaj/Documents/workspace/j2ee/MyWebapp/WebContent][INFO] Webapp assembled in [26 msecs][INFO] Building war: /Users/pankaj/Documents/workspace/j2ee/MyWebapp/target/MyWebapp-0.0.1-SNAPSHOT.war[INFO] [INFO] <<< tomcat7-maven-plugin:2.2:deploy (default-cli) @ MyWebapp <<<[INFO] [INFO] --- tomcat7-maven-plugin:2.2:deploy (default-cli) @ MyWebapp ---[INFO] Deploying war to https://localhost:9090/MyWebapp  SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".SLF4J: Defaulting to no-operation (NOP) logger implementationSLF4J: See https://www.slf4j.org/codes.html#StaticLoggerBinder for further details.Uploading: https://localhost:9090/manager/text/deploy?path=%2FMyWebappUploaded: https://localhost:9090/manager/text/deploy?path=%2FMyWebapp (2 KB)[INFO] tomcatManager status code:200, ReasonPhrase:OK[INFO] OK - Deployed application at context path /MyWebapp[INFO] ------------------------------------------------------------------------[INFO] BUILD SUCCESS[INFO] ------------------------------------------------------------------------[INFO] Total time: 3.185s[INFO] Finished at: Mon Sep 15 18:07:00 IST 2014[INFO] Final Memory: 13M/159M[INFO] ------------------------------------------------------------------------

You can go to your Tomcat Manager App and confirm that application is deployed or you can directly access the application in the browser to confirm it. That’s all for tomcat maven plugin and tomcat7-maven-plugin example.

您可以转到Tomcat Manager应用程序并确认已部署该应用程序,也可以直接在浏览器中访问该应用程序以进行确认。 这就是tomcat maven插件和tomcat7-maven-plugin示例的全部内容。

翻译自:

转载地址:http://aqmzd.baihongyu.com/

你可能感兴趣的文章
iOS语言中的KVO机制
查看>>
excel第一次打开报错 向程序发送命令时出错 多种解决办法含终极解决方法
查看>>
响应式web设计之CSS3 Media Queries
查看>>
实验三
查看>>
机器码和字节码
查看>>
环形菜单的实现
查看>>
【解决Chrome浏览器和IE浏览器上传附件兼容的问题 -- Chrome关闭flash后,uploadify插件不可用的解决办法】...
查看>>
34 帧动画
查看>>
二次剩余及欧拉准则
查看>>
Centos 7 Mysql 最大连接数超了问题解决
查看>>
thymeleaf 自定义标签
查看>>
关于WordCount的作业
查看>>
C6748和音频ADC连接时候的TDM以及I2S格式问题
查看>>
UIView的layoutSubviews,initWithFrame,initWithCoder方法
查看>>
STM32+IAP方案 实现网络升级应用固件
查看>>
用74HC165读8个按键状态
查看>>
jpg转bmp(使用libjpeg)
查看>>
linear-gradient常用实现效果
查看>>
sql语言的一大类 DML 数据的操纵语言
查看>>
VMware黑屏解决方法
查看>>