1. 程式人生 > 實用技巧 >[構建工具] Maven速查手冊

[構建工具] Maven速查手冊

介紹

Maven 是一個跨平臺的專案管理工具,主要用於專案構建,依賴管理,專案資訊管理。自動化構建過程,從清理、編譯、測試和生成報告、再到打包和部署。Maven 通過一小段描述資訊來管理專案。


作用

Maven 最熟悉的一個概念就是 POM,Maven 專案會有一個 pom.xml 檔案, 在這個檔案裡面新增相應配置,Maven 就會自動幫你下載相應 jar 包
<dependency>
  <groupId>com.google.firebase</groupId> 專案名
  <artifactId>firebase-admin</artifactId> 專案模組
  <version>5.3.1</version> 專案版本
</dependency>
groupId 定義了專案組,組和專案所在組織或公司,或者開源專案名稱,一般為公司域名反寫,比如 com.google.firebase 等等
artifactId 定義了 Maven 專案的名稱,在組中的唯一 ID,在同一個專案中可能有不同的子專案,可以定義不同的 artifactId。 artifactId 也是構建完成專案後生成的 jar 包或者 war 包的檔名的一部分。
version 顧名思義,就是專案的版本號,如果專案維釋出,一般在開發中的版本號習慣性加上SNAPSHOT, 比如 1.0-SNAPSHOT

依賴範圍

scope 定義了依賴範圍,如果依賴範圍為 test ,那麼該依賴只對測試有效,也就是說在測試程式碼中引入 junit 有效,在主程式碼中用 junit 會造成編譯錯誤。如果不宣告依賴範圍則預設為 compile ,表示該依賴對主程式碼和測試程式碼都有效。
Maven 有以下幾種依賴範圍:
compile 編譯依賴,在編譯、測試、執行時都有效
test 測試依賴,只對於測試 classpath 有效, JUnit 典型
provided 已提供依賴,只在編譯和測試有效,執行時無效, servlet-api 編譯和測試專案時需要該依賴,但是在執行專案時,由於容器已經提供,不需要 Maven 重複引入
runtime 執行時依賴,對於測試和執行有效,編譯主程式碼無效, JDBC驅動實現,專案主程式碼編譯只需要JDK提供的JDBC介面,只有執行測試或者執行專案才需要實現上述介面的具體JDBC驅動
system 系統依賴範圍,和 provided 範圍依賴一致,但是使用 system 範圍的依賴時必須通過 systemPath 元素顯示地指定依賴檔案的路徑。
import 匯入依賴,一般不用

常用命令

1. mvn compile 編譯原始碼,生成target目錄。
2. mvn test-compile 編譯測試程式碼
3. mvn test 執行測試
5. mvn package 編譯並且打包,根據pom.xml打成war或jar。
6. mvn -Dtest package 打包但不測試。完整命令為:mvn -D maven.test.skip=true package
7. mvn install 在本地Repository中安裝jar
8. mvn clean 清除產生的專案,target沒有了。
9. mvn eclipse:eclipse 生成eclipse專案
10.mvn idea:idea 生成idea專案
11.mvn eclipse:clean 清除eclipse的一些系統設定

跳過測試打包
clean package -Pproduct -DskipTests
clean package -Ptest -DskipTests

自定義構建

maven-assembly-plugin,支援定製化打包方式,例如 apache 專案的打包方式

test_assembly.xml
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
    <id>test</id>
    <formats>
        <format>zip</format>
    </formats>
    <fileSets>
        <fileSet>
            <directory>${project.basedir}/src/main/resources</directory>
            <excludes>
                <!-- 包括哪些配置檔案 -->
                <exclude>log4j.properties</exclude>
                <exclude>spring-dao.xml</exclude>
                <exclude>spring-rabbitmq.xml</exclude>
                <exclude>corebeans.xml</exclude>
            </excludes>
            <outputDirectory>/</outputDirectory>
        </fileSet>
        <fileSet>
            <directory>${project.basedir}/src/profiles/test</directory>
            <outputDirectory>/</outputDirectory>
        </fileSet>
        <fileSet>
            <directory>${project.build.directory}</directory>
            <outputDirectory>/</outputDirectory>
            <includes>
                <include>*.jar</include>
            </includes>
        </fileSet>
        <fileSet>
            <directory>${project.basedir}/src/bin</directory>
            <outputDirectory>/bin</outputDirectory>
        </fileSet>
    </fileSets>
    <dependencySets>
        <dependencySet>
            <useProjectArtifact>true</useProjectArtifact>
            <outputDirectory>lib</outputDirectory>
            <excludes>
                <exclude>${groupId}:${artifactId}</exclude>
            </excludes>
            <!-- 將scope為runtime的依賴包打包到lib目錄下。 -->
            <scope>runtime</scope>
        </dependencySet>
    </dependencySets>
</assembly>
pom.xml
<build>
    <plugins>

        <plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <version>0.7.9</version>

            <executions>
                <execution>
                    <id>pre-test</id>
                    <goals>
                        <goal>prepare-agent</goal>
                    </goals>
                </execution>
                <execution>
                    <id>post-test</id>
                    <phase>test</phase>
                    <goals>
                        <goal>report</goal>
                    </goals>
                    <configuration>
                        <excludes>
                            <exclude>com.iminer.main.*</exclude>
                        </excludes>
                    </configuration>
                </execution>
            </executions>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
                <encoding>UTF8</encoding>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.5</version>
            <configuration>
                <!--1、跳過單元測試(不編譯);2、跳過單元測試(編譯)
                    <skip>true</skip>
                    <skipTests>true</skipTests>
                 -->
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.6</version>
            <configuration>
                <archive>
                    <manifest>
                        <!-- 指定main入口 -->
                        <mainClass>com.iminer.main.Main</mainClass>
                        <addClasspath>true</addClasspath>
                        <classpathPrefix>lib/</classpathPrefix>
                    </manifest>
                    <manifestEntries>
                        <Class-Path>./</Class-Path>
                    </manifestEntries>
                </archive>
                <excludes>
                    <exclude>*.xml</exclude>
                    <exclude>*.properties</exclude>
                </excludes>
            </configuration>
        </plugin>
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <executions>  <!--執行器 mvn assembly:assembly -->
                <execution>
                    <id>make-zip</id><!--名字任意 -->
                    <phase>package</phase><!-- 繫結到package生命週期階段上 -->
                    <goals>
                        <goal>single</goal><!-- 只執行一次 -->
                    </goals>
                    <configuration>
                        <descriptors> <!--描述檔案路徑 -->
                            <descriptor>src/assembly/${dao_xml}_assembly.xml</descriptor>
                        </descriptors>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>