基本信息:
<modelVersion>4.0.0</modelVersion> :Maven3固定不变
<groupId>com.netqin.framework</groupId> :定义项目隶属于哪个组,是一个实际的项目
<artifactId>frame-core</artifactId> :项目模块名称
<packaging>jar</packaging> :打包方式,默认jar,可以配置war,pom(聚合项目)
<version>0.0.1-SNAPSHOT</version>:项目版本
<name>netqin foamework core</name> :项目一个比较易读的名称
<url>http://maven.apache.org/frame-core</url> :项目访问地址
聚合项目:
<modelVersion>4.0.0</modelVersion>
<groupId>com.netqin.framework</groupId>
<artifactId>frame-framework</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<name>netqin foamework</name>
<url>http://maven.apache.org</url>
<modules>
<module>frame-core</module>
<module>frame-service</module>
</modules>
子模块:
<modelVersion>4.0.0</modelVersion> :无论聚合项目还是子项目,都必须包含该配置
<parent>
<artifactId>frame-framework</artifactId>
<groupId>com.netqin.framework</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>frame-core</artifactId>:其余属性默认与父模块相同
<packaging>jar</packaging>
<name>netqin foamework core</name>
<build>
<finalName>frame-core</finalName>:生成的包的名称
</build>
聚合和子模块都可以配置如下内容:
1.属性
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
2.依赖
<dependencies>
<dependency>
<groupId>spring</groupId>
<artifactId>spring-core</artifactId>
<version>1.0.2</version>:以上三项为必填项,version在子模块继承时可以不用声明
<type>jar</type>:依赖类型,默认为jar,对应于项目的packaging,一般不用声明
<scope>compile</scope>:依赖范围,默认compile,编译、测试、运行都有效,比如spring-core
<optional>true</optional>:可选依赖,既只对当前项目有效,不会传递给子模块
<exclusions>:排除依赖,既排除掉当前依赖下对应依赖包,既项目编译时不会导入该依赖包,可以在下面声明一个高级版本进行替换
<exclusion>
<groupId>com.test.demo</groupId>
<artifactId>demo-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.test.demo</groupId>
<artifactId>demo-core</artifactId>
<version>1.0.2</version>:换个高级版本
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<type>jar</type>
<scope>test</scope>:只对测试范围有效,比如Junit。
另外还可配置provided:编译和测试有效,运行无效,比如servlet-api。
runtime:测试和运行时有效。
import:导入其它pom中的<dependencyManagement>,该元素只在<dependencyManagement>中可以使用,如下内容介绍。
</dependency>
</dependencies>
dependencies:会被子模块继承,如果不希望子模块继承,可以使用如下配置:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${juint.version}</version>
<type>jar</type>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${springframework.version}</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.test</groupId>
<artifactId>test</artifactId>
<version>1.0.0version>
<type>pom</type>:只能是pom
<scope>import</scope>:将该坐标对应的聚合项目中的<dependencyManagement>合并到当前的<dependencyManagement>中来
</dependency>
</dependencies>
</dependencyManagement>
<dependencyManagement>:被该元素包裹起来的依赖不会被子模块继承,
如果子模块中要使用,需要在dependencies中声明,但这时可以之生命groupId和artifactId,比如:
3.插件
<build>
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>2.4.1</version>
<executions>
<execution>
<id>default-clean</id>
<phase>clean</phase>:绑定clean生命周期
<goals>
<goal>clean</goal>:绑定clean生命周期的clean目标
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
上述配置,子模块也会继承,类似<dependencyManagement>,如果不希望子模块继承,可以如下配置:
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.1.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
子模块只要如下配置即可:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
</plugin>
</plugins>
</build>
插件需要绑定到某个生命周期才可以使用
依赖项目命令:
1.查看当前项目的已解析依赖,类似于eclipse的pom查看器中的Dependency Hierarchy的右侧视图
mvn dependency:list
2.查看当前项目的已解析依赖,类似于eclipse的pom查看器中的Dependency Hierarchy的左侧视图
mvn dependency:tree
3.查看项目使用了但是依赖没有声明,或者是声明了没有使用
mvn dependency:analyze
比如,没有使用到spring-core,但是项目中声明了
[WARNING] Unused declared dependencies found:
[WARNING] org.springframework:spring-core:jar:3.0.6.RELEASE:compile
《Maven实战》学习笔记(2)--POM简单设置
猜你喜欢
转载自hanqunfeng.iteye.com/blog/1300091
今日推荐
周排行