当前位置: 首页 > 工具软件 > maven-manager > 使用案例 >

tomcat7-maven-plugin-用于部署WAR的Tomcat Maven插件

梁华清
2023-12-01

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 web application 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.

最近,我正在开发一个Web应用程序,并在完成构建后,将复制的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文件,我删除了所有不需要的内容以使其清晰可见。

<project xmlns="https://maven.apache.org/POM/4.0.0" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="https://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>MyWebapp</groupId>
	<artifactId>MyWebapp</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>war</packaging>
	<build>
		<sourceDirectory>src</sourceDirectory>
		<plugins>
			<plugin>
				<artifactId>maven-war-plugin</artifactId>
				<version>2.3</version>
				<configuration>
					<warSourceDirectory>WebContent</warSourceDirectory>
					<failOnMissingWebXml>false</failOnMissingWebXml>
				</configuration>
			</plugin>
			<plugin>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.1</version>
				<configuration>
					<source>1.7</source>
					<target>1.7</target>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

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目录中。

<project xmlns="https://maven.apache.org/POM/4.0.0" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="https://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>MyWebapp</groupId>
	<artifactId>MyWebapp</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>war</packaging>
	
	<properties>
		<deployFolder>/Users/pankaj/Dev_Softwares/apache-tomcat-7.0.32/webapps</deployFolder>
	</properties>
	
	<build>
		<sourceDirectory>src</sourceDirectory>
		<plugins>
			<plugin>
				<artifactId>maven-war-plugin</artifactId>
				<version>2.3</version>
				<configuration>
					<warSourceDirectory>WebContent</warSourceDirectory>
					<failOnMissingWebXml>false</failOnMissingWebXml>
				</configuration>
			</plugin>
			<plugin>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.1</version>
				<configuration>
					<source>1.7</source>
					<target>1.7</target>
				</configuration>
			</plugin>
			
			<plugin>
				<artifactId>maven-antrun-plugin</artifactId>
				<executions>
					<execution>
						<phase>integration-test</phase>
						<configuration>
							<tasks>
								<copy file="target/${pom.build.finalName}.war" tofile="${deployFolder}/${pom.build.finalName}.war" />
							</tasks>
						</configuration>
						<goals>
							<goal>run</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
		</plugins>
		<finalName>${project.artifactId}</finalName>
	</build>
</project>

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 – Tomcat Maven Plugin.

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

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

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

    在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

    <settings xmlns="https://maven.apache.org/SETTINGS/1.0.0"
          xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="https://maven.apache.org/SETTINGS/1.0.0
                              https://maven.apache.org/xsd/settings-1.0.0.xsd">
    <servers>
          <server>
            <id>Tomcat</id>
            <username>tomcat</username>
            <password>tomcat</password>
          </server>
    </servers>
    </settings>

    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.
    <plugin>
    	<groupId>org.apache.tomcat.maven</groupId>
    	<artifactId>tomcat7-maven-plugin</artifactId>
    	<version>2.2</version>
    	<configuration>
    		<url>https://localhost:9090/manager/text</url>
    		<server>Tomcat</server>
    		<path>/MyWebapp</path>
    	</configuration>
    </plugin>

    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插件部分。
    <plugin>
    	<groupId>org.apache.tomcat.maven</groupId>
    	<artifactId>tomcat7-maven-plugin</artifactId>
    	<version>2.2</version>
    	<configuration>
    		<url>https://localhost:9090/manager/text</url>
    		<server>Tomcat</server>
    		<path>/MyWebapp</path>
    	</configuration>
    </plugin>

    如果您使用的是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 implementation
SLF4J: See https://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Uploading: https://localhost:9090/manager/text/deploy?path=%2FMyWebapp
Uploaded: 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示例的全部内容。

翻译自: https://www.journaldev.com/4738/tomcat7-maven-plugin-deploy-war

 类似资料: