maven是基于项目对象模型,通过一小段描述信息来管理项目的构建、报告和文档的项目软件管理工具
注意,mvn的目录结构有着固定的模式:
- src
- main
-java
- 各种自定义目录package
- test
- java
- 各种自定义目录package
- resources资源目录
- 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/maven-v4_0_0.xsd">
<modelVersion>4.0.0modelVersion>
<groupId>cn.seu.edu.maven01groupId>
<artifactId>maven01-modelartifactId>
<version>0.0.1-SNAPSHOTversion>
<dependencies>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.10version>
dependency>
dependencies>
project>
注意pom文件应该与src属于同一级目录
注意,mvn命令后面不能带有“;”
1. mvn compile,表示编译项目
2. mvn test,运行test
3. mvn package,为项目生成jar
4. mvn clean,删除target文件
5. mvn install,安装本地jar到目录
6. mvn archetype:generate,自动创建符合maven要求的目录结构,也可以使用mvn archetype:generate -DgroupId=xxx(这里一般就是组织名,例如公司网址反写+项目名) -DartifactId=sss(这里一般是项目在公司的唯一标识,例如项目名-模块名) -Dversion=11111 指定版本号 -Dpackage=dddddd代码所在的包
maven中任何依赖、插件、项目输出等够成为构件,构件通过坐标唯一标识;
镜像仓库,镜像仓库的修改在maven的conf目录下setting.xml中的mirros节点
<mirror>
<id>mirrorIdid>
《mirror of标识为那个仓库添加镜像,这里也可以使用 * 代表所有仓库,一旦使用了镜像仓库,所有对原仓库的访问都将转为对镜像仓库的访问》
<mirrorOf>repositoryIdmirrorOf>
<name>Human Readable Name for this Mirror.name>
<url>http://my.repository.com/repo/pathurl>
mirror>
mirrors>
默认情况下,maven的仓库在C:\Users\Administrator.m2\repostory,一般不将仓库放在C盘,若需要修改位置,修改maven的conf中的settings.xml中
<localRepository>D:/SoftWare/ThirdPart4Java/Maven350/ResponsitorylocalRepository>
为了方便,这里最好将settings.xml文件也复制到该仓库中
-vm
D:\SoftWare\javaJDK\JDK8u91\bin\javaw.exe
这里如果不更改,使用package命令时会出现
No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
一个完成的项目周期包括:清理clean、编译compile、测试test、打包package、集成测试、验证test、部署deploy
maven的相关插件在这里http://maven.apache.org/plugins/index.html,以source插件为例,为项目生成源码包
1. 在pom文件中添加下述语句
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-source-pluginartifactId>
<version>2.4version>
<executions>
<execution>
<phase>packagephase>
<goals>
<goal>jar-no-forkgoal>
goals>
execution>
executions>
plugin>
plugins>
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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<groupId>公司网址反写+项目名groupId>
<artifactId>项目名-模块名artifactId>
<version>0.0.1-SNAPSHOTversion>
<packaging>jarpackaging>
<name>hiname>
<url>http://maven.apache.orgurl>
<description>description>
<developers>developers>
<license>license>
<organization>organization>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
properties>
<dependencies>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>3.8.1version>
<scope>testscope>
<optional>true/falseoptional>
<exclusions>exclusions>
dependency>
dependencies>
<dependencyManagement>dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-source-pluginartifactId>
<version>2.4version>
<executions>
<execution>
<phase>packagephase>
<goals>
<goal>jar-no-forkgoal>
goals>
execution>
executions>
plugin>
plugins>
build>
<parent>parent>
<modules>modules>
project>
在settings.xml的profiles节点加入一个profile
<profile>
<id>jdk-1.7id>
<activation>
<activeByDefaule>trueactiveByDefaule>
<jdk>1.7jdk>
activation>
<properties>
<maven.compiler.source>1.7maven.compiler.source>
<maven.compiler.target>1.7maven.compiler.target>
<maven.compiler.compilerVersion>1.7maven.compiler.compilerVersion>
properties>
profile>
例如A/B分别依赖两个不同版本的构建,这里就会存在冲突,maven采用如下的原则
1. 短路优先,
A -> B -> C -> X(jar);
A -> D -> X(jar);
这里,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.0modelVersion>
<groupId>com.hongxinggroupId>
<artifactId>hongxing.aggreationartifactId>
<version>0.0.1-SNAPSHOTversion>
<packaging>pompackaging>
<name>hongxing.aggreationname>
<url>http://maven.apache.orgurl>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
properties>
<modules>
<module>../hongxing-begemodule>
<module>../hongxing-nangemodule>
<module>../hongxing-shanjimodule>
modules>
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.0modelVersion>
<groupId>com.hongxinggroupId>
<artifactId>hongxing-parentartifactId>
<version>0.0.1-SNAPSHOTversion>
<packaging>pompackaging>
<name>hongxing-parentname>
<url>http://maven.apache.orgurl>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<junit.version>3.8.1junit.version>
properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>${junit.version}version>
<scope>testscope>
dependency>
dependencies>
dependencyManagement>
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/maven-v4_0_0.xsd">
<modelVersion>4.0.0modelVersion>
<groupId>cm.seu.edu.webgroupId>
<artifactId>web-demoartifactId>
<packaging>warpackaging>
<version>0.0.1-SNAPSHOTversion>
<name>web-demo Maven Webappname>
<url>http://maven.apache.orgurl>
<dependencies>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>3.8.1version>
<scope>testscope>
dependency>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>javax.servlet-apiartifactId>
<version>3.0.1version>
<scope>providedscope>
dependency>
dependencies>
<build>
<finalName>web-demofinalName>
<plugins>
<plugin>
<groupId>org.apache.tomcat.mavengroupId>
<artifactId>tomcat7-maven-pluginartifactId>
<version>2.2version>
<executions>
<execution>
<phase>packagephase>
<goals>
<goal>rungoal>
goals>
execution>
executions>
plugin>
plugins>
build>
project>