maven创建web项目并自动部署到tomcat(jetty)

来源:互联网 发布:亚马逊的a9算法 编辑:程序博客网 时间:2024/05/19 11:18

maven的方便,相信大家都有所了解,但是自己去搭建一个maven的项目总是报各种错,那今天我们来搭建自己的web项目并把它自动部署到容器中。

一:新建一个maven项目

不多说,直接来new吧,至于maven安装这些就不在此处累述了()。


next-》next




当然第一次会慢一点,要下载archetype插件。


好吧有2个错,还有jdk的版本给了1.5,不符合我们的需要啊,那就要改了。

从简单的开始,先修改第一个错(The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Pathindex.jsp)

pom.xml文件的dependencies替换成如下:

顺便把junit 3.8.1改成4.12(此处可以不改),保存,错误少了一个

  <dependencies>    <dependency>      <groupId>junit</groupId>      <artifactId>junit</artifactId>      <version>4.12</version>      <scope>test</scope>    </dependency>        <!-- servlet --><dependency><groupId>javax.servlet</groupId><artifactId>servlet-api</artifactId><version>3.0-alpha-1</version><scope>provided</scope></dependency>  </dependencies>


第二个错:

右键项目-》properties




博主jdk1.8,所有括号里面显示的是我的jdk1.8.


再次点击project facets



ok,以上项目就不报错了!

右键-》run as -》maven build...


这样就打包成功了!我们先手工把他丢到tomcat中跑起来看看!


ok到这样我们的项目算是成功了。


但是每次都要这么麻烦,有没有什么地方可以改进呢?

当然默认的jdk的版本我们是可以设置的,在memen的setting.xml文件中找关键字jdk。


加上如图的jdk信息。

    <profile>            <id>jdk-1.7</id>            <activation>                <activeByDefault>true</activeByDefault>                <jdk>1.7</jdk>            </activation>            <properties>                <maven.compiler.source>1.7</maven.compiler.source>                <maven.compiler.target>1.7</maven.compiler.target>                <maven.compiler.compilerVersion>1.7</maven.compiler.compilerVersion>            </properties>        </profile>

OK,现在我们新建的项目的jdk都是1.7的版本了,但是最好还是要修改你的Dynamic Web Module 从2.3修改到3.0

项目的报错还是 servlet 的错

二:自动发布到容器中

1.首先我们要修改我们的pom.xml,在build里面追加maven的tomcat插件。

<plugin><span style="white-space:pre"></span><groupId>org.apache.tomcat.maven</groupId><span style="white-space:pre"></span><artifactId>tomcat7-maven-plugin</artifactId><span style="white-space:pre"></span><version>2.2</version><span style="white-space:pre"></span><configuration><span style="white-space:pre"></span><!-- tomcat的url --><span style="white-space:pre"></span><url>http://localhost:8080/manager/text</url><span style="white-space:pre"></span><!-- maven setting中server的id--><span style="white-space:pre"></span><server>tomcat</server><span style="white-space:pre"></span><!-- 用户名 --><span style="white-space:pre"></span><username>tomcat</username><span style="white-space:pre"></span><!-- 密码 --><span style="white-space:pre"></span><password>tomcat</password><span style="white-space:pre"></span><!-- 项目名称(此处我的是在build下面的<finalName>auto_container</finalName>) --><span style="white-space:pre"></span><path>/${project.build.finalName}</path><span style="white-space:pre"></span></configuration></plugin>

2.修改我们tomcat根目录下 conf文件夹下tomcat-users.xml增加:

<role rolename="admin-gui"/>    <role rolename="admin-script"/>    <role rolename="manager-gui"/>    <role rolename="manager-script"/>    <role rolename="manager-jmx"/>    <role rolename="manager-status"/>    <user username="tomcat" password="tomcat" roles="manager-gui,manager-script,manager-jmx,manager-status,admin-script,admin-gui"/>
manager-gui与manager-script是必须的,其他的可以没有,此处是博主Jenkins的配置就不改了。

3.启动我们的tomcat

4.项目package一下(与第一部分的打包一样 项目右键项目》run as -》maven build...  package )。

5.发布到tomcat 右键项目》run as -》maven build...  tomcat7:deploy



访问下,ok!

三:进一步完善


这时候小伙伴肯定想,难道非要运行2次maven的命令?

1.其实我们还可以继续对我们的pom.xml进行修改:

