maven系列學習之二:maven初體驗-簡單使用maven進行測試,編譯、打包和運行

1、編譯測試java

<dependency>apache

                     ……maven

                     <scope>test</scope>測試

              </dependency>ui

 

        Scope:依賴範圍。若依賴範圍爲test則表示該依賴只對測試有效。若是不聲明依賴範圍,那麼默認值爲compile,表示該依賴對主代碼和測試代碼都有效。spa

 

Mvn clean compile :插件

       執行過程:clean:clean --> resources:resources --> compiler:compileorm

 

Mvn clen testget

       執行過程:clean:clean --> resources:resources --> compiler:compile -->resources:testResources –-> compiler:testCompile --> surefire:testio

Surefiremaven中負責執行測試的插件,會顯示一共運行了多少測試,失敗了多少 ,出錯了多少,跳過了多少。

 

注意:3.1版本及以前的maven核心插件之一compiler插件默認只支持編譯java1.3,所以須要配置該插件使其支持java5,以下:

<project>

       <build>

              <plugins>

                     <plugin>

                            <groupId>org.apache.maven.plugins</groupId>

                            <artifactId>maven-compiler-plugin</artifactId>

                            <configuration>

                                   <source>1.6</source>

                                   <target>1.6</target>

                            </configuration>

                     </plugin>

              </plugins>

       </build>

</project>

 

2、打包和運行

Pom中若是沒有指定打包類型,則默認打包類型爲jar。執行命令mvn clean package可進行打包。Maven在打包以前執行編譯、測試等操做後會後執行jar:jar任務負責打包。默認包名命名規則爲:artifact-version.jar

 

打包完執行mvn clean install 可將該bao安裝到maven庫中,供其餘maven項目直接引用。

 

打包可運行程序

默認打包生成的jar是不可以直接運行的,由於帶有main方法的類信息不回添加到manifest中。爲了生成可執行的jar文件,須要藉助maven-shade-plugin,配置位置:<project><build><plugins>配置以下:

<plugin>

       <groupId>org.apache.maven.plugins</groupId>

       <artifactId>maven-shade-plugin</artifactId>

       <version>1.2.1</version>

       <executions>

              <execution>

                     <phase>package</phase>

                     <goals>

                            <goal>shade</goal>

                     </goals>

                     <configuration>

                            <transformers>

                                   <transformer implementation=」org.apache.maven.plugins.shade.resource.ManifestResourceTransformer」>

                                          <mainClass>com.mycom.mvntest.helloword.HelloWord</mainClass>

                                   <transformer>

                            <transformers>

                     <configuration>

              </execution>

       </executions>

</plugin>

 

打包後執行命令:java-jar target \hello-world-1.0-SNAPSHOT.jar

相關文章
相關標籤/搜索