架构(二)Maven安装以及Nexus配置

一 Maven安装配置

1.1 下载

http://mirrors.tuna.tsinghua.edu.cn/apache/maven/maven-3/3.5.4/binaries/apache-maven-3.5.4-bin.tar.gz

1.2 上传并解压

tar -zxvf apache-maven-3.5.4-bin.tar.gz

1.3 将解压好的文件移到/data/program/software/maven3

mv apache-maven-3.5.4/ /data/program/software/maven3

1.4 配置全局变量(/etc/profile)

export MAVEN_HOME=/data/program/software/maven3
export PATH=$PATH:$MAVEN_HOME/bin

如果是非root用户安装,可以配置用户变量

~/.bash_profile

1.5 刷新配置并测试(source /etc/profile)

mvn -v

1.6 settings.xml配置

1.6.1 maven的配置文件settings.xml存在两个地方

1.安装的地方:${MAVEN_HOME}/conf/settings.xml 全局配置
2.用户的目录:${user.home}/.m2/settings.xml  用户配置

1.6.2 如下是settings.xml的配置

本地仓库,该值表示构建系统本地仓库的路径,默认值是${user.home}/.m2/repository
<localRepository>usr/local/maven</localRepository>
Maven是否需要和用户交互以获得输入,如果maven需要和用户交互以获得输入,则设置成true,反之false,默认是true
<interactiveMode>true</interactiveMode>
maven是否需要使用plugin-registry.xml文件来管理插件版本
如果设置成true,则在{user.home}/.m2下需要有一个plugin-registry.xml来对plugins的版本进行管理
默认是false
<usePluginRegistry>false</usePluginRegistry>
表示maven是否需要在离线模式下运行,如果构建系统需要在离线模式下运行,则为true,默认false
当由于网络设置原因或者安全因素,构建服务器不能连接远程仓库的时候,该配置就十分有用
<offline>false</offline>
当插件的组织ID(groupId)没有显示提供时,供搜寻插件组织ID(groupId)的列表
该元素包含一个pluginGroup元素列表,每个子元素包含了一个组织ID(groupId)
当我们使用某个插件,并且没有在命令行为其提供组织ID的时候,Maven就会使用该列表
默认情况下该列表包含了org.apache.maven.plugins
<pluginGroups>
  <pluginGroup>org.codehaus.mojo</pluginGroup>
</pluginGroups>

二 使用Nexus配置Maven私有仓库

2.1 安装配置Nexus

2.1.1 下载nexus

https://www.sonatype.com/download-oss-sonatype

2.1.2 上传解压

tar -zxvf nexus-3.13.0-01-unix.tar.gz

2.1.3 将解压好的文件移到/data/program/software/nexus3

mv nexus-3.13.0-01/ /data/program/software/nexus3

2.1.4 修改配置

a 修改bin目录下nexus.rc文件

run_as_user="root"

b 修改bin目录下的nexus文件

INSTALL4J_JAVA_HOME_OVERRIDE=/data/program/software/java8

c 修改etc目录下的nexus-default.properties文件

application-port=8282

d 修改bin目录下的nexus.vmoptions文件

如何机器内存不足的话还需要修改jvm配置

-XX:LogFile=./sonatype-work/nexus3/log/jvm.log
-Dkaraf.data=./sonatype-work/nexus3
-Djava.io.tmpdir=./sonatype-work/nexus3/tmp

2.1.5 启动

进入bin目录启动:./nexus run &

2.1.6 访问

localhost:8081
默认端口号:8081
默认账号:admin
默认密码:admin123

 2.2 配置maven的settings.xml文件

<localRepository>${user.home}/.m2/repository</localRepository>
<offline>false</offline>
<interactiveMode>true</interactiveMode>
<pluginGroups>
	<pluginGroup>org.mortbay.jetty</pluginGroup>
	<pluginGroup>org.jenkins-ci.tools</pluginGroup>
</pluginGroups>
		
