搭建nexus3
创建仓库
test-releae仓库: hosted类型,Release库
test-snapshot仓库: hosted类,Snapshot库
test-public仓库:group类型,仓库组,配置 test-releae仓库和test-snapshot仓库
快照版本和正式版本的主要区别在于,本地获取这些依赖的机制有所不同。假设你依赖一个库的正式版本,构建的时候构建工具会先在本次仓库中查找是否已经有了这个依赖库,如果没有的话才会去远程仓库中去拉取。
一、mirror镜像
${user.home}/.m2/settings.xml文件中配置
mirror相当于一个拦截器,它会拦截maven对remote repository的相关请求。
mirror 定义了两个Repository之间的镜像关系,<mirrorOf></mirrorOf>指向被镜像的镜像Repository ID,如果该镜像仓库需要认证,则配置<server></server>
由于镜像仓库完全屏蔽了被镜像仓库,当镜像仓库不稳定或者停止服务的时候,Maven仍将无法访问被镜像仓库,因而将无法下载构件
配置同一个 repository 多个 mirror 时,相互之间是备份关系,只有当仓库连不上时才会切换到另一个,而如果能连上但是找不到依赖时是不会尝试下一个 mirror 地址的。
- <mirrorOf>*</mirrorOf> 匹配所有远程仓库
- <mirrorOf>repo1,repo2</mirrorOf> 匹配仓库repo1和repo2,使用逗号分隔多个远程仓库,发送repo1或repo2的请求,转到此Repository
- <mirrorOf>*,!repo1</miiroOf> 匹配所有远程仓库,repo1除外,使用感叹号将仓库从匹配中排除
<mirrors>
<!-- 配置阿里镜像 -->
<mirror>
<id>alimaven</id>
<name>aliyun maven</name>
<url>https://maven.aliyun.com/repository/public/</url>
<mirrorOf>*</mirrorOf>
</mirror>
<!--
配置本地私服nexus3
<mirror>
<id>nexus</id>
<name>Nexus Mirror</name>
<url>http://localhost:8081/repository/test-public/</url>
<mirrorOf>central</mirrorOf>
</mirror>
-->
</mirrors>
二、repository仓库
1.全局配置
${user.home}/.m2/settings.xml文件中配置
<updatePolicy></updatePolicy>用于配置对于SNAPSHOT版本向远程仓库中查找的频率。
频率共有四种,分别是always、daily、interval、never。
当本地仓库中存在需要的依赖项目时,always是每次都去远程仓库查看是否有更新,daily是只在第一次的时候查看是否有更新,当天的其它时候则不会查看;interval允许设置一个分钟为单位的间隔时间,在这个间隔时间内只会去远程仓库中查找一次,never是不会去远程仓库中查找(这种就和正式版本的行为一样了)
<profiles>
<profile>
<id>jdk-1.8</id>
<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>
<!--设置maven私库信息-->
<profile>
<id>nexus</id>
<repositories>
<repository>
<id>nexus-test-public</id>
<name>Nexus</name>
<url>http://localhost:8081/repository/test-public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>nexus-test-public</id>
<name>Nexus</name>
<url>http://localhost:8081/repository/test-public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
</snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>
<!-- 激活jdk-1.8的配置 激活私库信息的配置 -->
<activeProfiles>
<activeProfile>jdk-1.8</activeProfile>
<activeProfile>nexus</activeProfile>
</activeProfiles>
另:
对于jdk配置,为了方便项目迁移,也可以在pom文件中配置
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
2.局部配置
pom.xml文件中配置
<repositories>
<repository>
<id>nexus-test-public</id>
<name>test-public</name>
<url>http://localhost:8081/repository/test-public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
</snapshots>
</repository>
</repositories>
三、部署jar包
1.配置验证
${user.home}/.m2/settings.xml
<!--设置私库认证信息-->
<!--
表示当需要连接到一个私有服务器的时候需要的认证信息
发布的服务器,发布的位置在POM中配置,以id为关联
-->
<servers>
<server>
<id>test-release</id>
<username>test-release-user</username>
<password>123456</password>
</server>
<server>
<id>test-snapshot</id>
<username>test-snapshot-user</username>
<password>123456</password>
</server>
</servers>
2.配置部署仓库
pom.xml
<build>
<plugins>
<!--
如果需要把源码打包,用maven-source-plugin插件
打包的时候,会把源码打包成jar文件
会生成两个jar文件,一个存放class文件的jar包,一个存放java文件的jar包
-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<attach>true</attach>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<distributionManagement>
<repository>
<id>test-release</id>
<name>test-release</name>
<url>http://localhost:8081/repository/test-release/</url>
</repository>
<snapshotRepository>
<id>test-snapshot</id>
<name>test-snapshot</name>
<url>http://localhost:8081/repository/test-snapshot/</url>
</snapshotRepository>
</distributionManagement>