挑战常规--搭建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 序 在工作中可能存在有 ...
随机推荐
- Appium + Python 测试 QQ 音乐 APP的一段简单脚本
1. 大致流程 + 程序(Python):打开 QQ 音乐,点击一系列接收按键,进入搜索音乐界面,输入『Paradise』,播放第一首音乐. 2. Python 脚本如下 from appium im ...
- 关键字提取算法TF-IDF
在文本分类的学习过程中,在“如何衡量一个关键字在文章中的重要性”的问题上,遇到了困难.在网上找了很多资料,大多数都提到了这个算法,就是今天要讲的TF-IDF. 总起 TF-IDF,理解起来相当简单,他 ...
- springbean的生命周期
1.Spring对Bean进行实例化(相当于程序中的new Xx())2.Spring将值和Bean的引用注入进Bean对应的属性中3.如果Bean实现了BeanNameAware接口,Spring将 ...
- Matplotlib 使用 - 《Python 数据科学手册》学习笔记
一.引入 import matplotlib as mpl import matplotlib.pyplot as plt 二.配置 1.画图接口 Matplotlib 有两种画图接口: (1)一个是 ...
- 深入分析Java I/O的工作机制 (三)网络I/O的工作机制 很详细
3.网络I/O的工作机制 前言:数据从一台主机(服务端)发送到网络中的另一台主机(客户端)需要经过很多步骤:首先需要有相互沟通的意向.其次要有能够沟通的物理渠道(物理链路):是通过电话,还是直接面对面 ...
- JavaScript 快速入门
JavaScript是jquery的基础, JavaScript是一种描述性语言 JavaScript的组成 :ECMAScript,BOM,DOM. JavaScript的基本结构 <scri ...
- SWIG 基本概念和入门
C 和 C++ 被公认为(理当如此)创建高性能代码的首选平台.对开发人员的一个常见要求是向脚本语言接口公开 C/C++ 代码,这正是 Simplified Wrapper and Interface ...
- Java 程序员必备的 15 个框架,前 3 个地位无可动摇!
Java 程序员方向太多,且不说移动开发.大数据.区块链.人工智能这些,大部分 Java 程序员都是 Java Web/后端开发.那作为一名 Java Web 开发程序员必须需要熟悉哪些框架呢? 今天 ...
- [MongoDB]Mongo基本使用
[MongoDB]Mongo基本使用: 汇总: 1. [MongoDB]安装MongoDB2. [MongoDB]Mongo基本使用:3. [MongoDB]MongoDB的优缺点及与关系型数据库 ...
- 今日头条面试题——LRU原理和Redis实现
很久前参加过今日头条的面试,遇到一个题,目前半部分是如何实现 LRU,后半部分是 Redis 中如何实现 LRU. 我的第一反应应该是内存不够的场景下,淘汰旧内容的策略.LRU ... Least R ...