maven笔记

一.maven所能做的:

1.清理项目 mvn clean

2.编译项目 mvn compile

3.测试项目 mvn test

4.生成测试报告

5.打包项目  mvn package

6.部署项目

 

maven目录结构

 

跳过测试代码的编译:

mvn package -Dmaven.test.skip=true

 

二.仓库布局

1.本地仓库,可以在setting.xml中修改,默认是在用户目录下

    <localRepository>/path/to/local/repo</localRepository>

2.远程仓库

3.中央仓库

     打开jar文件$M2_HOME/lib/maven-model-builder-3.0.jar,里面可以看到pox.xml中的中央仓库配置

4.私服

    私服是一种特殊的远程仓库,它是架设在局域网内的仓库服务,私服代理广域网上的远程仓库,供局域网内的Maven用户使用,当maven需要下载构件的时候,它从私服请求,如果私服上不存在该构件,则从外部的远程仓库下载,缓存在私服上之后,再为maven的请求提供服务。此外,一些无法从外部仓库下载到的构件也可以从本地上传到私服供大家使用

 

三.dependency依赖中scope范围

1.compile  默认编译依赖范围,对于编译,测试,运行三种classpath都有效

2.test 测试依赖范围,只对测试classpath有效,只对测试代码有效

3.provided 已提供依赖范围,对编译,测试classpath都有效,但对于运行无效,因为容器已经提供,如servlet-api,打包的时候这个包不会打进去

4.runtime  运行提供,如:jdbc驱动

compile,runtime打包会打进去,test、provided打包不会打进去

 

四.依赖传递性原则

1.路径最近者优先  c-<b->a,c依赖b,b依赖a,会先用b的jar包

2.路径相同,第一个声明者优先:c->b->a,必需看b,a在c中哪一个先声明

 

五.maven的聚合与继承

聚合:就是把多个模块或项目聚合在一起,可以构建一个专门负责聚合工作的maven project

  建立该project的时候,要注意以下几点:

   1.本身也是作为一个maven项目,它必需有自己的POM

   2.它的打包方式必需为:pom

   3.引入新的元素:modules--module

   4.版本,聚合模块的版本和被聚合模块版本一致

   5.relative path:每个module的值 是一个当前POM的相对目录 

   6.聚合模块减少的内容 ,聚合模块的内容仅是一个pom.xml文件,它不会包含src/main/java,srt/test/java,因为它只是用来帮助其他模块构建的工具,本身并没有实质的内容

   7.子模块省略groupid和version:使用了继承的子模块中可以不声明groupid和version,子模块将隐式地继承父模块的这两个元素

  dependencyManagerment中的插件,是不会被子类继承的

 

  maven install 先打包,并加入到本地仓库

 

  聚合与继承项目使用案例

   

    其中除dn-web 是war项目,dn-parent为pom项目,其他均为jar项目

   1.dn-parent中的pom.xml设置

<?xml version="1.0" encoding="UTF-8"?>

<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.bigfong</groupId>
	<artifactId>dn-parent</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>pom</packaging>

	<name>dn-parent</name>
	<url>http://www.example.com</url>

	<modules>
		<module>../dn-web</module>
		<module>../dn-common</module>
		<module>../dn-dao</module>
		<module>../dn-service</module>
		<module>../dn-util</module>
	</modules>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<spring.version>4.0.3.RELEASE</spring.version>
		<aspectj.version>1.7.1</aspectj.version>
		<jackson.version>2.1.4</jackson.version>
		<org.springframework.version>3.2.2.RELEASE</org.springframework.version>
		<hbase.version>1.2.3</hbase.version>
	</properties>
	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>junit</groupId>
				<artifactId>junit</artifactId>
				<version>4.10</version>
			</dependency>
			<dependency>
				<groupId>redis.clients</groupId>
				<artifactId>jedis</artifactId>
				<version>2.7.3</version>
			</dependency>
			<dependency>
				<groupId>log4j</groupId>
				<artifactId>log4j</artifactId>
				<version>1.2.16</version>
			</dependency>
			<dependency>
				<groupId>org.slf4j</groupId>
				<artifactId>slf4j-log4j12</artifactId>
				<version>1.7.7</version>
			</dependency>
			<dependency>
				<groupId>javax.servlet</groupId>
				<artifactId>servlet-api</artifactId>
				<version>2.5</version>
				<scope>provided</scope>
			</dependency>
			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-context</artifactId>
				<version>${org.springframework.version}</version>
			</dependency>
			<!-- <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> 
				<version>${org.springframework.version}</version> </dependency> -->
			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-beans</artifactId>
				<version>${org.springframework.version}</version>
			</dependency>
			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-webmvc</artifactId>
				<version>${org.springframework.version}</version>
			</dependency>
			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-orm</artifactId>
				<version>${org.springframework.version}</version>
			</dependency>
			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-test</artifactId>
				<version>${org.springframework.version}</version>
			</dependency>
			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-aspects</artifactId>
				<version>${org.springframework.version}</version>
			</dependency>
			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-tx</artifactId>
				<version>${org.springframework.version}</version>
			</dependency>
			<dependency>
				<groupId>com.fasterxml.jackson.core</groupId>
				<artifactId>jackson-core</artifactId>
				<version>2.0.0</version>
			</dependency>
			<dependency>
				<groupId>org.codehaus.jackson</groupId>
				<artifactId>jackson-mapper-asl</artifactId>
				<version>1.8.7</version>
			</dependency>
			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-webmvc</artifactId>
				<version>${spring.version}</version>
			</dependency>
			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-web</artifactId>
				<version>${spring.version}</version>
			</dependency>
			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-context</artifactId>
				<version>${spring.version}</version>
			</dependency>
			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-core</artifactId>
				<version>${spring.version}</version>
			</dependency>
			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-beans</artifactId>
				<version>${spring.version}</version>
			</dependency>
			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-aop</artifactId>
				<version>${spring.version}</version>
			</dependency>
			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-tx</artifactId>
				<version>${spring.version}</version>
			</dependency>
			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-jdbc</artifactId>
				<version>${spring.version}</version>
			</dependency>
			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-test</artifactId>
				<version>${spring.version}</version>
			</dependency>
			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-context-support</artifactId>
				<version>${spring.version}</version>
			</dependency>
			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-test</artifactId>
				<version>${spring.version}</version>
			</dependency>
		</dependencies>
	</dependencyManagement>
