简体   繁体   中英

How to deploy WAR with Maven to Tomcat?

I use the Maven with Eclipse. Is possible build the project and then deploy built WAR file to Tomcat server?

I use Windows. I can build WAR file, and also deploy it on the server manually. But I want to deploy the WAR file automatically after build action and it doesn't work. I am novice in Maven.

Should I set something in my run configuration? I have set goals to install value.

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>

Furthermore I changed settings.xml file in path ~/.m2/settings.xml .

settings.xml

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

Meanwhile I resolved it. There were a few issues.

Firstly I set Goal in the Eclipse run configuration to tomcat:deploy .

And I changed pom.xml following

<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 in configuration depends on Tomcat version. For me works text , for others works html on end of URL. And of course, in tomcat-users.xml must be set role manager-script or manager-gui .

Maybe it helps to others.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM