Maven運行測試


 
Maven運行用於測試中的最佳實踐(我的認爲,呵呵)
 
一、建立maven工程
mvn archetype:create     -DgroupId=com.yourcompany    -DartifactId=myproject    -DarchetypeArtifactId=maven-archetype-quickstart
 
demo
mvn archetype:create -DgroupId=com.alibaba -DartifactId=seleniumdemo02 -DarchetypeArtifactId=maven-archetype-quickstart

讓生成工程的標準模板到外面倉庫去取
mvn archetype:generate -Pexternal

生成標準的dubbo、roma等的標準工程
mvn scaffold:create

mvn archetype:generate出錯
[INFO] ------------------------------------------------------------------------
[ERROR] BUILD ERROR
[INFO] ------------------------------------------------------------------------
[INFO] Internal error in the plugin manager executing goal 'org.apache.maven.plugins:maven-archetype-plugin:2.0-alpha-5-SNAPSHOT:generate': Unable to
load the mojo 'org.apache.maven.plugins:maven-archetype-plugin:2.0-alpha-5-SNAPSHOT:generate' in the plugin 'org.apache.maven.plugins:maven-archetype
plugin'. A required class is missing: org/codehaus/plexus/util/xml/XmlStreamReader
org.codehaus.plexus.util.xml.XmlStreamReader
[INFO] ------------------------------------------------------------------------
[INFO] For more information, run Maven with the -e switch
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 12 seconds
[INFO] Finished at: Mon Oct 12 00:00:15 CST 2009
[INFO] Final Memory: 8M/15M
[INFO] ------------------------------------------------------------------------
一條標準得不能再標準的命令,獲得的結果倒是一對看不懂的出錯信息,實在使人沮喪。其實出錯的緣由很簡單,maven-archetype-plugin沒有被認爲是Maven的核心插件,也就沒有在超級POM中爲其設定版本,因而,咱們運行archetype命令的時候,maven就去中央倉庫下載最新的SNAPSHOT,而偏偏這個SNAPSHOT是有問題的,是徹底沒法工做的,因而咱們看到了上面的結果。
 
解決方案很簡單,就是有點煩,咱們須要在運行archetype命令的時候指定其版本,命令以下: 
mvn org.apache.maven.plugins:maven-archetype-plugin:2.0-alpha-4:generate
 
指定groupId, artifactId, version,2.0-alpha-4是目前的最新版本,試試吧,如今archetype又能正常工做了,他會提示你一大堆可選的archetype類型,我看到了41個之多,我的仍是最喜歡默認的 15: internal -> maven-archetype-quickstart () ,在須要一個簡單的Maven項目進行測試的時候,很是有用。

二、建立資源文件
maven直接建立出來的工程,是不帶resource目錄的,使用起來不是很是的方便。
mkdir -p src/{main/java/com/mycompany/webapp,test/java/com/mycompany/webapp,test/resources}
【注意】這是在linux下運行的, 而在windows下面使用'\',且不用'-p'

Windows下demo
mkdir src\main\resources,src\test\resources
再使用mvn eclipse:eclipse以後,再導入eclipse中(若是已經在eclipse中的,只須要刷新整個eclipse工程便可)

windows下,建立完整的目錄結構
mkdir src\main\java\com\alibaba, src\main\resources, src\test\java\com\alibaba,src\test\resources

三、指定編譯級別, 同時注意eclipse的編譯級別
<build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <target>1.5</target>
                    <source>1.5</source>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>


三、在java目錄下面寫java的代碼,在測試下面寫集成測試的代碼
【注意】測試代碼的源代碼類的名稱中必須以‘Test’開頭或者結尾

四、使用mvn執行測試
mvn clean test

五、與頁面自動化的程序進行集成

六、Maven實戰
在運行測試時你有可能注意到
log4j:WARN No appenders could be found for logger (org.apache.cxf.bus.spring.BusApplicationContext).
log4j:WARN Please initialize the log4j system properly.
 
這是由於沒有加入log4j配置文件的緣故,通常狀況下,配置文件應當放在運行時的classpath中,即項目的target目錄中。但若是運行mvn clean的話target文件夾將被刪去,若是手動拷貝配置文件的話,麻煩不說還有可能形成版本的不一致。此時應當使用maven提供的資源管理功能,即將log4j.properties放在src/main/resources中,maven會自動將該文件放到項目的target目錄中(詳情參見上文中關於maven 命令lifecycle的講解),若是配置文件只在測試中被使用可將其放在src/test/resources中。
 
2.         引入spring 管理服務的客戶端
將spring的配置文件client-beans.xml放入src/main/resources中,代碼詳細附錄(二)
 
將org.world.hello.apps.cxf.bookstore.test.BookStoreServiceTest文件中的setUp方法替換爲
    public void setUp() {
//        JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
//        factory.getInInterceptors().add(new LoggingInInterceptor());       
//        factory.getOutInterceptors().add(new LoggingOutInterceptor());
//        factory.setServiceClass(BookStore.class);
//        factory.setAddress("http://localhost:9000/bookStore");
//        setService((BookStore) factory.create());
       
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
                new String[]{"client-beans.xml"});
 
        service = (BookStore)context.getBean("client");   
}
並添加 import org.springframework.context.support.ClassPathXmlApplicationContext;
 
啓動(/重啓)Server,執行mvn test驗證一切正常。
 
你有可能會問‘我並無在pom.xml中引入spring的包,爲何可使用spring呢?’;答案在於你所用的包是由maven從repository中下載過來的,其依賴性也由maven所管理,而cxf項目自己便依賴與spring框架,因此spring也被加入到你的classpath中了;你能夠從eclipse中看到你的項目依賴於哪些包。
 
 
3.         引入spring 管理服務的服務端
建立src/main/webapp/WEB-INF文件夾。
 
建立文件src/main/webapp/WEB-INF/web.xml,內容以下:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
    PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
    "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>    
</web-app>
 
建立文件src/main/webapp/index.html,內容以下:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content= "text/html; charset=GB18030">
<title>Insert title here</title>
</head>
<body>
    Hello from index.html
</body>
</html>
 
修改pom.xml,添加如下內容(添加了jetty的maven插件,並指定了在9090端口啓動jetty)
<plugins>
……
           <plugin>
              <groupId>org.mortbay.jetty</groupId>
              <artifactId>maven-jetty-plugin</artifactId>
              <configuration>
                  <connectors>
                     <connector
                         implementation="org.mortbay.jetty.nio.SelectChannelConnector">
                         <port>9090</port>
                     </connector>
                  </connectors>
              </configuration>
    </plugin>
……
</plugins>
<finalName>start_off</finalName>
 
 
執行命令
$mvn jetty:run
 
你將能看到「Hello from index.html」字樣,啓動的server可用ctrl+c來中止。
 
修改web.xml,內容以下
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
    PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
    "http://java.sun.com/dtd/web-app_2_3.dtd">
   
