【Maven】配置文件小結


本章主題:  細細的探究pom.xml文件的內部構造。針對一個具體的示例對pom.xml 有一個感性的認識。java

<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>
  <!– 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>

    一:基本的設置:apache

        maven的協做相關屬性:
app

    

<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>org.codehaus.mojo</groupId>
  <artifactId>my-project</artifactId>
  <version>1.0</version>
  <packaging>war</packaging>
</project>

   在這裏主要有如下的幾個元素:maven

        

  1. groupId : 組織標識,例如:org.codehaus.mojo,在M2_REPO目錄下,將是: org/codehaus/mojo目錄。ide

  2. artifactId : 項目名稱,例如:my-project,在M2_REPO目錄下,將是:org/codehaus/mojo/my-project目錄。ui

  3. version : 版本號,例如:1.0,在M2_REPO目錄下,將是:org/codehaus/mojo/my-project/1.0目錄。url

  4. packaging : 打包的格式,能夠爲:pom , jar , maven-plugin , ejb , war , ear , rar , parspa


二:Pom之間的關係:操作系統

    主要用於pom文件的複用:插件

1 依賴關係 : 依賴關係列表 : dependency list

<dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.0</version>
      <scope>test</scope>
    </dependency>
    …
  </dependencies>

1 : 最主要的幾個組件名稱仍是 :  groupid,artifacid,version

2 : scope:compile(default),provided,runtime,test,system

3 : exclusion


三: 繼承類型的關係:

繼承關係:繼承其餘pom.xml配置的機制。

好比父pom.xml:

<project>
  [...]
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.4</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  [...]
</project>

在子pom.xml文件繼承它的依賴(還能夠繼承其餘的:developers and contributors、plugin lists、reports lists、plugin executions with matching ids、plugin configuration):

[...]
<parent>
<groupId>com.devzuz.mvnbook.proficio</groupId>
  <artifactId>proficio</artifactId>
  <version>1.0-SNAPSHOT</version>
</parent>
[...]

在這種機制下,maven還提供了一個相似java.lang.Object的頂級父pom.xml文件:

<project>
  <modelVersion>4.0.0</modelVersion>
  <name>Maven Default Project</name>
  <repositories>
    <repository>
      <id>central</id>
      <name>Maven Repository Switchboard</name>
      <layout>default</layout>
      <url>http://repo1.maven.org/maven2</url>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
    </repository>
  </repositories>
  <pluginRepositories>
    <pluginRepository>
      <id>central</id>
      <name>Maven Plugin Repository</name>
      <url>http://repo1.maven.org/maven2</url>
      <layout>default</layout>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
      <releases>
        <updatePolicy>never</updatePolicy>
      </releases>
    </pluginRepository>
  </pluginRepositories>
  <build>
    <directory>target</directory>
    <outputDirectory>target/classes</outputDirectory>
    <finalName>${project.artifactId}-${project.version}</finalName>
    <testOutputDirectory>target/test-classes</testOutputDirectory>
    <sourceDirectory>src/main/java</sourceDirectory>
    <scriptSourceDirectory>src/main/scripts</scriptSourceDirectory>
    <testSourceDirectory>src/test/java</testSourceDirectory>
    <resources>
      <resource>
        <directory>src/main/resources</directory>
      </resource>
    </resources>
    <testResources>
      <testResource>
        <directory>src/test/resources</directory>
      </testResource>
    </testResources>
    <pluginManagement>
       <plugins>
         <plugin>
           <artifactId>maven-antrun-plugin</artifactId>
           <version>1.1</version>
         </plugin>      
         <plugin>
           <artifactId>maven-assembly-plugin</artifactId>
           <version>2.2-beta-2</version>
         </plugin>
         <plugin>
           <artifactId>maven-clean-plugin</artifactId>
           <version>2.2</version>
         </plugin>
         <plugin>
           <artifactId>maven-compiler-plugin</artifactId>
           <version>2.0.2</version>
         </plugin>
         <plugin>
           <artifactId>maven-dependency-plugin</artifactId>
           <version>2.0</version>
         </plugin>
         <plugin>
           <artifactId>maven-deploy-plugin</artifactId>
           <version>2.3</version>
         </plugin>
         <plugin>
           <artifactId>maven-ear-plugin</artifactId>
           <version>2.3.1</version>
         </plugin>
         <plugin>
           <artifactId>maven-ejb-plugin</artifactId>
           <version>2.1</version>
         </plugin>
         <plugin>
           <artifactId>maven-install-plugin</artifactId>
           <version>2.2</version>
         </plugin>
         <plugin>
           <artifactId>maven-jar-plugin</artifactId>
           <version>2.2</version>
         </plugin>
         <plugin>
           <artifactId>maven-javadoc-plugin</artifactId>
           <version>2.4</version>
         </plugin>
         <plugin>
           <artifactId>maven-plugin-plugin</artifactId>
           <version>2.4.1</version>
         </plugin>
         <plugin>
           <artifactId>maven-rar-plugin</artifactId>
           <version>2.2</version>
         </plugin>
         <plugin>               
           <artifactId>maven-release-plugin</artifactId>
           <version>2.0-beta-7</version>
         </plugin>
         <plugin>               
           <artifactId>maven-resources-plugin</artifactId>
           <version>2.2</version>
         </plugin>
         <plugin>
           <artifactId>maven-site-plugin</artifactId>
           <version>2.0-beta-6</version>
         </plugin>
         <plugin>
           <artifactId>maven-source-plugin</artifactId>
           <version>2.0.4</version>
         </plugin>         
         <plugin>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.4.2</version>
         </plugin>
         <plugin>
           <artifactId>maven-war-plugin</artifactId>
           <version>2.1-alpha-1</version>
         </plugin>
       </plugins>
     </pluginManagement>
  </build>
  <reporting>
    <outputDirectory>target/site</outputDirectory>
  </reporting>
  <profiles>
    <profile>
      <id>release-profile</id>
      <activation>
        <property>
          <name>performRelease</name>
          <value>true</value>
        </property>
      </activation>
      <build>
        <plugins>
          <plugin>
            <inherited>true</inherited>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-source-plugin</artifactId>
            <executions>
              <execution>
                <id>attach-sources</id>
                <goals>
                  <goal>jar</goal>
                </goals>
              </execution>
            </executions>
          </plugin>
          <plugin>
            <inherited>true</inherited>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-javadoc-plugin</artifactId>
            <executions>
              <execution>
                <id>attach-javadocs</id>
                <goals>
                  <goal>jar</goal>
                </goals>
              </execution>
            </executions>
          </plugin>
          <plugin>
            <inherited>true</inherited>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-deploy-plugin</artifactId>
            <configuration>
              <updateReleaseInfo>true</updateReleaseInfo>
            </configuration>
          </plugin>
        </plugins>
      </build>
    </profile>
  </profiles>