</project>

2.dn-web中的pom.xml设置 ,并依赖service

<?xml version="1.0" encoding="UTF-8"?>

<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>

	<artifactId>dn-web</artifactId>
	<packaging>war</packaging>

	<name>dn-web Maven Webapp</name>
	<!-- FIXME change it to the project's website -->
	<url>http://www.example.com</url>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<maven.compiler.source>1.7</maven.compiler.source>
		<maven.compiler.target>1.7</maven.compiler.target>
	</properties>

	<parent>
		<groupId>com.bigfong</groupId>
		<artifactId>dn-parent</artifactId>
		<version>0.0.1-SNAPSHOT</version>
		<relativePath>../dn-parent</relativePath>
	</parent>

	<dependencies>
		<dependency>
			<groupId>com.bigfong</groupId>
			<artifactId>dn-service</artifactId>
			<version>0.0.1-SNAPSHOT</version>
		</dependency>
	</dependencies>

	<build>
		<finalName>dn-web</finalName>
	</build>
</project>

3.dn-service中的pom.xml设置,并依赖dao,util,common

<?xml version="1.0" encoding="UTF-8"?>

<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>

	<artifactId>dn-service</artifactId>
	<packaging>jar</packaging>

	<name>dn-service</name>
	<!-- FIXME change it to the project's website -->
	<url>http://www.example.com</url>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<maven.compiler.source>1.7</maven.compiler.source>
		<maven.compiler.target>1.7</maven.compiler.target>
	</properties>

	<parent>
		<groupId>com.bigfong</groupId>
		<artifactId>dn-parent</artifactId>
		<version>0.0.1-SNAPSHOT</version>
		<relativePath>../dn-parent</relativePath>
	</parent>

	<dependencies>
		<dependency>
			<groupId>com.bigfong</groupId>
			<artifactId>dn-dao</artifactId>
			<version>0.0.1-SNAPSHOT</version>
		</dependency>
		<dependency>
			<groupId>com.bigfong</groupId>
			<artifactId>dn-util</artifactId>
			<version>0.0.1-SNAPSHOT</version>
		</dependency>
		<dependency>
			<groupId>com.bigfong</groupId>
			<artifactId>dn-common</artifactId>
			<version>0.0.1-SNAPSHOT</version>
		</dependency>
	</dependencies>

	<build>
		
	</build>
</project>

4.dn-dao中的pom.xml设置,并依赖util,common

<?xml version="1.0" encoding="UTF-8"?>

<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>

	<artifactId>dn-dao</artifactId>
	<packaging>jar</packaging>

	<name>dn-dao</name>
	<!-- FIXME change it to the project's website -->
	<url>http://www.example.com</url>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<maven.compiler.source>1.7</maven.compiler.source>
		<maven.compiler.target>1.7</maven.compiler.target>
	</properties>

	<parent>
		<groupId>com.bigfong</groupId>
		<artifactId>dn-parent</artifactId>
		<version>0.0.1-SNAPSHOT</version>
		<relativePath>../dn-parent</relativePath>
	</parent>

	<dependencies>
		<dependency>
			<groupId>com.bigfong</groupId>
			<artifactId>dn-util</artifactId>
			<version>0.0.1-SNAPSHOT</version>
		</dependency>
		<dependency>
			<groupId>com.bigfong</groupId>
			<artifactId>dn-common</artifactId>
			<version>0.0.1-SNAPSHOT</version>
		</dependency>
	</dependencies>

	<build>
		
	</build>