<plugin><span style="white-space:pre"></span><groupId>org.apache.tomcat.maven</groupId><span style="white-space:pre"></span><artifactId>tomcat7-maven-plugin</artifactId><span style="white-space:pre"></span><version>2.2</version><span style="white-space:pre"></span><executions><span style="white-space:pre"></span><execution><span style="white-space:pre"></span><!-- 阶段这个有很多,按自己的需求来 --><span style="white-space:pre"></span><phase>package</phase><span style="white-space:pre"></span><goals><span style="white-space:pre"></span><!-- 目标,这个地方的选择也很多,由于我们之前部署过了,所以这个地方选的是 redeploy,当然第一次不是选deploy--><span style="white-space:pre"></span><goal>redeploy</goal><span style="white-space:pre"></span></goals><span style="white-space:pre"></span><configuration><span style="white-space:pre"></span><url>http://localhost:8080/manager/text</url><span style="white-space:pre"></span><server>mytomcat</server><span style="white-space:pre"></span><path>/${project.build.finalName}</path><span style="white-space:pre"></span></configuration><span style="white-space:pre"></span></execution><span style="white-space:pre"></span></executions></plugin>
细心的小伙伴发现我在configuration里面少了用户名和密码,不是博主少了,而是我把他放到了其他地方:

2.maven根目录下的conf目录的setting.xml文件,找到servers增加:

<!-- 配置tomcat-/manager/text 访问权限 -->    <server>      <id>mytomcat</id>      <username>tomcat</username>      <password>tomcat</password>    </server>

好的,我们在此发布一下:

3.右键项目》run as -》maven build...  package


再次访问下:

至此我们tomcat的自动部署就完成了!

当然jetty自动部署原理一样:
<plugin><span style="white-space:pre"></span><groupId>org.mortbay.jetty</groupId><span style="white-space:pre"></span><artifactId>jetty-maven-plugin</artifactId><span style="white-space:pre"></span><version>8.1.16.v20140903</version><span style="white-space:pre"></span><executions><span style="white-space:pre"></span><execution><span style="white-space:pre"></span><phase>package</phase><span style="white-space:pre"></span><goals><span style="white-space:pre"></span><goal>run</goal><span style="white-space:pre"></span></goals><span style="white-space:pre"></span></execution><span style="white-space:pre"></span></executions></plugin>
运行package之前记得关闭你的tomcat哦,jetty的默认端口也是8080。


jetty默认是在/目录下的,我们也访问下:


四:总结

小伙伴是否完成了自己的自动部署呢?!
最后放上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/maven-v4_0_0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.julyday</groupId><artifactId>auto_container</artifactId><packaging>war</packaging><version>0.0.1-SNAPSHOT</version><name>auto_container Maven Webapp</name><url>http://maven.apache.org</url><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version><scope>test</scope></dependency><!-- servlet --><dependency><groupId>javax.servlet</groupId><artifactId>servlet-api</artifactId><version>3.0-alpha-1</version><scope>provided</scope></dependency></dependencies><build><finalName>auto_container</finalName><plugins><!-- 两次命令方式 --><plugin>    <groupId>org.apache.tomcat.maven</groupId>    <artifactId>tomcat7-maven-plugin</artifactId>    <version>2.2</version>    <configuration>    <!-- tomcat的url -->        <url>http://localhost:8080/manager/text</url>        <!-- maven setting中server的id-->        <server>tomcat</server>        <!-- 用户名 -->        <username>tomcat</username>        <!-- 密码 -->        <password>tomcat</password>        <!-- 项目名称(此处我的是在build下面的<finalName>auto_container</finalName>) -->        <path>/${project.build.finalName}</path>    </configuration></plugin><!-- 打包成功后部署到tomcat --><plugin><groupId>org.apache.tomcat.maven</groupId><artifactId>tomcat7-maven-plugin</artifactId><version>2.2</version><executions><execution><!-- 阶段这个有很多,按自己的需求来 --><phase>package</phase><goals><!-- 目标,这个地方的选择也很多,由于我们之前部署过了,所以这个地方选的是 redeploy,当然第一次不是选deploy--><goal>redeploy</goal></goals><configuration><url>http://localhost:8080/manager/text</url><server>mytomcat</server><path>/${project.build.finalName}</path></configuration></execution></executions></plugin><!-- jetty --><plugin>    <groupId>org.mortbay.jetty</groupId>    <artifactId>jetty-maven-plugin</artifactId>    <version>8.1.16.v20140903</version><executions><execution><phase>package</phase><goals><goal>run</goal></goals></execution></executions></plugin></plugins></build></project>
祝大家顺利完成自动部署。




0 0
原创粉丝点击