</project>

能夠經過下面命令查看當前pom.xml受到超pom.xml文件的影響:

mvn help:effective-pom

聚合關係:用於將多個maven項目聚合爲一個大的項目。

<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>org.codehaus.mojo</groupId>
  <artifactId>my-parent</artifactId>
  <version>2.0</version>
  <modules>
    <module>my-project<module>
  </modules>
</project>

屬性

maven的屬性,是值的佔位符,相似EL,相似ant的屬性,好比${X},可用於pom文件任何賦值的位置。有如下分類:

  1. env.X:操做系統環境變量,好比${env.PATH}

  2. project.x:pom文件中的屬性,好比:<project><version>1.0</version></project>,引用方式:${project.version}

  3. settings.x:settings.xml文件中的屬性,好比:<settings><offline>false</offline></settings>,引用方式:${settings.offline}

  4. Java System Properties:java.lang.System.getProperties()中的屬性,好比java.home,引用方式:${java.home}

  5. 自定義:在pom文件中能夠:<properties><installDir>c:/apps/cargo-installs</installDir></properties>,引用方式:${installDir}

構建設置

構建有兩種build標籤:

<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">
  …
  <!– "Project Build" contains more elements than just the BaseBuild set –>
  <build>…</build>
  <profiles>
    <profile>
      <!– "Profile Build" contains a subset of "Project Build"s elements –>
      <build>…</build>
    </profile>
  </profiles>
</project>

build中的主要標籤:Resources和Plugins。

Resources:用於排除或包含某些資源文件

    <resources>
      <resource>
        <targetPath>META-INF/plexus</targetPath>
        <filtering>false</filtering>
        <directory>${basedir}/src/main/plexus</directory>
        <includes>
          <include>configuration.xml</include>
        </includes>
        <excludes>
          <exclude>**/*.properties</exclude>
        </excludes>
      </resource>
    </resources>

Plugins:設置構建的插件

  <build>    …    <plugins>      <plugin>        <groupId>org.apache.maven.plugins</groupId>        <artifactId>maven-jar-plugin</artifactId>        <version>2.0</version>        <extensions>false</extensions>        <inherited>true</inherited>        <configuration>          <classifier>test</classifier>        </configuration>        <dependencies>…</dependencies>        <executions>…</executions>      </plugin>

相關文章
相關標籤/搜索