eclipse创建maven项目首先需要添加自己本地的maven版本
更改eclipse中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/xsd/maven-4.0.0.xsd">
<!-- 版本号 -->
<modelVersion>4.0.0</modelVersion>
<!-- 坐标信息 -->
<groupId> 一般用公司的网址+项目名称 </groupId>
<artifactId>项目名+模块名</artifactId>
<!--
第一个表示大版本号
第二个表示分支版本号
第三表示小版本号
snapshot快照
alpha内测版本
beta公测版本
release稳定版本
GA正式发布
-->
<version>0.0.1-SNAPSHOT</version>
<!-- 默认是jar,还有zip,war,pom -->
<packaging>jar</packaging>
<!-- 项目描述名 -->
<name>hi</name>
<!-- 项目地址 -->
<url>http://maven.apache.org</url>
<!-- 项目描述 -->
<description></description>
<!-- 开发人员列表 -->
<developers></developers>
<!-- 许可证信息 -->
<licenses></licenses>
<!-- 组织信息 -->
<organization></organization>
<!-- 依赖列表 -->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- 依赖项 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<!-- 依赖范围 :compile默认的在编译,测试,运行有效。provided编译测试时有效。
runtime测试运行时有效。test,只在测试时有用。system和provided一样。但system的
可移植性差,和本机系统相关联。import导入的范围,他只适用在dependencyManagement中
表示从其他pom文件中导入dependency的配置-->
<scope>test</scope>
<!-- 设置依赖是否可选,默认为false -->
<optional></optional>
<!-- 排除依赖传递列表 -->
<exclusions>
<exclusion></exclusion>
</exclusions>
</dependency>
</dependencies>
<!-- 依赖的管理 -->
<dependencyManagement>
<dependencies>
<dependency></dependency>
</dependencies>
</dependencyManagement>
<build>
<!-- 插件列表 -->
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<!-- 子模块对父模块的pom继承 -->
<parent></parent>
<!-- 聚合多个war -->
<modules></modules>
</project>
maven创建web项目
<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>com.test</groupId>
<artifactId>dfdf</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>dfdf Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<!-- 只在编译和测试时运行 -->
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<finalName>dfdf</finalName>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<!-- 在打包阶段运行tomcat -->
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
<version>2.2</version>
</plugin>
</plugins>
</build>
</project>
maven 的命令
clean 清空。
package 打包。
install 安装到本地仓库中。
compile 编译。
1依赖于2,2依赖于3,3相当于传递依赖于1。
1的pom.xml
<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>
<groupId>com.hongxing</groupId>
<artifactId>hongxing-bge</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>hongxing-bge</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
2的pom.xml
<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>
<groupId>com.hongxing</groupId>
<artifactId>hongxing-nange</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>hongxing-nange</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<!-- 引入依赖 -->
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- 引入1的依赖 ,拿到1的坐标-->
<dependency>
<groupId>com.hongxing</groupId>
<artifactId>hongxing-bge</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
3的pom.xml
<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>
<groupId>com.hongxing</groupId>
<artifactId>hongxing-shanji</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>hongxing-shanji</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- 引入2的依赖,2的坐标 -->
<dependency>
<groupId>com.hongxing</groupId>
<artifactId>hongxing-nange</artifactId>
<version>0.0.1-SNAPSHOT</version>
<!-- 排除依赖 ,排除3的依赖-->
<exclusions>
<exclusion>
<groupId>com.hongxing</groupId>
<artifactId>hongxing-bge</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</project>
创建maven项目,更改jdk版本
<profile>
<id>jdk-1.8</id>
<activation>
<activeByDefault>true</activeByDefault>
<jdk>1.8</jdk>
</activation>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
</properties>
</profile>
如果不行,再对eclipse进行设置。window->preferences->maven->user settings,user settings那里选择maven安装目录下的settings.xml文件。
依赖冲突:两个原则:短路优先,优先解析路径短的版本(a依赖b b依赖c a又依赖c那么a直接依赖c不会通过b再去依赖c)
不同pom文件路径一样长,先声明先优先(a依赖b,a依赖c,如果在a的pom文件中先写的依赖b,那么依赖b)
同一pom文件后声明的覆盖先声明的(a依赖b,但是b的pom文件有两个关于junit的版本,那么a会依赖后声明的版本)
maven的聚合和继承
以上面三个pom文件为例,只需要把上面三个pom文件install到本地仓库,新创建一个maven文件用来聚合三个maven项目,新建的pom文件如下
两点:1.pom文件坐标下的package标签用pom<package>pom</package>
2.在project标签下加<modules>
<module></module>
<module></module>
</modules>
<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>
<groupId>com.hongxing</groupId>
<artifactId>hongxing-aggreation</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<name>hongxing-aggreation</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<modules>
<module>../hongxing-bge</module>
<module>../hongxing-nange</module>
<module>../hongxing-shanji</module>
</modules>
</project>
继承:解决依赖传递的弊端(1.a依赖b,b依赖c,如果a想要依赖c,那么b pom文件的<scope>标签必须是compile</scope>(默认不填也是compile))
1.父pom文件的打包方式必须为pom即<packaging>pom</packaging>
2.标签<dependencies>外面必须嵌套一个<dependencyManagement>标签
父pom文件
<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>
<groupId>com.hongxing</groupId>
<artifactId>hongxing-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<name>hongxing-parent</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!-- 在此处声明版本号 -->
<junit.version>3.8.1</junit.version>
</properties>
<!-- 继承用dependencyManagement标签管理 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<!-- 用el表达式获取版本 -->
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
子pom文件
1.引入父pom文件的坐标必须用<parent>标签,<relativePath>相对路径</relativePath>(当前pom的相对路径使用..跳出当前项目)通过<groupId>和<artifactId>两个标签声明继承父类的那个jar包
<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>
<groupId>com.hongxing</groupId>
<artifactId>hongxing-bge</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>hongxing-bge</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<!-- 从父pom文件引入junit的jar,引入父pom文件的坐标 -->
<parent>
<groupId>com.hongxing</groupId>
<artifactId>hongxing-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
</dependencies>
</project>
<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>
<groupId>org.baosight</groupId>
<artifactId>maven</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<finalName>maven</finalName>
<plugins>
<!-- web项目没有web.xml报错的配置 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
<!-- cargo 实现自动化部署-->
<plugin>
<groupId>org.codehaus.cargo</groupId>
<artifactId>cargo-maven2-plugin</artifactId>
<version>1.4.9</version>
<configuration>
<container>
<containerId>tomcat7x</containerId>
<home>E:\tool\apache-tomcat-7.0.76</home>
<type>installed</type>
<!-- type 可选值有 installed、remote
默认值为installed,可以省略
-->
</container>
<configuration>
<type>existing</type>
<home>E:\tool\apache-tomcat-7.0.76</home>
<properties>
<!-- 更改端口号,可以省去此配置:-->
<cargo.servlet.port>8080</cargo.servlet.port>
</properties>
</configuration>
</configuration>
<executions>
<execution>
<id>cargo-run</id>
<!-- 当安装到本地仓库时运行 -->
<phase>install</phase>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<!-- 配置jdk版本 -->
<profiles>
<profile>
<id>jdk-1.8</id>
<activation>
<activeByDefault>true</activeByDefault>
<jdk>1.8</jdk>
</activation>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
</properties>
</profile>
</profiles>
</project>