使用github搭建个人Maven私服

准备

申请Github帐号并配置ssh public key: http://blog.youkuaiyun.com/psu_vjd/article/details/54317621

安装Git工具: http://blog.youkuaiyun.com/laiahu/article/details/7516939

安装Maven工具: http://blog.youkuaiyun.com/javaee_ssh/article/details/43774583

搭建过程

  1. 利用github网站中创建一个新的仓库,记下仓库地址:
    git@github.com:${github_account}/${maven-repo}.git
  2. 初始化git本地仓库,添加远端git仓库地址
    cd ${git_repo_path}
    git init
    git remote add origin git@github.com:${github_account}/${maven-repo}.git
  3. 创建.gitignore 并提交git本地仓库master分支
    echo "" >.gitignore
    git add -f .gitignore
    git commit -m 'add .gitignore' && git push origin master
  4. 创建分支snapshot并push到远程分支
    git branch snapshot
    git push origin snapshot
    git checkout snapshot
  5. 当开发完成后发布新的artifects(例如:${groupId}:${artifactId}:${version}),首先利用mvn deploy将artifects发布至刚刚创建的git仓库中
    mvn deploy -DaltDeploymentRepository=${distribute_id}::default::file:${git_repo_path}
    其中,${distribute_id}对应pom文件中distributionManagement/repository/id,也可随意填写
  6. 将需要发布对应版本的artifects提交至本地git仓库中,然后push至对应的分支snapshot 
    cd ${git_repo_path}
    git checkout snapshot
    git add -f ${groupId}/${artifactId}/${version}
    git commit -m 'deploy ${groupId}:${artifactId}:${version}'
    git push origin snapshot
  7. 在settings.xml中添加如下配置:
    <?xml version="1.0"?>
    <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    		  xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
    
    	<profiles>
    		<profile>
    			<id>${github_account}-github-maven</id>
    			<activation>
    				<activeByDefault>true</activeByDefault>
    				<jdk>1.7</jdk>
    			</activation>
    			<repositories>
    				<repository>
    					<id>${github_account}-maven-snapshot-repository</id>
    					<name>${github_account}-maven-snapshot-repository</name>
    					<url>https://raw.githubusercontent.com/${github_account}/${maven-repo}/snapshot</url>
    				</repository>
    			</repositories>
    		</profile>
    	</profiles>
    
    	<pluginGroups>
    		<pluginGroup>org.apache.maven.plugins</pluginGroup>
    	</pluginGroups>
    
    	<activeProfiles>
    		<activeProfile>${github_account}-github-maven</activeProfile>
    	</activeProfiles>
    
    </settings>
  8. 在pom.xml中使用上传的jar包:
    <dependencies>
    	<dependency>
    		<groupId>${groupId}</groupId>
    		<artifactId>${artifactId}</artifactId>
    		<version>${version}</version>
    	</dependency>
    </dependencies>
### 设置和使用Maven私有仓库 #### 安装Nexus服务器 为了建立Maven私有仓库,通常会选择安装Nexus服务器。通过GitHub上的资源可获取详细的搭建指南[^1]。完成部署之后,在浏览器输入`ip:9090`就能到达私有库的首页界面[^2]。 #### 修改settings.xml文件 为了让本地项目能连接至新设立的私有仓库,需调整Maven全局或用户级别的`settings.xml`配置文档。具体来说是在其中加入镜像(mirrors)、服务器(servers)节点来指定私服地址及其认证信息: ```xml <mirrors> <mirror> <id>nexus</id> <url>http://your.nexus.server/repository/maven-public/</url> <mirrorOf>*</mirrorOf> </mirror> </mirrors> <servers> <server> <id>nexus-releases</id> <username>admin</username> <password>admin123</password> </server> </servers> ``` 上述代码片段中的URL应替换为实际的Nexus服务路径,而用户名密码则对应于拥有上传权限的账户凭证[^4]。 #### 使用POM引入依赖项 当一切准备就绪后,便可以在项目的`pom.xml`里声明所需的构件坐标,格式如下所示: ```xml <dependency> <groupId>com.example</groupId> <artifactId>example-artifact</artifactId> <version>1.0-SNAPSHOT</version> </dependency> ``` 只要确保所填写的信息与已发布的制品相匹配,就可以顺利拉取到对应的JAR包并应用于工程之中。 #### 备份与恢复策略 考虑到数据的安全性和稳定性,对于已经投入使用Maven私有仓库而言,定期执行备份操作至关重要。这不仅有助于防止意外丢失重要资料,也能简化跨环境迁移的工作流程[^3]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值