Maven坐标
GroupId、artifactId和version构成了Maven的坐标(groupId和version可以从parent继承),指定了组件在Maven仓库中的位置。Maven中的每个组件都有一个坐标,通过这个坐标我们在自己的项目中可以设置对该组件的依赖。
------groupId:项目属于哪个组,往往和项目所在的组织或公司存在关联;
------artifactId:当前Maven项目在组中唯一的ID;
------version:定义当前项目的版本,如:1.0(-SNAPSHOT),SNAPSHOT表示快照,说明该项目还处于开发阶段,是不稳定版本;
------packaging:当我们有了groupId:artifactId:version作为地址后,还需要packaging为我们提供组件的类型,例如:<packaging>war</packaging>标识组件为一个war。如果packaging不指定,默认值为jar,当前可选值为:pom, jar, maven-plugin, ejb, war, ear, rar, par;
------classifier:可选,坐标也可以展示为groupId:artifactId:packaging:classifier:version。
POM关联
Maven的一个有力的地方就在于对项目关联的处理;包括依赖、继承和聚合(多模块项目)。
Dependencies
<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:依赖组件的坐标,如果当Maven通过这些坐标无法从中心仓库获取该组件时,可以通过下面的方法处理:
1、用安装插件安装本地依赖,在命令行中输入:
mvn install:install-file -Dfile=non-maven-proj.jar -DgroupId=some.group -DartifactId=non-maven-proj -Dversion=1 -Dpackaging=jar
2、创建你自己的仓库,并部署它
3、设置依赖scope到system,并定义一个systemPath,但这个不推荐。
------type:对应所以来的组件的packaging;
------scop:用于控制依赖的范围,有以下几种范围供选择
1、compile:编译依赖范围,默认,对于所有的classpath都是有效的;
2、provided:仅对编译和测试classpath有效;
3、runtime:编译时不需要,尽在运行时需要;
4、test:仅用于测试;
5、system:和provided类似,只是你需要提供JAR,组件不再在仓库中查找。
------systemPath:当scop配置为system时就需要它了
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.0</version>
<type>jar</type>
<scope>system</scope>
<systemPath>$(object.path)/lib/junit.jar</systemPath>
<optional>true</optional>
</dependency>
------optional:设置为true,标识该依赖只对该项目有效,如果其他项目依赖该项目,该依赖将不会传递。
Exclusions
该配置告诉Maven你不想包含的该依赖的依赖(即,依赖传递的依赖)。
<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>
...
</dependencies>
也可以使用通配符,表示排除所有传递的依赖。
<dependencies>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-embedder</artifactId>
<version>2.0</version>
<exclusions>
<exclusion>
<groupId>*</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
...
</dependencies>
Inheritance
继承是一个有力的工具,在maven中使用继承时,需要为parent和aggregation(多模块)项目设置packaging为pom:<packaging>pom</packaging>,然后就子项目就可以继承该POM了。
<parent>
<groupId>org.codehaus.mojo</groupId>
<artifactId>my-parent</artifactId>
<version>2.0</version>
<relativePath>../my-parent</relativePath>
</parent>
relativePath是可选的,指定parent的搜索路径,如果配置了,Maven将首先搜索relativePath,然后本地仓库,最后远程仓库查找parentPOM。
POM就像Java对象最终都继承自java.lang.Object一样,所有的POM都继承自一个Super POM,你通过查ungjianyige最小化的pom.xml并在命令行中执行 mvn help:effective-pom来查看Super POM对你的POM的影响,下面是Maven 3.0.4的Super POM,该Super POM的位置在apache-maven-version\lib\maven-model-builder-version.jar\org\apache\maven\model\下面,文件名pom-4.0.0.xml。
<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>
Dependency Management
dependencyManagement用于在parent项目定义一个依赖,如:
<dependencyManagement>
<dependencies >
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.0</version>
<type>jar</type>
<scope>compile</scope>
<optional>true</optional>
</dependency>
</dependencies >
</dependencyManagement>
然后从这个parent继承的POMs就能设置他们的依赖为:
<dependencies >
<dependency >
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency >
</dependencies >
继承的POM的dependency的其它信息可以从parent的dependencyManagement中获得,这样的好处在于可以将依赖的细节放在一个控制中心,子项目就不用在关心依赖的细节,只需要设置依赖。
Aggregation(或者Multi-Module)
在多模块的项目中,可以将一个模块的集合配置到modules中。
<modules>
<module>my-project</module>
<module>another-project</module>
</modules>
在这里列出的模块不需要考虑顺序,Maven将自己根据依赖关系排序。
Properties
Maven中的Properties就像Ant中的,可以在POM的任何地方通过${X}来返回属性的值,X就表Poperty。
存在以下5种不同的类型:
1、env.X:将返回环境变量的值,如:${env.PATH}返回PATH环境变量的值;
2、project.X:POM中的对应元素的值,'.'表示在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()获取到的属性在POM属性中都是可用的,如:${java.home};
5、X:在POM的<properties />中设置的元素,如: <properties><someVar>value</someVar></properies>通过${someVar}获取。