Maven工程的拆分和聚合
- 工程的拆分
面对当令互联网+的行业,软件项目变得越来越庞大,复杂程度越来越高,ddd提高了开发与管理的成本。工程的拆分可以实现分模块开发与测试,可实现多线程开发与管理,提高工程代码复用度的同时也提高软件的开发速度与效率。
例如一个CRM系统的话,现在可以使用maven进行拆分,拆成Dao,Service,web - 工程的聚合
我们的CRM项目拆成多个子模块后,独立运行各个模块是无法完成软件项目的要求的,只有把它们都整合起来,分工合作才能完成工作。因此需要父工程来管理各个子模块,把它们聚合在一起运行,把Dao, Service, web打成一个独立的可运行的war包。 - 继承的理解
类似java类的继承,都是为了消除重复。子类继承父类,父类里有的方法和属性在子类中就不需要再定义和实现了,使用的时候直接调用父类就可以。我们把CRM拆分后,有一个父工程,子工程(CRM_Dao, CRM_Service, CRM_web)要用到的依赖都可以在父工程(CRM)的pom.xml先定义好,将来子工程在开发的时候就不需要再引坐标了。
通过标签来解决冲突
- 案例Demo
<dependency>
<groupId>org . apache . struts</groupId>
<artifactId>struts2 -core</artifactId>
<version>2.3.24</version>
< exclusions >
<exclusion>
<artifactId>javassist</artifactId>
<groupId>javassist</ groupId>
</exclusion>
</exclusions>
</dependency>
Maven传递依赖两个的原则
- 谁先申明,就用谁的。跟坐标代码的顺序有关
- 案例Demo
添加struts2-spring-plugin(2.3.24)在前,spring-context(4.2.3.RELEASE)在后。
<dependency>
<groupId>org. apache. struts</ groupId>
<artifactId>struts2-spring-p1ugin</artifactId>
<version>2.3.24</version>
</dependency>
<dependency>
<groupId>org. springframework</ groupId>
<artifactId>spring- context</artifactId>
<version>4. 2.3. RELEASE</version>
</dependency>
-
- 路径近者优先原则
struts2-spring-plugin(2.3.24)跟spring-context(4.2.3.RELEASE)都会添加spring-beans的依赖。Strut2-spring-plugin会引入spring-beans.3.0.5, 而spring-context会引入spring-beans.4.2.3。如果这时我们直接加入新的依赖叫spring-beans.4.2.4.RELEASE
- 路径近者优先原则
分析:系统中如果要引入spring-beans,可以有以下方法
通过引入struts2-spring-plugin,它会引入spring-beans.3.0.5的版本。需要经过的路径为2个节点。
通过引入srping-beans-4.2.3,它会引入spring-beans.4.2.3的版本,需要经过的路径为2个节点。
如果直接引入spring-beans-4.2.4,只需要经过1个节点
因此不管spring-beans.4.2.4的位置在哪,系统始终都是引入spring-beans.4.2.4的版本。
使用版本锁定实现解决冲突
在父工程的pom.xml文件中添加以下的配置
<dependencyManagement>
<!-- 在这里锁定junit的版本为4.0 -->
<dependencies>
<dependency>
<groupId> junit</ groupId>
<artifactId>junit</artifactId>
<version>4.0</version>
<scope>test</ scope>
</ dependency>
</ dependencies>
< /dependencyManagement>
子工程中的pom.xml文件添加junit坐标时就不需要版本的信息了:
<dependencies>
<--! 父工程己锁定版本,此处不再添加版本信息-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</ dependency>
</ dependencies>
- 定义版本常量
在使用坐标时,对于同一个框架,引入多次时,它的版本信息就会多次出现,所以
可以借用常量的思想,将这些版本号提取出来,在需要用到的时候,直接写版本的常量名称就可以了。 - 在pom.xml中基本配置
<properties>
<spring.version>4. 2.4. RELEASE</spring.version>
<hibernate.version>5.0.7. Final</hibernate.version>
<struts.version>2.3.24</struts.version>
<aspectj.version>1.8.7</aspectj.version>
<junit.version>4.0</junit.version>
<c3p0.version>0.9.1.2</c3p0.version>
<mysql.version>5.1.6</mysql.version>
<log4j.version>1.2.12</10g4j.version>
<slf4j.version>1.7.5</slf4j.version>
</properties>
- 引用上面的变量
<dependency>
<--! 引用上面的版本-->
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring .version }</version>
</ dependency>
使用nexus进行私服安装
Nexus 是Maven仓库管理器,通过nexus可以搭建maven仓库,同时nexus还提供强大的仓库管理功能,构件搜索功能等。
下载Nexus, 下载地址:http://www.sonatype.org/nexus/archived/
下载完毕以后,解压,进入指定的目录
安装并启动这个应用程序
cmd进入nexus解压后bin目录(D:\nexus\nexus-2.12.0-01\bin), 执行nexus.bat install
-
启动nexus
方法1:
cmd进入bin(你解压的nexus的bin)目录,执行nexus.bat start
方法2 :
直接启动nexus服务 在任务管理器中 -
查看nexus的配置文件conf/nexus.properties
# Jetty section
application-port=8081 # nexus的访问端口配置
application-host=0.0.0.0 # nexus主机监听配置(不用修改)
nexus-webapp=${bundleBasedir}/nexus # nexus工程目录
nexus-webapp-context-path=/nexus # nexus的web访问路径
# Nexus section
nexus-work=${bundleBasedir}/../sonatype-work/nexus # nexus仓库目录
runtime=${bundleBasedir}/nexus/WEB-INF # nexus运行程序目录
- 登录nexus
访问:http://localhost:8081/nexus/
使用Nexus 内置账户admin/admin123登陆:
点击右上角的Login,输入账号和密码 登陆
- 成功页面
- nexus的仓库有4种类型
- hosted,宿主仓库,部署自己的jar到这个类型的仓库,包括releases和snapshot两部分,Releases公司内部发布版本仓库、 Snapshots 公司内部测试版本仓库
- proxy,代理仓库,用于代理远程的公共仓库,如maven中央仓库,用户连接私服,私服自动去中央仓库下载jar包或者插件
- group,仓库组,用来合并多个hosted/proxy仓库,通常我们配置自己的maven连接仓库组。
- virtual(虚拟):兼容Maven1 版本的jar或者插件
- nexus仓库默认在sonatype-work目录中:
- central:代理仓库,代理中央仓库
- apache-snapshots:代理仓库
存储snapshots构件,代理地址https://repository.apache.org/snapshots/ - central-m1:virtual类型仓库,兼容Maven1 版本的jar或者插件
- releases:本地仓库,存储releases构件。
- snapshots:本地仓库,存储snapshots构件。
- thirdparty:第三方仓库
public:仓库组
将jar上传到私服
- 配置Setting
需要在客户端即部署dao工程的电脑上配置 maven环境,并修改 settings.xml 文件,配置连接私服的用户和密码 。
此用户名和密码用于私服校验,因为私服需要知道上传的账号和密码 是否和私服中的账号和密码一致。
对应nexus上的repository:
- 配置项目pom.xml
配置私服仓库的地址,本公司的自己的jar包会上传到私服的宿主仓库,根据工程的版本号决定上传到哪个宿主仓库,如果版本为release则上传到私服的release仓库,如果版本为snapshot则上传到私服的snapshot仓库
<distributionManagement>
<repository>
<id>releases</id>
<url>http://localhost:8081/nexus/content/repositories/releases/</url>
</repository>
<snapshotRepository>
<id>snapshots</id>
<url>http://localhost:8081/nexus/content/repositories/snapshots/</url>
</snapshotRepository>
</distributionManagement>
注意:pom.xml这里 和 settings.xml 配置 对应!
- 将项目dao工程打成jar包发布到私服
启动nexus后,对dao工程执行deploy命令
Run As Maven-build-输入:deploy->run
在私服上面和私服的本地仓库中可以看到已经上传
从私服上面下载jar
摘要:
没有配置nexus之前,如果本地仓库没有,去中央仓库下载,通常在企业中会在局域网内部署一台私服服务器,有了私服本地项目首先去本地仓库找jar,如果没有找到则连接私服从私服下载jar包,如果私服没有jar包私服同时作为代理服务器从中央仓库下载jar包,这样做的好处是一方面由私服对公司项目的依赖jar包统一管理,一方面提高下载速度,项目连接私服下载jar包的速度要比项目连接中央仓库的速度快的多。
- 管理仓库组
nexus中包括很多仓库,hosted中存放的是企业自己发布的jar包及第三方公司的jar包,proxy中存放的是中央仓库的jar,为了方便从私服下载jar包可以将多个仓库组成一个仓库组,每个工程需要连接私服的仓库组下载jar包。
如图所示展示了仓库组包括了本地仓库、代理仓库等。
- mavenSetting中配置setting
在客户端的setting.xml中配置私服的仓库,由于setting.xml中没有repositories的配置标签需要使用profile定义仓库。
<profile>
<!--profile的id-->
<id>dev</id>
<repositories>
<repository>
<!--仓库id,repositories可以配置多个仓库,保证id不重复-->
<id>nexus</id>
<!--仓库地址,即nexus仓库组的地址-->
<url>http://localhost:8081/nexus/content/groups/public/</url>
<!--是否下载releases构件-->
<releases>
<enabled>true</enabled>
</releases>
<!--是否下载snapshots构件-->
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<!-- 插件仓库,maven的运行依赖插件,也需要从私服下载插件 -->
<pluginRepository>
<!-- 插件仓库的id不允许重复,如果重复后边配置会覆盖前边 -->
<id>public</id>
<name>Public Repositories</name>
<url>http://localhost:8081/nexus/content/groups/public/</url>
</pluginRepository>
</pluginRepositories>
</profile>
- 使用profile定义仓库需要激活才可生效。
<activeProfiles>
<activeProfile>dev</activeProfile>
</activeProfiles>
声明参考资料:黑马程序员资料