1. 程式人生 > >Maven入門2-pom.xml文件與settings.xml文件

Maven入門2-pom.xml文件與settings.xml文件

lifecycle env 方式 lap logs 下載 nvi docs 16px

Maven入門2-pom.xml文件與settings.xml文件

  本文內容來源於官網文檔部分章節,settings.xml文件:參考http://maven.apache.org/settings.html,pom.xml文件參考:http://maven.apache.org/guides/introduction/introduction-to-the-pom.html。

  http://maven.apache.org/pom.html;一個是POM的簡單介紹,一個是詳細介紹。

  下面針對幾個主要的地方做介紹:

  轉載請註明出處:http://www.cnblogs.com/liun1994/

1、settings.xml文件

  settings.xml文件包含多個不同的標簽,用來配置maven的執行,包含兩個settings.xml文件,一個在${user.home}/.m2/settings.xml目錄下,一個在${maven.home}/conf/settings.xml目錄下;第一個稱為global settings,第二個稱為user settings,如果兩個都存在的話,將會合並,並以user settings為主導;settings.xml文件的內容通過配置的環境變量來解釋執行。下面是settings.xml文件的一個預覽:

<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 https://maven.apache.org/xsd/settings-1.0.0.xsd"> <localRepository/> <interactiveMode/> <usePluginRegistry/> <offline
/> <pluginGroups/> <servers/> <mirrors/> <proxies/> <profiles/> <activeProfiles/> </settings>
轉載請註明出處:http://www.cnblogs.com/liun1994/

  1)簡單值配置:

<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
                      https://maven.apache.org/xsd/settings-1.0.0.xsd">
  <localRepository>${user.home}/.m2/repository</localRepository><!-- 系統本地倉庫地址 -->
  <interactiveMode>true</interactiveMode><!-- 是否允許與用戶的輸入進行互動,默認true -->
  <usePluginRegistry>false</usePluginRegistry><!-- 是否允許使用${user.home}/.m2/plugin-registry.xml文件管理插件的版本,默認為false -->
  <offline>false</offline><!-- 是否允許在離線模式運行,這在無法與遠程倉庫連接時很有用 -->
  ...
</settings>

  2)插件組:

    pluginGroup可以有好多個,默認包括org.apache.maven.plugins和org.codehaus.mojo。

<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
                      https://maven.apache.org/xsd/settings-1.0.0.xsd">
  ...
  <pluginGroups>
    <pluginGroup>org.mortbay.jetty</pluginGroup>
  </pluginGroups>
  ...
</settings>

配置插件之後可以在命令行使用:mvn jerry:run


  3)servers配置

    下載和部署的倉庫,由POM文件的repositories和distributionManagement標簽定義,然而某些設置卻不能在這個文件下設置;比如username,password等,這些信息在服務器的settings.xml文件中配置。

<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
                      https://maven.apache.org/xsd/settings-1.0.0.xsd">
  ...
  <servers>
    <server>
      <id>server001</id><!-- 與mirror/repository的id匹配 -->
      <username>my_login</username>
      <password>my_password</password>
      <privateKey>${user.home}/.ssh/id_dsa</privateKey>
      <passphrase>some_passphrase</passphrase>
      <filePermissions>664</filePermissions>
      <directoryPermissions>775</directoryPermissions>
      <configuration></configuration>
    </server>
  </servers>
  ...
</settings>

  4)mirrors

    更多關於mirrors的配置可參考此處:http://maven.apache.org/guides/mini/guide-mirror-settings.html

    如果是國內的網,可能會遇到無法訪問的問題,這時可換成國內的mirror。

<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
                      https://maven.apache.org/xsd/settings-1.0.0.xsd">
  ...
  <mirrors>
    <mirror>
      <id>planetmirror.com</id> <!-- 當連接此mirror時使用server的配置 -->
      <name>PlanetMirror Australia</name>
      <url>http://downloads.planetmirror.com/pub/maven2</url>
      <mirrorOf>central</mirrorOf>
    </mirror>
  </mirrors>
  ...