<web-app>    
    <context-param>
     <param-name>contextConfigLocation</param-name>
     <param-value>WEB-INF/beans.xml</param-value>
 </context-param>
 
 <listener>
     <listener-class>
         org.springframework.web.context.ContextLoaderListener
     </listener-class>
 </listener>
 
 <servlet>
     <servlet-name>CXFServlet</servlet-name>
     <display-name>CXF Servlet</display-name>
     <servlet-class>
         org.apache.cxf.transport.servlet.CXFServlet
     </servlet-class>
     <load-on-startup>1</load-on-startup>
 </servlet>
 
 <servlet-mapping>
     <servlet-name>CXFServlet</servlet-name>
     <url-pattern>/services/*</url-pattern>
 </servlet-mapping>
    
</web-app>
其中指定了使用WEB-INF/beans.xml做爲cxf的配置文件,並將服務掛載在/services/*上。
 
下面建立WEB-INF/beans.xml
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:jaxws="http://cxf.apache.org/jaxws"
 xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd">
 <import resource="classpath:META-INF/cxf/cxf.xml" />
 <import resource= "classpath:META-INF/cxf/cxf-extension-soap.xml" />
 <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
 
 <jaxws:endpoint address="/bookStore" serviceName="t:bookStoreService"
     xmlns:t="http://bookstore.hello.world.org">
     <jaxws:implementor>
         <bean
            class= "org.world.hello.apps.cxf.bookstore.server.BookStoreServiceImpl" />
     </jaxws:implementor>
 
     <jaxws:inInterceptors>
         <bean
            class= "org.apache.cxf.interceptor.LoggingInInterceptor" />
     </jaxws:inInterceptors>
     <jaxws:outInterceptors>
         <bean
            class= "org.apache.cxf.interceptor.LoggingOutInterceptor" />
     </jaxws:outInterceptors>
 
 </jaxws:endpoint>
 
</beans>
 
其中的jaxws:endpoint指定了服務的終端,address爲「/ bookStore」表示,服務被髮布在 http://127.0.0.1:9090/start_off/services/bookStore ,serviceName和它的namespace應與在org.world.hello.apps.cxf.bookstore.server.BookStore.java中指定的相同。
jaxws:implementor中指定了服務的實現者。
 
執行命令
$mvn jetty:run
 
http://127.0.0.1:9090/start_off/services/bookStore?wsdl 能夠看到已發佈服務的wsdl。
 
修改client-beans.xml將proxyFactory的address屬性從新指定爲「http://127.0.0.1:9090/start_off/services/bookStore」
在另外一個命令行
$mvn test
 
若是經過測試,說明服務已經正常發佈在jetty上了。
 
 
在pom.xml中更改打包的格式,即將<packaging>jar</packaging>改成<packaging>war</packaging>。
 
執行命令
$mvn package
 
* 注: package 指令在 maven 中的生命期在 test 以後,因此執行 package 時會自動執行 test ,即只有經過了測試才能打包。
 
執行完打包指令以後,在項目的target目錄中就能夠看到start_off-1.0.war,要將工程部署在tomcat上,只需將war文件拷貝到webapps文件夾中便可。
 
 

2、拆分你的第一個maven2項目爲多個子項目的組合

修改pom.xml中的<packaging>war</packaging>爲<packaging>pom</packaging>
 
執行命令(要在同一行中寫)
mvn archetype:create -Dversion=1.0 -DgroupId=org.world.hello.apps -DartifactId=start_off_api
 
執行以上命令後,在start_off項目的pom.xml中會自動添加<modules><module>start_off_api</module></modules>
,代表start_off_api項目爲本項目的子項目。
 
同時在start_off項目的文件夾中會生成start_off_api項目的文件結構。
 
查看start_off_api項目的pom.xml,也能夠看出它和start_off項目的關係。
 <parent>
    <artifactId>start_off</artifactId>
    <groupId>org.world.hello.apps</groupId>
    <version>1.0</version>
 </parent
 
執行命令       
$mvn eclipse:eclipse
能夠發現一條指令同時在操做兩個項目。
輸出以下結果:
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO] ------------------------------------------------------------------------
[INFO] start_off ............................................. SUCCESS [3.734s]
[INFO] start_off_api ......................................... SUCCESS [5.531s]
[INFO] ------------------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 9 seconds
[INFO] Finished at: Mon Nov 26 10:24:54 CST 2007
[INFO] Final Memory: 9M/17M
 
以後可將start_off_api項目也導入eclipse。
 
 
同理建立start_off_web項目。
執行命令(要在同一行中寫)
mvn archetype:create -Dversion=1.0 -DgroupId=org.world.hello.apps -DartifactId=start_off_web
 
 
將沒有用的App.java和AppTest.java刪除。
將start_off項目的src/main/webapp文件夾移到start_off_web項目中。
將start_off項目src中的剩餘內容移到start_off_api項目的src文件夾中。
 
註釋掉org.world.hello.apps.cxf.bookstore.test. BookStoreServiceTest中的有效測試,添加一個假的測試(目的是跳過maven生命週期的測試階段,而執行下面任務)
    public void testTruth() {
        assertTrue( true);
}
 
修改start_off_web項目的pom.xml文件,修改後的內容以下:
<?xml version="1.0" encoding="UTF-8"?>
<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">
 <parent>
     <artifactId>start_off</artifactId>
     <groupId>org.world.hello.apps</groupId>
     <version>1.0</version>
 </parent>
 <modelVersion>4.0.0</modelVersion>
 <groupId>org.world.hello.apps</groupId>
 <artifactId>start_off_web</artifactId>
 <name>start_off_web</name>
 <version>1.0</version>
 <packaging>war</packaging>
 <url>http://maven.apache.org</url>
 <dependencies>
     <dependency>
         <groupId>org.world.hello.apps</groupId>
         <artifactId>start_off_api</artifactId>
         <version>1.0</version>
     </dependency>
 </dependencies>
 
 <build>
     <finalName>start_off_web</finalName>
 
     <plugins>
         <plugin>
            <groupId>org.mortbay.jetty</groupId>
            <artifactId>maven-jetty-plugin</artifactId>
            <configuration>
                <connectors>
                   <connector
                       implementation="org.mortbay.jetty.nio.SelectChannelConnector">
                       <port>9090</port>
                   </connector>
                </connectors>
            </configuration>
         </plugin>
 
     </plugins>
 </build>
</project>
 
* 注:其中指出了start_off_web 項目依賴於 start_off_api 項目
 
修改start_off_api項目的pom.xml文件,修改後的內容以下:
<?xml version="1.0" encoding="UTF-8"?>
<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">
 <parent>
     <artifactId>start_off</artifactId>
     <groupId>org.world.hello.apps</groupId>
     <version>1.0</version>
 </parent>
 <modelVersion>4.0.0</modelVersion>
 <groupId>org.world.hello.apps</groupId>
 <artifactId>start_off_api</artifactId>
 <name>start_off_api</name>
 <version>1.0</version>
 <packaging>jar</packaging>
 <url>http://maven.apache.org</url>
 <dependencies>
 </dependencies>
 
 <build>
     <plugins>
         <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <configuration>
                <archive>
                   <manifest>
                       <mainClass>
                          org.world.hello.apps.cxf.bookstore.bin.Server
                       </mainClass>
                   </manifest>
                </archive>
            </configuration>
         </plugin>
     </plugins>
 </build>
</project>
 
* 注:子項目所依賴的包可從父項目中間接獲得
 
在start_off項目中的pom.xml中刪去插件maven-jetty-plugin和maven-jar-plugin。
 
執行命令
$mvn clean eclipse:clean eclipse:eclipse
$mvn install
 
以上的install命令,將項目安裝到了本地的repository中。
 
在start_off_web項目的根目錄下執行命令mvn jetty:run即可啓動服務,wsdl在 http://127.0.0.1:9090/start_off_web/services/bookStore?wsdl
 
若想將服務部署到其餘server上,可在start_off項目的根目錄下執行mvn package,生成的war被放在start_off_web項目的target目錄中。
 
 
建立start_off_impl子項目,執行命令(要在同一行中寫)
mvn archetype:create -Dversion=1.0 -DgroupId=org.world.hello.apps -DartifactId=start_off_impl
 
將沒有用的App.java和AppTest.java刪除。
修改start_off_impl項目的pom.xml以下
<?xml version="1.0" encoding="UTF-8"?>
<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">
 <parent>
     <artifactId>start_off</artifactId>
     <groupId>org.world.hello.apps</groupId>
     <version>1.0</version>
 </parent>
 <modelVersion>4.0.0</modelVersion>
 <groupId>org.world.hello.apps</groupId>
 <artifactId>start_off_impl</artifactId>
 <name>start_off_impl</name>
 <version>1.0</version>
 <packaging>jar</packaging>
 <url>http://maven.apache.org</url>
 <dependencies>
     <dependency>
         <groupId>org.world.hello.apps</groupId>
         <artifactId>start_off_api</artifactId>
         <version>1.0</version>
     </dependency>
 </dependencies>
</project>
 
修改start_off_web項目的pom.xml,添加依賴
       <dependency>
           <groupId>org.world.hello.apps</groupId>
           <artifactId>start_off_impl</artifactId>
           <version>1.0</version>
</dependency>
 
將start_off_api中的
org.world.hello.apps.cxf.bookstore.server. BookStoreServiceImpl.java
org.world.hello.apps.cxf.bookstore.test. BookStoreServiceTest.java
org.world.hello.apps.cxf.bookstore.bin.Server.java
移動到start_off_impl項目中。
 
在start_off項目的根目錄下執行命令
$mvn clean eclipse:clean eclipse:eclipse install
      
以後即可啓動jetty,驗證服務仍可訪問,項目拆分完畢。
相關文章
相關標籤/搜索