如何使用Maven将WAR部署到Tomcat?

时间:2013-04-05 00:39:27

标签: maven tomcat

我在Eclipse中使用Maven。是否可以构建项目,然后将构建的WAR文件部署到Tomcat服务器?

我使用Windows。我可以构建WAR文件,并手动将其部署在服务器上。但我想在构建操作后自动部署WAR文件,但它不起作用。我是Maven的新手。

我应该在运行配置中设置一些内容吗?我已将目标设定为install值。

的pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>HelloWorld</groupId>
<artifactId>HelloWorld</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>HelloWorld</name>

<properties>
    <spring.version>3.2.2.RELEASE</spring.version>
</properties>

<dependencies>

    <!-- Java Server Pages Standard Tag Library -->
    <dependency>
        <groupId>jstl</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>


    <!-- Spring 3 dependencies -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>${spring.version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>${spring.version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${spring.version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>${spring.version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${spring.version}</version>
    </dependency>
</dependencies>

<build>
    <directory>${project.basedir}/target</directory>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.3.2</version>
            <configuration>
                <source>${java-version}</source>
                <target>${java-version}</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <executions>
                <execution>
                    <id>install</id>
                    <phase>install</phase>
                    <goals>
                        <goal>sources</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <!-- Maven Tomcat Plugin -->
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>tomcat-maven-plugin</artifactId>
            <configuration>
                <server>Apache_Tomcat_7_x86</server>
                <warFile>${project.build.directory}/HelloWorld-0.0.1-SNAPSHOT.war</warFile>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.3</version>
            <configuration>
                <packagingExcludes>WEB-INF/web.xml</packagingExcludes>
            </configuration>
        </plugin>
    </plugins>
</build>

此外,我更改了路径settings.xml中的~/.m2/settings.xml文件。

的settings.xml

  <servers>
      <server>
        <id>Apache_Tomcat_7_x86</id>
        <username>admin</username>
        <password>admin</password>
      </server>
  </servers>

1 个答案:

答案 0 :(得分:20)

同时我解决了这个问题。有一些问题。

首先,我将Eclipse 运行配置中的目标设置为tomcat:deploy

我在

之后更改了pom.xml
<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>tomcat-maven-plugin</artifactId>
  <configuration>
    <url>http://localhost:8080/manager/text</url>
    <server>Apache_Tomcat_7_x86</server>
    <path>/HelloWorld</path>
  </configuration>
</plugin>

配置中的URL取决于Tomcat版本。对我而言,作品text,其他作品在网址末尾html。当然,tomcat-users.xml必须设置角色manager-scriptmanager-gui

也许对其他人有帮助。