<servers>
	<server>
		<id>nexus-releases</id>
		<username>admin</username>
		<password>admin123</password>
	</server>
	<server>
		<id>nexus-snapshots</id>
		<username>admin</username>
		<password>admin123</password>
	</server>
</servers>
这是server的ID(不是登录进来的user),与maven想要连接上的repository/mirror中的id元素相匹配。
username,password这两个元素成对出现,表示连接这个server需要用户名和密码,在nexus中,默认管理员用
户admin,密码admin123,这里使用两个服务器配置,分别对应release,snapshot
		
<mirrors>
	<mirror>
		<id>nexus-releases</id>
		<mirrorOf>*</mirrorOf>
		<url>http://localhost:8081/repository/maven-public/</url>
	</mirror>
	<mirror>
		<id>nexus-snapshots</id>
		<mirrorOf>*</mirrorOf>
		<url>http://localhost:8081/repository/maven-snapshots/</url>
	</mirror>
</mirrors>
id唯一的镜像标识和用户友好的镜像名称,id被用来区分mirror元素,并且当连接时候被用来获得相应的证书
mirrorOf镜像所包含的仓库id,列如,指向maven central仓库的镜像(http://repo1.maven.org/maven2/),
设置这个元素为central,更多的高级映射列如repo1, repo2或者*都是可以的,没必要一定和mirror相匹配,在这里
mirrorOf应该使用*,以表明是所有仓库都会被镜像到指定的的地址
url镜像基本的url,构建系统将使用这个url连接仓库,这里应该添加nexus的地址,地址可以在nexus仓库页面中找到
<profiles>
	<profile>
		<id>nexus</id>
		<repositories>
			<repository>
				<id>nexus-releases</id>
				<url>http://nexus-releases</url>
				<releases>
					<enabled>true</enabled>
				</releases>
				<snapshots>
					<enabled>true</enabled>
				</snapshots>
			</repository>
			<repository>
				<id>nexus-snapshots</id>
				<url>http://nexus-snapshots</url>
				<releases>
					<enabled>true</enabled>
				</releases>
				<snapshots>
					<enabled>true</enabled>
				</snapshots>
			</repository>
		</repositories>
	</profile>
</profiles>
profile项代表maven的基本配置。按照maven的一贯尿性,很多xml的配置项都会有一个配置项的复数形式作
为父节点,以保证该配置项可以配置多个。在profiles项中,当然也可以配置多个profile,不过在这里配一个就够了,
下面介绍profile项的各个子节点
id用来确定该profile的唯一标识
repositories/repository用以规定依赖包仓库的相关信息,在下属节点中,id就不用多说了,url是指定仓库地址,
这里使用伪造的地址,否则即使设置了mirror,maven也有可能会直接从中央仓库下载包;releases和snapshots放在
一块说吧,这两个节点下属的enabled节点用以规定相应的依赖包是否对当前策略有效,假如将snapshot的enabled设置为disable,
则不会下载snapshot包
<activeProfiles>
	<activeProfile>nexus</activeProfile>
</activeProfiles>
用以规定当前启用的配置,将对应的profile的id加入到这一项即可使profile生效

2.3 上传jar到nexus

2.3.1 第一种方式

mvn deploy:deploy-file -DgroupId=com.alibaba -DartifactId
=dubbo -Dversion=2.4.8 -Dpackaging=jar -Dfile=/users/dennis/Documents/software/dubbo-2.8.4.jar
-Durl=http://localhost:8081/repository/maven-releases/ -DrepositoryId=nexus-releases

2.3.2 第二种方式

代码的pom.xml中直接接入
<distributionManagement>
	<repository>
		<id>nexus-releases</id>
		<name>maven-releases</name>
		<url>http://localhost:8081/repository/maven-releases/</url>
	</repository>
</distributionManagement>
mvn deploy

猜你喜欢

转载自www.cnblogs.com/pw818/p/9424724.html