maven的使用記錄

Maven是一個項目管理及項目描述工具;它能使項目管理更簡單及標準化;
它的構建生命週期有:準備資源-驗證-編譯-測試-打包-安裝-發佈

maven有三種構建配置文件
1、每個項目有 pom.xml這個配置文件;
2、每個用戶有%USER_HOME%/.m2/settings.xml 這個配置文件,如當前用戶可以cat ~/.m2/settings.xml查看配置內容;
3、還有一個全局的配置文件%M2_HOME%/conf/settings.xml,如用mac中的brew安裝的maven可以通過cat /usr/local/Cellar/maven/3.5.4/libexec/conf/settings.xml這個方式查看配置內容;

(1)mac 安裝maven

brew search maven
brew install maven
mvn -v 

(2)生成本地java項目,項目名叫testweb

mvn archetype:generate -DgroupId=www.testweb.webapp -DartifactId=testweb -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false
#archetype 是maven內置的一個插件
#DgroupId 包名
#DartifactId 項目名
#DarchetypeArtifactId 項目生成結構目前經常用到的值有
#   maven-archetype-quickstart  Java Project項目結構
#   maven-archetype-webapp   JavaWeb Project項目結構

上面命令執行完會生成下面的目錄結構
testweb
├── pom.xml
└── src
└── main
├── resources
└── webapp
├── WEB-INF
│   └── web.xml
└── index.jsp
pom.xml (pom:project object model)裏面有項目構建信息,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.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <!--指定包名-->
  <groupId>www.testweb.webapp</groupId>
  <!--指定項目名-->
  <artifactId>testweb</artifactId>
  <!--指定最後的打包方式-->
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>testweb Maven Webapp</name>
  <url>http://maven.apache.org</url>

  <!--在需要上傳jar資源的項目時配置distributionManagement,其中<id>設置需要跟.m2文件夾下的settings.xml中<servers>下的id相同。<url>需要指定nexus中配置的hosts Repository資源的地址;配置了這個maven 編譯項目,並通過mvn deploy 來發布jar資源內部的鏡像服務器-->
  <distributionManagement>
        <repository>
            <id>server_id</id>
            <name>myNexus</name>
            <url>http://nexus_ip:8081/repository/host-releases/</url>
        </repository>
  </distributionManagement>

  <!--指定外部依賴-->
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
         <!--外部依賴的包名-->
         <groupId>myjdk</groupId>
         <!--外部依賴的項目名-->
         <artifactId>myjdk</artifactId>
         <!--指定該外部依賴的使用範圍,可選擇的值有compile,provided,provided,test,system,import-->
         <!--具體參考http://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html-->
         <scope>system</scope>
         <version>1.0</version>
         <!--指定依賴的包路徑-->
         <systemPath>${basedir}\src\lib\myjdk.jar</systemPath>
     </dependency>
  </dependencies>
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-antrun-plugin</artifactId>
        <version>1.1</version>
        <executions>
          <execution>
            <id>id.pre-clean</id>
            <phase>pre-clean</phase>
            <goals>
              <goal>run</goal>
            </goals>
            <configuration>
              <tasks>
                <echo>pre-clean phase</echo>
              </tasks>
            </configuration>
          </execution>

          <execution>
            <id>id.clean</id>
            <phase>clean</phase>
            <goals>
              <goal>run</goal>
            </goals>
            <configuration>
              <tasks>
                <echo>clean phase</echo>
              </tasks>
            </configuration>
          </execution>

          <execution>
            <id>id.post-clean</id>
            <phase>post-clean</phase>
            <goals>
              <goal>run</goal>
            </goals>
            <configuration>
              <tasks>
                <echo>post-clean phase</echo>
              </tasks>
            </configuration>
          </execution>
          <execution>
            <id>id.validate</id>
            <phase>validate</phase>
            <goals>
              <goal>run</goal>
            </goals>
            <configuration>
              <tasks>
                <echo>validate phase</echo>
              </tasks>
            </configuration>
          </execution>

          <execution>
            <id>id.compile</id>
            <phase>compile</phase>
            <goals>
              <goal>run</goal>
            </goals>
            <configuration>
              <tasks>
                <echo>compile phase</echo>
              </tasks>
            </configuration>
          </execution>

          <execution>
            <id>id.test</id>
            <phase>test</phase>
            <goals>
              <goal>run</goal>
            </goals>
            <configuration>
              <tasks>
                <echo>test phase</echo>
              </tasks>
            </configuration>
          </execution>

          <execution>
            <id>id.package</id>
            <phase>package</phase>
            <goals>
              <goal>run</goal>
            </goals>
            <configuration>
              <tasks>
                <echo>package phase</echo>
              </tasks>
            </configuration>
          </execution>

          <execution>
            <id>id.deploy</id>
            <phase>deploy</phase>
            <goals>
              <goal>run</goal>
            </goals>
            <configuration>
              <tasks>
                <echo>deploy phase</echo>
              </tasks>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
    <finalName>testweb</finalName>
  </build>

  <!--profiles定義各個環境的變量配置-->
  <profiles>

    <profile>
    <!-- 本地開發環境 -->
      <id>dev</id>
      <!--properties自己定義的一些屬性-->
      <properties>

      </properties>
    </profile>
    <profile>
      <activation>
        <activeByDefault>true</activeByDefault>
      </activation>
      <id>prod</id>
      <!-- 發佈環境 -->
      <properties>

      </properties>
    </profile>
  </profiles> 
</project>
1、其中dependencies指定包涵的依賴jar包,packaging是指定發部時打包的方式,如jar、war、ear等
2、基中groupId:artifactId:version用這個在maven的倉庫中標記着一個項目
3、在項目目錄下執行mvn help:effective-pom這條命令可以查看項目的詳細的pom信息
4、build裏是構建過程的配置,這裏引入了一個插入件org.apache.maven.plugins:maven-antrun-plugin:1.1,引入這個插件來在構建過程的pre-clean和clean及post-clean的三個階段各執行一個echo任務,及validate、compile、test、package、deploy這些構建階段也都各執行一個echo;
5、maven有profile功能,爲項目設置不同的配置,如上項目有兩個配置環境一個dev與prod,也就是一個是開發環境配置,一個是生產環境配置。其中prod通過activeByDefault這個設置成了默認配置環境,也就是當mvn package時調用的就是prod的配置環境信息,如果需要調用dev的配置環境信息需要mvn package -P dev,也就是需要通過-P這個參數來指使用的配置環境;像這些指的配置所對應的文件是在放到src/main/resources目錄下的config文件夾下,config下有多個環境的配置文件,命名規則爲是application-環境名稱.properties

(3)

pre-clean 就是指定清除編譯前要做什麼
mvn clean 清除之前編譯過程產生的文件及目錄
mvn post-clean 有三個階段 mvn pre-clean , mvn clean, mvn post-clean;就是指定清除編譯後要做什麼
mvn validate 驗證項目是否正確及需要的信息是否是有用的
mvn compile 編譯項目
mvn test 測試項目
mvn package 打包項目如生成war包或jar包
mvn deploy 佈置項目

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章