Maven配置tomcat和jetty插件来运行项目

针对eclipse中的Run on Server有些情况下并不是那么好操作,比如配置maven下的springmvc插件,如果使用此方法运行会很容易出现组件缺少导致错误出现一大堆的问题。

那么针对这种情况,maven下的容器插件tomcat和jetty做了很好的支持。比如在maven下配置了这两个组件:

<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/maven-v4_0_0.xsd">
    ...
    <build>
        <finalName>testmvchelloworld</finalName>
        <plugins>
            <!-- Config: Maven Tomcat Plugin -->
            <!-- http://mvnrepository.com/artifact/org.apache.tomcat.maven/tomcat7-maven-plugin -->
            <!-- http://tomcat.apache.org/maven-plugin-2.0/tomcat7-maven-plugin/plugin-info.html -->
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.2</version>
                <!-- Config: contextPath and Port (Default:8080) -->
                <!-- 
                <configuration> 
                    <path>/</path> 
                    <port>8899</port> 
                </configuration>
                 -->
            </plugin>
            <!-- Config: Maven Jetty Plugin -->
            <!-- http://mvnrepository.com/artifact/org.mortbay.jetty/jetty-maven-plugin -->
            <!-- http://www.eclipse.org/jetty/documentation/current/jetty-maven-plugin.html -->
            <plugin>
                <groupId>org.eclipse.jetty</groupId>
                <artifactId>jetty-maven-plugin</artifactId>
                <version>9.4.3.v20170317</version>
                <!-- Config: contextPath and Port (Default:8080) -->
                <!--             
                <configuration>
                    <httpConnector>
                        <port>8899</port>
                    </httpConnector>
                    <webAppConfig>
                        <contextPath>/</contextPath>
                    </webAppConfig>
                </configuration>
                 -->
            </plugin>
        </plugins>
    </build>
</project>

提示:如果想要带上引用名,可以在<contextPath>标签上加上/${project.artifactId}

那么在调试运行项目时,可以直接使用maven的命令直接进行:

//tomcat7
mvn tomcat7:run
//jetty
mvn jetty:run

其中控制台上输出的信息和本身Run on Server保持了一致。

posted @ 2017-04-10 03:16  EasonJim  阅读(2505)  评论(0编辑  收藏  举报