生活随笔
收集整理的這篇文章主要介紹了
maven 配置篇
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
什么是pom?
??? pom作為項目對象模型。通過xml表示maven項目,使用pom.xml來實現。主要描述了項目:包括配置文件;開發者需要遵循的規則,缺陷管理系統,組織和licenses,項目的url,項目的依賴性,以及其他所有的項目相關因素。
快速察看:
xml 代碼
<project>????<modelVersion>4.0.0<!---->modelVersion>??????????<groupId>...<!---->groupId>????<artifactId>...<!---->artifactId>????<version>...<!---->version>????<packaging>...<!---->packaging>????<dependencies>...<!---->dependencies>????<parent>...<!---->parent>????<dependencyManagement>...<!---->dependencyManagement>????<modules>...<!---->modules>????<properties>...<!---->properties>??????????<build>...<!---->build>????<reporting>...<!---->reporting>??????????<name>...<!---->name>????<description>...<!---->description>????<url>...<!---->url>????<inceptionYear>...<!---->inceptionYear>????<licenses>...<!---->licenses>????<organization>...<!---->organization>????<developers>...<!---->developers>????<contributors>...<!---->contributors>??????????<issueManagement>...<!---->issueManagement>????<ciManagement>...<!---->ciManagement>????<mailingLists>...<!---->mailingLists>????<scm>...<!---->scm>????<prerequisites>...<!---->prerequisites>????<repositories>...<!---->repositories>????<pluginRepositories>...<!---->pluginRepositories>????<distributionManagement>...<!---->distributionManagement>????<profiles>...<!---->profiles>??<!---->project>??
基本內容:
??? POM包括了所有的項目信息。
maven 相關:
pom定義了最小的maven2元素,允許groupId,artifactId,version。所有需要的元素
- groupId:項目或者組織的唯一標志,并且配置時生成的路徑也是由此生成,如org.codehaus.mojo生成的相對路徑為:/org/codehaus/mojo
- artifactId: 項目的通用名稱
- version:項目的版本
- packaging: 打包的機制,如pom, jar, maven-plugin, ejb, war, ear, rar, par
- classifier: 分類
POM關系:
主要為依賴,繼承,合成
? 依賴關系:
?
xml 代碼
<dependencies>??????<dependency>????????<groupId>junit<!---->groupId>????????<artifactId>junit<!---->artifactId>????????<version>4.0<!---->version>????????<type>jar<!---->type>????????<scope>test<!---->scope>????????<optional>true<!---->optional>??????<!---->dependency>??????...????<!---->dependencies>?? - groupId, artifactId, version:描述了依賴的項目唯一標志
可以通過以下方式進行安裝: - 使用以下的命令安裝:
- mvn install:install-file –Dfile=non-maven-proj.jar –DgroupId=some.group –DartifactId=non-maven-proj –Dversion=1
- 創建自己的庫,并配置,使用deploy:deploy-file
- 設置此依賴范圍為system,定義一個系統路徑。不提倡。
- type:相應的依賴產品包形式,如jar,war
- scope:用于限制相應的依賴范圍,包括以下的幾種變量:
- compile :默認范圍,用于編譯
- provided:類似于編譯,但支持你期待jdk或者容器提供,類似于classpath
- runtime:在執行時,需要使用
- test:用于test任務時使用
- system:需要外在提供相應得元素。通過systemPath來取得
- systemPath: 僅用于范圍為system。提供相應的路徑
- optional: 標注可選,當項目自身也是依賴時。用于連續依賴時使用
?? 獨占性?? ?
?? 外在告訴maven你只包括指定的項目,不包括相關的依賴。此因素主要用于解決版本沖突問題
?
xml 代碼
<dependencies>??????<dependency>????????<groupId>org.apache.maven<!---->groupId>????????<artifactId>maven-embedder<!---->artifactId>????????<version>2.0<!---->version>????????<exclusions>??????????<exclusion>????????????<groupId>org.apache.maven<!---->groupId>????????????<artifactId>maven-core<!---->artifactId>??????????<!---->exclusion>????????<!---->exclusions>??????<!---->dependency>??
表示項目maven-embedder需要項目maven-core,但我們不想引用maven-core
繼承關系
??? 另一個強大的變化,maven帶來的是項目繼承。主要的設置:
定義父項目
xml 代碼
<project>????<modelVersion>4.0.0<!---->modelVersion>????<groupId>org.codehaus.mojo<!---->groupId>????<artifactId>my-parent<!---->artifactId>????<version>2.0<!---->version>????<packaging>pom<!---->packaging>??<!---->project>??
??? packaging 類型,需要pom用于parent和合成多個項目。我們需要增加相應的值給父pom,用于子項目繼承。主要的元素如下:
- 依賴型
- 開發者和合作者
- 插件列表
- 報表列表
- 插件執行使用相應的匹配ids
- 插件配置
- 子項目配置
xml 代碼
<project>????<modelVersion>4.0.0<!---->modelVersion>????<parent>??????<groupId>org.codehaus.mojo<!---->groupId>??????<artifactId>my-parent<!---->artifactId>??????<version>2.0<!---->version>??????<relativePath>../my-parent<!---->relativePath>????<!---->parent>????<artifactId>my-project<!---->artifactId>??<!---->project>??
relativePath可以不需要,但是用于指明parent的目錄,用于快速查詢。
dependencyManagement:
用于父項目配置共同的依賴關系,主要配置依賴包相同因素,如版本,scope。
合成(或者多個模塊)
??? 一個項目有多個模塊,也叫做多重模塊,或者合成項目。
如下的定義:
xml 代碼
<project>????<modelVersion>4.0.0<!---->modelVersion>????<groupId>org.codehaus.mojo<!---->groupId>????<artifactId>my-parent<!---->artifactId>????<version>2.0<!---->version>????<modules>??????<module>my-project1<module>??????<module>my-project2<module>????<!---->modules>??<!---->project>??
build 設置
??? 主要用于編譯設置,包括兩個主要的元素,build和report
? build
??? 主要分為兩部分,基本元素和擴展元素集合
注意:包括項目build和profile build
xml 代碼
<project>????????<build>...<!---->build>????<profiles>??????<profile>????????????????<build>...<!---->build>??????<!---->profile>????<!---->profiles>??<!---->project>??
基本元素
xml 代碼
<build>????<defaultGoal>install<!---->defaultGoal>????<directory>${basedir}/target<!---->directory>????<finalName>${artifactId}-${version}<!---->finalName>????<filters>??????<filter>filters/filter1.properties<!---->filter>????<!---->filters>????...??<!---->build>?? - defaultGoal: 定義默認的目標或者階段。如install
- directory: 編譯輸出的目錄
- finalName: 生成最后的文件的樣式
- filter: 定義過濾,用于替換相應的屬性文件,使用maven定義的屬性。設置所有placehold的值
資源(resources)
??? 你項目中需要指定的資源。如spring配置文件,log4j.properties
xml 代碼
<project>????<build>??????...??????<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>??????<testResources>????????...??????<!---->testResources>??????...????<!---->build>??<!---->project>??
- resources: resource的列表,用于包括所有的資源
- targetPath: 指定目標路徑,用于放置資源,用于build
- filtering: 是否替換資源中的屬性placehold
- directory: 資源所在的位置
- includes: 樣式,包括那些資源
- excludes: 排除的資源
- testResources: 測試資源列表
插件
? 在build時,執行的插件,比較有用的部分,如使用jdk 5.0編譯等等
xml 代碼
<project>????<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>??????<!---->plugins>????<!---->build>??<!---->project>?? - extensions: true or false,是否裝載插件擴展。默認false
- inherited: true or false,是否此插件配置將會應用于poms,那些繼承于此的項目
- configuration: 指定插件配置
- dependencies: 插件需要依賴的包
- executions: 用于配置execution目標,一個插件可以有多個目標。
如下:
???
xml 代碼
<plugin>??????????<artifactId>maven-antrun-plugin<!---->artifactId>????????????<executions>????????????<execution>??????????????<id>echodir<!---->id>??????????????<goals>????????????????<goal>run<!---->goal>??????????????<!---->goals>??????????????<phase>verify<!---->phase>??????????????<inherited>false<!---->inherited>??????????????<configuration>????????????????<tasks>??????????????????<echo>Build?Dir:?${project.build.directory}<!---->echo>????????????????<!---->tasks>??????????????<!---->configuration>????????????<!---->execution>??????????<!---->executions>????????<!---->plugin>??
? 說明:
- id:規定execution 的唯一標志
- goals: 表示目標
- phase: 表示階段,目標將會在什么階段執行
- inherited: 和上面的元素一樣,設置false maven將會拒絕執行繼承給子插件
- configuration: 表示此執行的配置屬性
插件管理
??? pluginManagement:插件管理以同樣的方式包括插件元素,用于在特定的項目中配置。所有繼承于此項目的子項目都能使用。主要定義插件的共同元素
擴展元素集合
主要包括以下的元素:
Directories
用于設置各種目錄結構,如下:
?
xml 代碼
<build>??????<sourceDirectory>${basedir}/src/main/java<!---->sourceDirectory>??????<scriptSourceDirectory>${basedir}/src/main/scripts<!---->scriptSourceDirectory>??????<testSourceDirectory>${basedir}/src/test/java<!---->testSourceDirectory>??????<outputDirectory>${basedir}/target/classes<!---->outputDirectory>??????<testOutputDirectory>${basedir}/target/test-classes<!---->testOutputDirectory>??????...????<!---->build>??
Extensions
表示需要擴展的插件,必須包括進相應的build路徑。
xml 代碼
<project>????<build>??????...??????<extensions>????????<extension>??????????<groupId>org.apache.maven.wagon<!---->groupId>??????????<artifactId>wagon-ftp<!---->artifactId>??????????<version>1.0-alpha-3<!---->version>????????<!---->extension>??????<!---->extensions>??????...????<!---->build>??<!---->project>??
Reporting
??? 用于在site階段輸出報表。特定的maven 插件能輸出相應的定制和配置報表。
?
xml 代碼
<reporting>??????<plugins>????????<plugin>??????????<outputDirectory>${basedir}/target/site<!---->outputDirectory>??????????<artifactId>maven-project-info-reports-plugin<!---->artifactId>??????????<reportSets>????????????<reportSet><!---->reportSet>??????????<!---->reportSets>????????<!---->plugin>??????<!---->plugins>????<!---->reporting>??
Report Sets
??? 用于配置不同的目標,應用于不同的報表
xml 代碼
<reporting>??????<plugins>????????<plugin>??????????...??????????<reportSets>????????????<reportSet>??????????????<id>sunlink<!---->id>??????????????<reports>????????????????<report>javadoc<!---->report>??????????????<!---->reports>??????????????<inherited>true<!---->inherited>??????????????<configuration>????????????????<links>??????????????????<link>http://java.sun.com/j2se/1.5.0/docs/api/<!---->link>????????????????<!---->links>??????????????<!---->configuration>????????????<!---->reportSet>??????????<!---->reportSets>????????<!---->plugin>??????<!---->plugins>????<!---->reporting>?
轉載于:https://www.cnblogs.com/sayou/archive/2013/01/17/2864099.html
總結
以上是生活随笔為你收集整理的maven 配置篇的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。