</project>

5.dn-common中的pom.xml设置

<?xml version="1.0" encoding="UTF-8"?>

<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>

	<artifactId>dn-common</artifactId>
	<packaging>jar</packaging>

	<name>dn-common</name>
	<!-- FIXME change it to the project's website -->
	<url>http://www.example.com</url>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<maven.compiler.source>1.7</maven.compiler.source>
		<maven.compiler.target>1.7</maven.compiler.target>
	</properties>

	<parent>
		<groupId>com.bigfong</groupId>
		<artifactId>dn-parent</artifactId>
		<version>0.0.1-SNAPSHOT</version>
		<relativePath>../dn-parent</relativePath>
	</parent>

	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.11</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
		</dependency>
	</dependencies>

	<build>

	</build>
</project>

六.骨架选择

  1.org.apache.maven.archetypes=>       maven-archetype-quickstart   ->jar

   2.org.apache.maven.archetypes=>       maven-archetype-webapp   ->war

 

七.自动部属

在dn-web项目中,<build>里添加

<plugins>
			<!-- mvn cargo:run 自动部署 -->
			<plugin>
				<groupId>org.codehaus.cargo</groupId>
				<artifactId>cargo-maven2-plugin</artifactId>
				<version>1.2.3</version>
				<configuration>
					<container>
						<containerId>tomcat7x</containerId>
						<home>J:/Tomcat</home>
					</container>
					<configuration>
						<type>existing</type>
						<home>J:/Tomcat</home>
					</configuration>
				</configuration>
				<executions>
					<execution>
						<id>cargo-run</id>
						<phase>install</phase>
						<goals>
							<goal>run</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
			<!-- mvn cargo:run 自动部署 -->
		</plugins>

  在dn-parent项目中运行mvn package -Dmaven.test.skil=true,打包成功后,在dn-web项目中运行mvn cargo:run会自动部署到tomcat中

八.多环境配置支持,例子配置数据库资源

在dn-web项目中,<build>里添加

<!-- 多环境配置 -->
		<resources>
			<resource>
				<directory>src/main/resources</directory>
				<includes>
					<include>**/*</include>
				</includes>
			</resource>
			<!-- 设置对auto-config.properties,jdbc.properties进行过虑,即这些文件中的${key}会被替换掉为真正的值 -->
			<resource>
				<directory>src/main/resources</directory>
				<includes>
					<include>jdbc.properties</include>
				</includes>
				<filtering>true</filtering>
			</resource>
		</resources>

在<project>标签最底部添加

<profiles>
		<profile>
			<id>test</id>
			<activation>
				<activeByDefault>false</activeByDefault>
			</activation>
			<properties>
				<jdbc.driver>test.mysql</jdbc.driver>
				<jdbc.url>test.url</jdbc.url>
				<jdbc.username>test.username</jdbc.username>
				<jdbc.password>test.password</jdbc.password>
			</properties>
		</profile>
		<profile>
			<id>dev</id>
			<activation>
				<activeByDefault>false</activeByDefault>
			</activation>
			<properties>
				<jdbc.driver>dev.mysql</jdbc.driver>
				<jdbc.url>dev.url</jdbc.url>
				<jdbc.username>dev.username</jdbc.username>
				<jdbc.password>dev.password</jdbc.password>
			</properties>
		</profile>
		<profile>
			<id>product</id>
			<activation>
				<activeByDefault>true</activeByDefault>
			</activation>
			<properties>
				<jdbc.driver>product.mysql</jdbc.driver>
				<jdbc.url>product.url</jdbc.url>
				<jdbc.username>product.username</jdbc.username>
				<jdbc.password>product.password</jdbc.password>
			</properties>
		</profile>
	</profiles>

在resources目录中创建文件jdbc.properties

jdbc.driver = ${dbc.driver}
jdbc.url = ${jdbc.url}
jdbc.username = ${jdbc.username}
jdbc.password = ${jdbc.password}

在打包前,在哪个环境下配置<activeByDefault>true</activeByDefault>,打包后,会生成相对配置的jdbc.properties文件

 

手动添加jar包到本地仓库

mvn install:install-file  -Dfile=xxx.jar  -DgroupId=xxx.xxx  -DartifactId=xxx.xxx   -Dversion=1.0.0   -Dpackaging=jar

 

maven聚合项目样例下载

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值