百度搜索“搭建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,变成微服务
如对本文有疑问, 点击进行留言回复!!
网友评论