天天看點

maven deploy plugin_輕松了解maven是什麼

定義

maven是基于項目對象模型(POM-project object model),可以通過一段描述來管理項目建構的軟體項目管理工具。

也就是說:maven可以幫我們優雅的引(抄)用前人實作過的jar包,以及建構自己項目的結構。

說到引用,不得不提maven中兩個非常重要的概念:倉庫,坐标。

舉個例子,如果說他人的jar包是一個包裹,我們要用它,就得知道包裹在什麼倉庫的什麼位置,知道之後把它拿過來拷貝一份放在自己的倉庫裡,豈不美哉。

這裡的倉庫就分為遠端倉庫和本地倉庫,遠端倉庫就是人家包裹的老巢(比如maven全球中央倉庫:https://search.maven.org/,id:central),本地倉庫就是自己的窩,坐标位置就是倉庫的貨架号,用于定位到實際的包裹。

好了,既然我們拿到包裹了,該怎麼用呢?

先看如下一個maven配置檔案示例:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://maven.apache.org/POM/4.0.0"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <!-- 指定了目前pom的版本 -->
    <modelVersion>4.0.0</modelVersion>

    <!-- 坐标 -->
    <!-- 通常為反寫的公司網址+項目名 -->
    <groupId>test.maven</groupId>
    <!-- 項目名+子產品名 -->
    <artifactId>helloworld</artifactId>
    <!-- 版本号,大版本号+分支版本号+小版本号 -->
    <version>1.0.0-SNAPSHOT</version>
    <!-- 預設是jar,還有war/zip/pom -->
    <package></package>

    <!-- 繼承父項目 -->
    <parent>
        <groupId>test.maven</groupId>
        <artifactId>helloworld.parent</artifactId>
        <version>1.0.0-SNAPSHOT</version>
    </parent>

    <!-- 聚合子項目 -->
    <modules>
        <module>module1</module>
    </modules>

    <!-- 屬性 -->
    <properties>
        <app.version>1.0.0-SNAPSHOT</app.version>
    </properties>

    <!-- 依賴清單 -->
    <dependencies>
        <dependency>
            <groupId>test.maven</groupId>
            <artifactId>helloworld.dependency</artifactId>
            <version>1.0.0-SNAPSHOT</version>
            <!-- 依賴範圍,可引入3種classpath:編譯,測試,運作。預設為compile,編譯測試運作都生效 -->
            <scope>test</scope>
        </dependency>
    </dependencies>

    <!-- 并不在項目中運作,隻做依賴的管理,便于子子產品引用 -->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>test.maven</groupId>
                <artifactId>helloworld.dependency</artifactId>
                <version>1.0.0-SNAPSHOT</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>

        <pluginManagement>
            <!-- 項目全局的Maven插件管理 -->
            <plugins>
                <plugin>
                    <artifactId>maven-source-plugin</artifactId>
                    <version>2.4</version>
                    <configuration>
                        <attach>true</attach>
                    </configuration>
                    <executions>
                        <execution>
                            <!-- 執行階段 -->
                            <phase>compile</phase>
                            <goals>
                                <goal>jar</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </pluginManagement>

        <plugins>
            <!-- 插件清單 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>3.0.0-M1</version>
            </plugin>
        </plugins>
    </build>

    <!-- 定義遠端snapshots庫和releases庫的nexus位址 -->
    <distributionManagement>
        <repository>
            <id>releases</id>
            <url>http://</url>
        </repository>
        <snapshotRepository>
            <id>snapshots</id>
            <url>http://</url>
        </snapshotRepository>
    </distributionManagement>
</project>
           

mvn install 會将項目生成的構件安裝到本地Maven倉庫

mvn deploy 用來将項目生成的構件分發到遠端Maven倉庫

本地Maven倉庫的構件隻能供目前使用者使用,在分發到遠端Maven倉庫之後,所有能通路該倉庫的使用者都能使用你的構件。是以我們需要配置POM的<distributionManagement>來指定Maven遠端分發構件的位置。

setting.xml

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

<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">

    <!-- 本地倉庫位址,可配 -->
	<localRepository>/Users/liliang/.m2/repository</localRepository>

    <!-- 遠端倉庫,distributionManagement的鑒權資訊 -->
    <servers>
        <server>
            <id>snapshots</id>
            <username>admin</username>
            <password>12345</password>
        </server>
        <server>
            <id>releases</id>
            <username>admin</username>
            <password>12345</password>
        </server>
    </servers>

  <!-- 鏡像伺服器 -->
  <mirrors>
   <mirror>
      <id>ele-pub</id>
      <!-- 指定具體為哪個伺服器提供鏡像服務,可用*通配符表示所有 -->
      <mirrorOf>*</mirrorOf>
      <name></name>
      <url>http://</url>
    </mirror>
  </mirrors>

</settings>
           

配置檔案配好後,我們就可以使用maven的指令操作了

maven deploy plugin_輕松了解maven是什麼

maven的指令有一個生命周期,是作者根據軟體工程經驗構思出來的,分為三個階段:

  • clean

清理項目,将本地target目錄下之前編譯好的檔案全部清空

  • default

核心階段,包括complie,test,package,intall

  • site

生成項目站點,部署到伺服器上,包括deploy

通過這3個階段的maven指令後,就能夠建構部署好一個項目了。