设为首页 加入收藏

TOP

挑战常规--搭建gradle、maven私人仓库很简单(一)
2019-09-17 17:36:26 】 浏览:61
Tags:挑战 常规 搭建 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

首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇工业4.0架构方案介绍 下一篇activiti工作流的web流程设计器整..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目