挑战常规--搭建gradle、maven私人仓库很简单
常规
百度搜索“搭建maven私有仓库”,搜索到的结果几乎都是使用nexus

不一样的简单
如果了解maven上传原理,完全没必要搞得那么复杂庞大,区区不足百行代码就可以实现一个私有仓库。
maven上传的核心本质是:使用Http PUT上传,使用Http GET下载。再简单不过的代码如下:
@WebServlet("/")
public class RepositoryServer extends HttpServlet
{
/**储存位置 */
private File path;
public void init(ServletConfig config) throws ServletException
{
super.init(config);
//或者指定其他位置
path = new File(config.getServletContext().getRealPath("/repository"));
}
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
{
String reqPath = req.getServletPath();
File reqFile = new File(path, reqPath);
if (!reqFile.exists())
{
resp.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
if (reqFile.isDirectory())
{
resp.sendError(HttpServletResponse.SC_FORBIDDEN);
} else
{
try (OutputStream out = resp.getOutputStream())
{
Files.copy(reqFile.toPath(), out);
}
}
}
protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
{
String reqPath = req.getServletPath();
File reqFile = new File(path, reqPath);
if (reqPath.endsWith("/"))
{
reqFile.mkdirs();
} else
{
File parentDir = reqFile.getParentFile();
if (!parentDir.exists())
{
parentDir.mkdirs();
}
try (InputStream in = req.getInputStream())
{
Files.copy(in, reqFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
}
}
}
}
测试上传和引用(这里执行gradle使用,maven一样参考)
apply plugin: 'java'
apply plugin: 'maven'
version "0.9.9.1"
group "cn.heihei.testproject"
project.ext.artifactId = "model" repositories {
jcenter()
} dependencies { testImplementation 'junit:junit:4.12'
}
tasks.withType(JavaCompile) {
options.encoding = "UTF-8"
}
task sourcesJar(type: Jar) {
classifier = 'sources'
from sourceSets.main.allSource
}
artifacts {
archives sourcesJar
}
jar {
manifest {
attributes('Implementation-Title': project.name,
'Implementation-Version': project.version) }
}
uploadArchives{
repositories {
mavenDeployer{
repository(url:"http://localhost:8080/RepositoryServer/")
pom.project{
version project.version
groupId project.group
packaging 'jar'
artifactId project.ext.artifactId
}
}
}
}
apply plugin: 'java'
repositories {
maven{
url "http://localhost:8080/RepositoryServer/"
}
jcenter()
}
dependencies {
implementation 'cn.heihei.testproject:model:0.9.9.1'
testImplementation 'junit:junit:4.12'
}
bash结果
ProjectModel>gradle uploadArchives
Could not find metadata cn.heihei.testproject:model/maven-metadata.xml in remote (http://localhost:8080/RepositoryServer/) BUILD SUCCESSFUL in 1s
4 actionable tasks: 1 executed, 3 up-to-date
ProjectServer>gradle build
Download http://localhost:8080/RepositoryServer/cn/heihei/testproject/model/0.9.9.1/model-0.9.9.1.pom
Download http://localhost:8080/RepositoryServer/cn/heihei/testproject/model/0.9.9.1/model-0.9.9.1.jar BUILD SUCCESSFUL in 1s
2 actionable tasks: 2 up-to-date
目录显示
if (reqFile.isDirectory())
{
if (!reqPath.endsWith("/"))
{
resp.sendRedirect(req.getContextPath() + reqPath + "/");
return;
}
resp.setContentType("text/html;charset=utf-8");
try (PrintWriter wr = resp.getWriter())
{
wr.println("<html><body>");
wr.println("<h1>" + reqPath + "</h1>");
wr.println("[上一层] <a href='../'>..</a><br>");
File[] fs = reqFile.listFiles();
if (fs != null && fs.length > 0)
{
for (File f : fs)
{
if (f.isFile())
{
wr.println("[文件] <a href='" + f.getName() + "'>" + f.getName() + "</a><br>");
} else
{
wr.println("[目录] <a href='" + f.getName() + "/'>" + f.getName() + "</a><br>");
}
}
}
wr.println("</body></html>");
}
}
安全
作为私钥仓库,使用basic 安全认证进行控制访问
简单代码
private String authorization;
public void init(ServletConfig config) throws ServletException
{
super.init(config);
path = new File(config.getServletContext().getRealPath("/repository"));
authorization="aGVpaGVpOjY1NDRjNGRmMGM1NjhhNjg5ZDUwN2QwNjJkMTYyNmJk"; //或从其他地方加载
}
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
{
if(!check(req,resp))
{
return;
}
super.service(req, resp);
}
private boolean check(HttpServletRequest req, HttpServletResponse resp) throws IOException
{
String auth=req.getHeader("Authorization");
if(auth!=null&&auth.startsWith("Basic "))
{
auth=auth.substring(6);
if(auth.equals(authorization))
{
return true;
}
}
resp.setHeader("WWW-Authenticate", "Basic realm=\"Need Login\", charset=\"UTF-8\"");
resp.sendError(HttpServletResponse.SC_UNAUTHORIZED);
return false;
}
上传和引用控制
uploadArchives{
repositories {
mavenDeployer{
repository(url:"http://localhost:8080/RepositoryServer/") {
authentication(userName: "heihei", password: "6544c4df0c568a689d507d062d1626bd")
}
pom.project{
version project.version
groupId project.group
packaging 'jar'
artifactId project.ext.artifactId
}
}
}
}
repositories {
maven{
url "http://localhost:8080/RepositoryServer/"
authentication {
basic(BasicAuthentication)
}
credentials {
username = 'heihei'
password = '6544c4df0c568a689d507d062d1626bd'
}
}
jcenter()
}
思考
当然如果仅仅个人非团队开发,是否本地仓库更好?
repository(url:"file:///D:/wamp64/www/repository")
也可以使用web容器,如NanoHTTPD、tomcat embeded、etty embeded,变成微服务
挑战常规--搭建gradle、maven私人仓库很简单的更多相关文章
- Git: 搭建一个本地私人仓库
Git: 搭建一个本地私人仓库 寝室放个电脑.实验室也有个电脑 为进行数据同步,充分利用实验室的服务器搭建了个本地私人仓库 1. 安装流程 当然首先保证服务器上与PC机上都已经安装了可用的Git 在P ...
- 使用nexus搭建一个maven私有仓库
使用nexus搭建一个maven私有仓库 大家好,我是程序员田同学.今天带大家搭建一个maven私有仓库. 很多公司都是搭建自己的Maven私有仓库,今天就带大家使用nexus搭建一个自己的私有仓库, ...
- 【图文并茂】 做开发这么久了,还不会搭建服务器Maven私有仓库?这也太Low了吧
大家好,我是冰河~~ 最近不少小伙伴想在自己公司的内网搭建一套Maven私服环境,可自己搭建的过程中,或多过少的总会出现一些问题,问我可不可以出一篇如何搭建Maven私服的文章.这不,就有了这篇文章嘛 ...
- 【Maven】maven的常用命令以及搭建maven私人仓库
一.maven环境搭建 1. 二.maven常用命令 1.创建一个新的项目: mvn archetype:create -DgroupId=com.puyangsky.test -DartifactI ...
- 搭建本地的yum仓库-较简单
1.创建目录安装软件程序 1.在/root路径下创建123.sh文件,把此文件复制到123.sh里, sh 123.sh2.首选安装nginx,作为web展示 3.强力清除老版本残留rpm -e n ...
- 实战maven私有仓库三部曲之二:上传到私有仓库
在上一章<实战maven私有仓库三部曲之一:搭建和使用>我们搭建了maven私有仓库,并体验了私有仓库缓存jar包的能力,避免了局域网内开发人员去远程中央仓库下载的痛苦等待,本章我们再来体 ...
- maven的使用之一简单的安装
首先,我们知道,在传统的项目中,我们会导入一堆的jar包,那样的话,我们会发现我们的jar包的大小已经占了整个项目大小的90%以上,甚至更多,而且,我们的jar包只能自己使用,如果 其他人想用的话,还 ...
- Android业务组件化之Gradle和Sonatype Nexus搭建私有maven仓库
前言: 公司的业务组件化推进的已经差不多三四个月的时间了,各个业务组件之间的解耦工作已经基本完成,各个业务组件以module的形式存在项目中,然后项目依赖本地的module,多少有点不太利于项目的并行 ...
- 使用Nexus搭建Maven本地仓库
阅读目录 序 Nexus 本文版权归mephisto和博客园共有,欢迎转载,但须保留此段声明,并给出原文链接,谢谢合作. 文章是哥(mephisto)写的,SourceLink 序 在工作中可能存在有 ...
随机推荐
- 学习C++后感
c++是一门系统级语言,记得大一是要学习这门课时还上网找过学习方法.但网上很多学习方法看的我都头晕,都说学习C++很难,有的说学C++前最好先学C语言,有的说学C++最好不要学C语言,当翻了C++课本 ...
- Windows 10 IoT Core 17083 for Insider 版本更新
1月26日,微软发布了Windows 10 IoT Core 17083 for Insider版本更新,概括如下: 新特性:1. General bug fixes2. Enabled Flash ...
- Python编程练习:使用 turtle 库完成叠边形的绘制
绘制效果: 源代码: # 叠边形 import turtle turtle.setup(650,350,200,200) turtle.penup() turtle.fd(-100) turtle.l ...
- Redis之分布式锁
目录 一.加锁原因 二.原子操作 三.分布式锁 四.分布式锁常见问题 一.加锁原因 在一些比较高并发的业务场景,经常听到通过加锁的方法实现线程安全. 下面简单介绍一下 1.1 加锁方式 数据库锁 数据 ...
- linux中修改字符编码
一. ubuntu修改字符编码 1. 添加字符编码,例如zh_CN.UTF-8,有两种方式 方法1:locale-gen zh_CN.UTF-8 #locale-gen命令只在ubuntu中才有 ...
- 关于I/O编程
IO在计算机中指Input/Output,也就是输入和输出 由于程序在运行时,数据是驻留在内存中的,并由CPU这个超快的计算核心来执行,涉及到数据交换的地方,通常是磁盘.网络等,就需要IO接口 IO编 ...
- JavaScript的屏幕输出及时间函数
Js屏幕输出 重定向页面输出: 更改输出流,如果在加载时使用则会正常排序,如果在HTML加载完成后使用,如点击事件,则会全部覆盖原HTML的内容. 1 <script> 2 //参数为输出 ...
- MapReduce中的Join
一. MR中的join的两种方式: 1.reduce side join(面试题) reduce side join是一种最简单的join方式,其主要思想如下: 在map阶段,map函数同时读取两个文 ...
- mysql 开发进阶篇系列 51 权限与安全(权限表user,db详细介绍 )
一.概述 mysql 的权限系统主要用来对连接到数据库的用户进行权限验证,以此来判断此用户是否属于合法用户,以及合法用户给予的相应数据库权限.下面将介绍权限系统的工作原理,以及将要熟练掌握账号的管理和 ...
- Android--Service之AIDL传递复杂对象
前言 Android的AIDL不仅可以在绑定服务中传递一些Android规定的数据类型的数据,还可以传递一些复杂类型的数据.但是与传递系统允许的数据类型相比,复杂类型数据的传递要做更多的工作,本篇博客 ...