</settings>

  其他的一些配置,可去官網鏈接查詢!

2、pom.xml文件

  1)簡單介紹

  POM文件是Project Object Model的簡稱,它包含了使用Maven進行項目管理的詳細信息,它包含許多默認的值,比如編譯路徑默認為:target;源碼路徑src/main/java;測試路徑:src/test/java。

  所有的pom.xml文件都繼承Super POM,Super POM是Maven默認的Pom,下面是內嵌POM的一個預覽,版本為3.0.4:

  默認POM配置了一些路徑,插件,默認打包方式為jar。轉載請註明出處:http://www.cnblogs.com/liun1994/

技術分享
<project>
  <modelVersion>4.0.0</modelVersion>
 
  <repositories>
    <repository>
      <id>central</id>
      <name>Central Repository</name>
      <url>http://repo.maven.apache.org/maven2</url>
      <layout>default</layout>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
    </repository>
  </repositories>
 
  <pluginRepositories>
    <pluginRepository>
      <id>central</id>
      <name>Central Repository</name>
      <url>http://repo.maven.apache.org/maven2</url>
      <layout>default</layout>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
      <releases>
        <updatePolicy>never</updatePolicy>
      </releases>
    </pluginRepository>
  </pluginRepositories>
 
  <build>
    <directory>${project.basedir}/target</directory>
    <outputDirectory>${project.build.directory}/classes</outputDirectory>
    <finalName>${project.artifactId}-${project.version}</finalName>
    <testOutputDirectory>${project.build.directory}/test-classes</testOutputDirectory>
    <sourceDirectory>${project.basedir}/src/main/java</sourceDirectory>
    <scriptSourceDirectory>src/main/scripts</scriptSourceDirectory>
    <testSourceDirectory>${project.basedir}/src/test/java</testSourceDirectory>
    <resources>
      <resource>
        <directory>${project.basedir}/src/main/resources</directory>
      </resource>
    </resources>
    <testResources>
      <testResource>
        <directory>${project.basedir}/src/test/resources</directory>
      </testResource>
    </testResources>
    <pluginManagement>
      <!-- NOTE: These plugins will be removed from future versions of the super POM -->
      <!-- They are kept for the moment as they are very unlikely to conflict with lifecycle mappings (MNG-4453) -->
      <plugins>
        <plugin>
          <artifactId>maven-antrun-plugin</artifactId>
          <version>1.3</version>
        </plugin>
        <plugin>
          <artifactId>maven-assembly-plugin</artifactId>
          <version>2.2-beta-5</version>
        </plugin>
        <plugin>
          <artifactId>maven-dependency-plugin</artifactId>
          <version>2.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-release-plugin</artifactId>
          <version>2.0</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
 
  <reporting>
    <outputDirectory>${project.build.directory}/site</outputDirectory>
  </reporting>
 
  <profiles>
    <!-- NOTE: The release profile will be removed from future versions of the super POM -->
    <profile>
      <id>release-profile</id>
 
      <activation>
        <property>
          <name>performRelease</name>
          <value>true</value>
        </property>
      </activation>
 
      <build>
        <plugins>
          <plugin>
            <inherited>true</inherited>
            <artifactId>maven-source-plugin</artifactId>
            <executions>
              <execution>
                <id>attach-sources</id>
                <goals>
                  <goal>jar</goal>
                </goals>
              </execution>
            </executions>
          </plugin>
          <plugin>
            <inherited>true</inherited>
            <artifactId>maven-javadoc-plugin</artifactId>
            <executions>
              <execution>
                <id>attach-javadocs</id>
                <goals>
                  <goal>jar</goal>
                </goals>
              </execution>
            </executions>
          </plugin>
          <plugin>
            <inherited>true</inherited>
            <artifactId>maven-deploy-plugin</artifactId>
            <configuration>
              <updateReleaseInfo>true</updateReleaseInfo>
            </configuration>
          </plugin>
        </plugins>
      </build>
    </profile>
  </profiles>
 
