maven的方便,相信大家都有所瞭解,但是自己去搭建一個maven的項目總是報各種錯,那今天我們來搭建自己的web項目並把它自動部署到容器中。
一:新建一個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。
二:自動發佈到容器中
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"/>
3.啓動我們的tomcat
4.項目package一下(與第一部分的打包一樣 項目右鍵項目》run as -》maven build... package )。
5.發佈到tomcat 右鍵項目》run as -》maven build... tomcat7:deploy
訪問下,ok!
三:進一步完善
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的自動部署就完成了!
<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。四:總結
<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>
祝大家順利完成自動部署。