使用maven命令建立web項目_maven

來源:互聯網
上載者:User

1.建立項目

mvn archetype:generate -DgroupId=cn.dinfo.boc.bocsearch  -DartifactId=bocsearch -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false

2.項目的目錄結構:

 

.|____bocsearch||____pom.xml||____src|||____main||||____resources||||____webapp|||||____index.jsp|||||____WEB-INF||||||____web.xml

3.修改設定檔:

   1.修改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.0http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>cn.dinfo.boc.bocsearch</groupId>
  <artifactId>bocsearch</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>bocsearch Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <properties>
  <jdk.version>1.7</jdk.version>
  <spring.version>4.1.1.RELEASE</spring.version>
  <jstl.version>1.2</jstl.version>
  <junit.version>4.11</junit.version>
  <logback.version>1.0.13</logback.version>
  <jcl-over-slf4j.version>1.7.5</jcl-over-slf4j.version>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><!-- 解決打包中文亂碼 -->
 </properties>
  <dependencies>
  <!-- Unit Test -->
  <dependency>
   <groupId>junit</groupId>
   <artifactId>junit</artifactId>
   <version>${junit.version}</version>
  </dependency>

  <!-- Spring Core -->
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-core</artifactId>
   <version>${spring.version}</version>
   <exclusions>
    <exclusion>
     <groupId>commons-logging</groupId>
     <artifactId>commons-logging</artifactId>
    </exclusion>
   </exclusions>
  </dependency>

  <dependency>
   <groupId>org.slf4j</groupId>
   <artifactId>jcl-over-slf4j</artifactId>
   <version>${jcl-over-slf4j.version}</version>
  </dependency>
  <dependency>
   <groupId>ch.qos.logback</groupId>
   <artifactId>logback-classic</artifactId>
   <version>${logback.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>
  <!-- jstl -->
  <dependency>
   <groupId>jstl</groupId>
   <artifactId>jstl</artifactId>
   <version>${jstl.version}</version>
  </dependency>
    <!-- Servlet -->
  <dependency>
   <groupId>javax.servlet</groupId>
   <artifactId>jstl</artifactId>
   <version>1.2</version>
  </dependency>
  <dependency>
   <groupId>javax.el</groupId>
   <artifactId>el-api</artifactId>
   <version>1.0</version>
   <scope>provided</scope>
  </dependency>
  <dependency>
   <groupId>javax.servlet</groupId>
   <artifactId>servlet-api</artifactId>
   <version>2.5</version>
   <scope>provided</scope>
  </dependency>
  <dependency>
   <groupId>javax.servlet.jsp</groupId>
   <artifactId>jsp-api</artifactId>
   <version>2.1</version>
   <scope>provided</scope>
  </dependency>
  
  
  
  
 </dependencies>
  <build>
  <finalName>BocWebApp</finalName><!-- 設定war包的名稱 -->

  <plugins>
      <!-- Eclipse project -->
    <plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-eclipse-plugin</artifactId>
   <version>2.9</version>
   <configuration>
           <!-- Always download and attach dependencies source code -->
    <downloadSources>true</downloadSources>
    <downloadJavadocs>false</downloadJavadocs>
    <!-- Avoid type mvn eclipse:eclipse -Dwtpversion=2.0 -->
    <wtpversion>2.0</wtpversion>
   </configuration>
    </plugin>

    <!-- Set JDK Compiler Level -->
    <plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-compiler-plugin</artifactId>
   <version>2.3.2</version>
   <configuration>
    <source>${jdk.version}</source>
    <target>${jdk.version}</target>
   </configuration>
    </plugin>

    <!-- For Maven Tomcat Plugin -->
    <plugin>
   <groupId>org.apache.tomcat.maven</groupId>
   <artifactId>tomcat7-maven-plugin</artifactId>
   <version>2.2</version>
   <configuration>
    <path>/CounterWebApp2</path>
   </configuration>
    </plugin>
  </plugins>
 </build>
</project>

 

2.修改web.xml

<?xml version="1.0" encoding="UTF-8"?>
 <web-app xmlns="http://java.sun.com/xml/ns/javaee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
       http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
 version="2.5">

<display-name>Boc Web Application</display-name>

 <servlet>
  <servlet-name>mvc-dispatcher</servlet-name>
  <servlet-class>
                        org.springframework.web.servlet.DispatcherServlet
                </servlet-class>
  <load-on-startup>1</load-on-startup>
 </servlet>

 <servlet-mapping>
  <servlet-name>mvc-dispatcher</servlet-name>
  <url-pattern>/</url-pattern>
 </servlet-mapping>

 <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
 </context-param>

 <listener>
  <listener-class>
                   org.springframework.web.context.ContextLoaderListener
                </listener-class>
 </listener>
</web-app>

 3. eclipse ide支援:進入bocsearch所在目錄執行以下命令:

     mvn eclipse:eclipse -Dwtpversion=2.0 注意,此選項 -Dwtpversion=2.0 告訴 Maven 將項目轉換到 Eclipse 的 Web 項目(WAR),而不是預設的Java項目(JAR)。為方便起見,以後我們會告訴你如何配置 pom.xml 中的這個 WTP 選項。

3.2 匯入到 Eclipse IDE – File -> Import… -> General -> Existing Projects into workspace.將項目匯入到eclipse中

4.修改pom設定檔:

 

<plugins>    <!-- Eclipse project -->  <plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-eclipse-plugin</artifactId><version>2.9</version><configuration>        <!-- Always download and attach dependencies source code --><downloadSources>true</downloadSources><downloadJavadocs>false</downloadJavadocs><!-- Avoid type mvn eclipse:eclipse -Dwtpversion=2.0 --><wtpversion>2.0</wtpversion></configuration>  </plugin>

注意,為方便起見,聲明 maven-eclipse-plugin,並配置wtpversion 來避免輸入參數 -Dwtpversion=2.0。現在,每次使用 mvn eclipse:eclipse,Maven將這個項目匯入轉換為 Eclipse Web 項目。

 

#之前 mvn eclipse:eclipse --> Eclipse Java project (JAR)mvn eclipse:eclipse -Dwtpversion=2.0 --> Eclipse Java web project (WAR)#之後mvn eclipse:eclipse --> Eclipse Java web project (WAR)

 

5.更新代碼:

在這一步中,在上一步配置完pom.xml後,重新執行 mvn eclipse:eclipse 這個命令,我們將建立Spring MVC的一些檔案和logback日誌架構的檔案夾。


注意,如果它不存在,需要手動建立檔案夾。

/src/main/java/cn.dinfo.boc.bocsearch.controller/LoginController.java

@Controller
public class LoginController {
 private static int counter = 0;
 private static final String VIEW_INDEX = "result";
 private final static org.slf4j.Logger logger = LoggerFactory.getLogger(LoginController.class);

 @RequestMapping(value = "/login", method = RequestMethod.GET)
 public String welcome(ModelMap model) {

  model.addAttribute("message", "Welcome ljf");
  model.addAttribute("counter", ++counter);
  logger.debug("[welcome] counter : {}", counter);

  // Spring uses InternalResourceViewResolver and return back index.jsp
  return VIEW_INDEX;

 }
}

 

5.2 建立Spring設定檔。 /src/main/webapp/WEB-INF/mvc-dispatcher-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:context="http://www.springframework.org/schema/context"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="
        http://www.springframework.org/schema/beans    
        http://www.springframework.org/schema/beans/spring-beans.xsd         http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

 <context:component-scan base-package="cn.dinfo.boc.bocsearch.controller" />
 <bean
  class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  <property name="prefix">
   <value>/WEB-INF/pages/</value>
  </property>
  <property name="suffix">
   <value>.jsp</value>
  </property>
 </bean>

</beans>

5.3 更新讓現有的 web.xml 支援 Servlet 2.5(預設的Servlet2.3 太舊了), 並且還通過 Spring 監聽器 ContextLoaderListener 整合了Spring架構。 /src/main/webapp/WEB-INF/web.xml

 

 

<web-app xmlns="http://java.sun.com/xml/ns/javaee"         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/javaee       http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"version="2.5"><display-name>Counter Web Application</display-name><servlet><servlet-name>mvc-dispatcher</servlet-name><servlet-class>                        org.springframework.web.servlet.DispatcherServlet                </servlet-class><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>mvc-dispatcher</servlet-name><url-pattern>/</url-pattern></servlet-mapping><context-param><param-name>contextConfigLocation</param-name><param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value></context-param><listener><listener-class>                   org.springframework.web.context.ContextLoaderListener                </listener-class></listener></web-app>

 

5.4 移動檔案 result.jsp 到 WEB-INF/pages 目錄下, 為了保護直接存取。並更新內容:

5.5 在資源檔夾(resources)中建立 logback.xml 檔案(將下面的語句複製到空的xml檔案命名logback.xml)啟動報錯,之後刪除了此檔案 /src/main/resources/logback.xml

 

<?xml version="1.0" encoding="UTF-8"?> 

6.打包運行

 1.

6.1 要編譯,測試和項目打包成一個WAR檔案,輸入:

 

mvn package

 

一個新的 WAR 檔案將在 project/target/BocWebApp.war產生,只需複製並部署到Tomcat 發布的目錄。

 

2.

如果想通過 Eclipse 伺服器這個項目外掛程式(Tomcat 或其它容器)調試,這裡再輸入:

 

mvn eclipse:eclipse

如果一切順利,該項目的依賴將被裝配附加到 Web部署項目。圖片: 右鍵點擊 project -> Properties -> Deployment Assembly

6.3 Maven 的 Tomcat 外掛程式聲明(加入到 pom.xml): pom.xml

 

<!-- For Maven Tomcat Plugin --> 

  <!-- For Maven Tomcat Plugin -->
    <plugin>
   <groupId>org.apache.tomcat.maven</groupId>
   <artifactId>tomcat7-maven-plugin</artifactId>
   <version>2.2</version>
   <configuration>
    <path>/CounterWebApp</path>
   </configuration>
    </plugin>

輸入命令:

 

mvn tomcat:run

 

13:59:18.538 [main] DEBUG o.s.c.e.PropertySourcesPropertyResolver - Searching for key 'spring.liveBeansView.mbeanDomain' in [systemEnvironment]
13:59:18.541 [main] DEBUG o.s.c.e.PropertySourcesPropertyResolver - Could not find key 'spring.liveBeansView.mbeanDomain' in any property source.
13:59:18.543 [main] DEBUG o.s.web.servlet.DispatcherServlet - Published WebApplicationContext of servlet 'mvc-dispatcher' as ServletContext attrib
13:59:18.545 [main] INFO  o.s.web.servlet.DispatcherServlet - FrameworkServlet 'mvc-dispatcher': initialization completed in 797 ms
13:59:18.548 [main] DEBUG o.s.web.servlet.DispatcherServlet - Servlet 'mvc-dispatcher' configured successfully
三月 11, 2016 1:59:18 下午 org.apache.coyote.http11.Http11Protocol init
INFO: Initializing Coyote HTTP/1.1 on http-8080
三月 11, 2016 1:59:18 下午 org.apache.coyote.http11.Http11Protocol start
INFO: Starting Coyote HTTP/1.1 on http-8080

這將啟動Tomcat,部署項目預設在連接埠8080。

詳情參考網址:http://www.yiibai.com/maven/maven_environment_setup.html

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.