</project>
View Code

  最小化配置:

<project>
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.mycompany.app</groupId>
  <artifactId>my-app</artifactId>
  <version>1</version>
</project>

  如果使用最小化配置,沒有指定repositories,那麽將會使用Super POM的配置下載庫及插件,即http://repo.maven.apache.org/maven2。

  幾個例子:Maven中有繼承和聚合的概念,對應兩個標簽:parent, module。

Example1:
目錄結構:
. |-- my-module | `-- pom.xml `-- pom.xml
配置:
<project> <parent> <groupId>com.mycompany.app</groupId> <artifactId>my-app</artifactId> <version>1</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>my-module</artifactId> </project>

Example2:
目錄結構: . |-- my-module | `-- pom.xml `-- parent `-- pom.xml
配置:
<project> <parent> <groupId>com.mycompany.app</groupId> <artifactId>my-app</artifactId> <version>1</version> <relativePath>../parent/pom.xml</relativePath> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>my-module</artifactId> </project>

聚合:

<project>
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.mycompany.app</groupId>
  <artifactId>my-app</artifactId>
  <version>1</version>
  <packaging>pom</packaging>

  <modules>
    <module>my-module</module>
  </modules>
</project>

  詳情參考:http://maven.apache.org/guides/introduction/introduction-to-the-pom.html

  2)標簽預覽

    共分四類,下面介紹幾個比較重要的;

<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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
 
  <!-- The Basics -->
  <groupId>...</groupId>
  <artifactId>...</artifactId>
  <version>...</version>
  <packaging>...</packaging>
  <dependencies>...</dependencies>
  <parent>...</parent>
  <dependencyManagement>...</dependencyManagement>
  <modules>...</modules>
  <properties>...</properties>
 
  <!-- Build Settings -->
  <build>...</build>
  <reporting>...</reporting>
 
  <!-- More Project Information -->
  <name>...</name>
  <description>...</description>
  <url>...</url>
  <inceptionYear>...</inceptionYear>
  <licenses>...</licenses>
  <organization>...</organization>
  <developers>...</developers>
  <contributors>...</contributors>
 
  <!-- Environment Settings -->
  <issueManagement>...</issueManagement>
  <ciManagement>...</ciManagement>
  <mailingLists>...</mailingLists>
  <scm>...</scm>
  <prerequisites>...</prerequisites>
  <repositories>...</repositories>
  <pluginRepositories>...</pluginRepositories>
  <distributionManagement>...</distributionManagement>
  <profiles>...</profiles>
</project>

  在POM文件中,repositories標簽配置去哪下載依賴庫及插件,但是如果在settings.xml配置了mirror的話,就會去mirror地址下載,相當於攔截器;

  Maven倉庫分兩類:

技術分享

  repository裏存放的都是各種jar包和maven插件。當向倉庫請求插件或依賴的時候,會先檢查local repository,如果local repository有則直接返回,否則會向remote repository請求,並緩存到local repository。

  mirror相當於一個攔截器,它會攔截maven對remote repository的相關請求,把請求裏的remote repository地址,重定向到mirror裏配置的地址。

  <mirrorOf></mirrorOf>標簽裏面放置的是要被鏡像的Repository ID。為了滿足一些復雜的需求,Maven還支持更高級的鏡像配置:

3、小結

  下面是我學習Maven時的步驟,同時也適用於其他技術:

  1)搜索相關博客、資料,了解一下概念性的知識;

  2)安裝與簡單配置,閱讀官方文檔中感興趣的部分;

  3)對於不理解的部分,搜索相關的資料,搞明白;

  4)暫時告一段落,下次用到時如果出錯,一方面從官方文檔找配置,另一方面搜索質量高的博客借鑒。

轉載請註明出處:http://www.cnblogs.com/liun1994/

Maven入門2-pom.xml文件與settings.xml文件