1、配置自己的maven ,并配置自己的maven仓库
2、建立的项目中 src/main/resource 放配置文件 约定优于配置
src/test/resource 放配置文件
3、pom.xml中配置依赖,可以自动下载包,中央找pom.xml
4 例如 三个项目,user-core user-log user-service
前两个要 clean install 第三个 引用 ,如果第一个user-core导入过commons-logging ,则导入user-core项目的项目也会自动导入commons-logging包.
<dependency>
<groupId>com.yanlei.use</groupId>
<artifactId>user-log</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId> // 表达式,表示本项目
<artifactId>user-core</artifactId>
<version>${project.version}</version> //表达式
</dependency>
5 scope 默认的是compile
compile 编译范围有效 此包会往下会传递
provide 打包不打进去 ,编译测试的时候会使用此包,,,,,,,, 如servlet-api,运行时 TOMCAT提供,打包如果打入,会冲突
runtime 编译时候不依赖, 运行时依赖 mysqlconnect 只有运行时才需要,提供所写代码的接口实现.
test 测试的时候依赖,打包时候不打. 如JUINT,DBUnit 不会传递,防止没用包太多
6 依赖传递. c-->b b-->a 所以c依赖a
D-->A A-->B1 D-->C C-->B2 D依赖B1(D-A-B1,D-C-B2 那个先写依赖那个) (想依赖B2, 在A中<exclusive>B1GroupID 和artifactID 可以排除A下级中的依赖)
D-->A A-->B1 D-->C C-->B2 D-B3 D依赖B3 (D-A-B1,D-C-B2,D-B3)直接依赖层次短的
7 聚合三个模块,,, 写个简单的maven项目 pom.xml中 ,也可以吧pom放在三个模块的共同目录下, 然后改下路径 user-core.xml即可.
<!-- 聚合三个模块 -->
<modules>
<module>../user-core</module>
<module>../user-log</module>
<module>../user-service</module>
</modules>
8 每个模块有很多公共部分,可以把公共部分抽取出来,写成父类,然后其他继承.
父类一样是新建个简单的maven项目,可以公共的部分有
父类pom.xml
<span style="white-space:pre"> </span><url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencyManagement> //只是负责管理,继承他的项目并不会拥有它的包
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
</dependencyManagement>
子类
<span style="white-space:pre"> </span><parent>
<groupId>com.yanlei.user</groupId>
<artifactId>user-parent</artifactId> //父类的
<version>0.0.1-SNAPSHOT</version>
<relativePath>../user-parent/pom.xml</relativePath> //父类的路径,如果是在公共目录下,可以写成pom.xml
</parent>
<artifactId>user-service</artifactId>
<packaging>jar</packaging>
<name>user-service</name>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId> //可以不用写版本了,父类中有版本
</dependency>
<dependency>
<groupId>com.yanlei.user</groupId>
<artifactId>user-log</artifactId>
<version>0.0.1-SNAPSHOT</version> //自己依赖的包
</dependency>
<dependency>
<groupId>com.yanlei.user</groupId>
<artifactId>user-core</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId> <span style="font-family: Arial, Helvetica, sans-serif;">//可以不用写版本了,父类中有版本 </span>
</dependency>
</dependencies>
9 添加 nexus sonatype 类似私服,建在自己的局域网,方便大家下载,不用每次都去中央仓库
下载 nexus-2.10.0-02-bundle.zip包,配置其 bin 到path下, 然后修改 bin/jsw/conf/wrapper.java.command 为java的绝对路径,包含Java程序
cmd下安装nexus install 服务,然后启动 ,
进行访问 http://localhost:8081/nexus/#welcome login admin admin123
10 使用nexus .
host类型的工厂,针对内部, 面向内网. mvn deploy
可以吧自己的项目提交跟工厂,根据snapshot,releases 分,3rd 针对找不到pom.xml的包,自己去官网下,,,,手动,导入
center工厂 nexus中没有的包, 去中央工厂下载,然后放入数据放入center工厂,
(以后新增一个包,如果组中的各工厂有,从nexus中下载,如果没有,通过nexus 到中央工厂下载)
方法一: 可以在父类中,一个一个的将仓库导入,但是太麻烦,可以把几个仓库组合成一个组,放入public Respositories 然后用他一个URL就方便了许多.
<repositories>
<span style="white-space:pre"> </span><repository>
<span style="white-space:pre"> </span></repository>
</repositories>
<repositories>
<repository>
<id>nexus</id>
<name>Nexus Repository</name>
<url>http://localhost:8081/nexus/content/groups/public/</url> //其他人使用,可以改变ip地址即可
<releases>
<enabled>true</enabled>
</releases>
<!-- 默认关闭 -->
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
方法二:按方法一,每个项目都要导入一次,太麻烦,可以在setting.xml中
生成仓库
<profile>
<id>nexusRepo</id>
<repositories>
<repository>
<id>nexus</id>
<name>Nexus Repository</name>
<url>http://localhost:8081/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<!-- 默认关闭 -->
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
</profile>
激活上面仓库: 注销 父类中的 <repositories>就可以了.
<activeProfiles>
<activeProfile>nexusRepo</activeProfile>
11
在setting中,配置镜像
<mirror>
<id>nexusMirror</id>
<mirrorOf>nexus,central</mirrorOf> 或者* 代表所有工厂
<name>Human Readable Name for this Mirror.</name>
<url>http://localhost:8081/nexus/content/groups/public/</url>
</mirror>
访问nexus自定义的工厂,central(maven-model-builder-3.3.9中)工厂,都自动找配的url,找不到就不会去中央工厂(被屏蔽了)了.
12
maven-model-builder-3.3.9中的pom.xml,若想改变中央工厂的属性,在setting中,
<profile>
<repositories>
<repository>
<id>central</id>
<name>Central Repository</name>
<url>https://repo.maven.apache.org/maven2</url>
<layout>default</layout>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<profile>
<!--激活生效-->
<activeProfiles>
<activeProfile>central</activeProfile>
这样就可以使新的中央工厂的配置生效
13 注:装了nexus后,本地仓库有,而nexus没有, 也会先找nexus,可以更新下索引
14
发布自己的项目, parent中pom.xml -----------------clean deploy
不过在parent.xml中
<distributionManagement>
<repository>
<id>user-release</id> //发布release程序
<name>user release resp</name>
<url>http://localhost:8081/nexus/content/repositories/releases/</url> 到release的工厂
</repository>
<snapshotRepository>
<id>user-snapshots</id> //发布snapshots的程序
<name>user snapshots resp</name>
<url>http://localhost:8081/nexus/content/repositories/snapshots/</url> 到snapshot的工厂
</snapshotRepository>
</distributionManagement>
然后再setting.xml中授权,这样就可以把 自己的项目发布到 nexus的仓库中,.....也再一次说明了,host的仓库,是针对内部的仓库. 接受内部项目....center仓库 接受外部jar包
<server>
<id>user-release</id> //id跟上面的id匹配
<username>admin</username>
<password>admin123</password>
</server>
<server>
<id>user-snapshots</id> //id跟上面的ID匹配
<username>admin</username>
<password>admin123</password>
</server>
</servers>
15 在nexus中也可以建立自己的仓库,管理自己的模块, 建立的项目必须为host 仓库,然后 配置权限,赋予角色,建立用户.
仓库必须为两个,一个release,一个snapshot的. ..
部署的话,还是按照以上,先配置 <distributionManagement> 然后再setting中设置用户 授权