前言:mavn是比较常用的项目管理工具,如果在团队开发中,对于可公用的程序需要单独剥离出来并且仍想被maven管理的话,可能需要一个统一的远程jar管理平台,在maven系,有各种私服系统,我们在这里介绍一个轻量的maven代理器.其非常简单,对于小型公司,其足够实用..
maven-proxy是一个基于http/servert的服务器,用于存储基于maven的项目包(jar等)
maven-proxy的套路大概如下:
1)客户端开发机器,安装maven工具(http://maven.apache.org )
2)远程代理端,安装maven-proxy(http://maven-proxy.codehaus.org )
3)客户端maven通过配置远程jar代理服务器地址,来获取本地没有的jar
4)管理员或者开发者向proxy中push可用的maven项目(包括jar,source,pom.xml文件等)
5)开发机在maven项目中引用proxy中的jar
1.mavn-proxy的安装
从(http://maven-proxy.codehaus.org中下载maven-proxy,其有2个类型的安装程序
1)一个maven-proxy-standalone-0.2.zip:这个是基于java的一个程序,解压后获取jar.通过命令行执行其main方法以启动服务.
2)maven-proxy-webapp-0.2.zip:基于servet的java web项目,放在tomcat中既可使用..
maven-proxy应该是一个服务的方式被使用,我们在这里仅仅介绍,standaone方式的安装
maven-proxy在安装之前,需要对配置文件进行修改,这也是其服务得以正确使用的前提:
在其网站上下载*.properties文件(http://maven-proxy.codehaus.org/Configuration )
如下是本人可用properties文件全文:
################ GLOBAL SETTINGS # This is where maven-proxy stores files it has downloaded repo.local.store=./target/repo #The port to listen on - not used if loaded as a webapp port=9999 #This is the base area that all files are loaded from. While it is possible to leave this blank, this behaviour #is deprecated and will be disabled in version 2.0. There are too many namespace conflicts caused by not using #a prefix. #The repository will be shown at http://localhost:9999/repository/ #for the .war loaded into a webapp server, the default prefix is "repository" (edit the web.xml to change) # As maven doesn't like a trailing slash, this address shouldn't have one either. prefix=repository #This is the simple date format used to display the last modified date while browsing the repository. lastModifiedDateFormat=yyyy/MM/dd HH:mm:ss ################ SNAPSHOT HANDLING #If you want the proxy to look for newer snapshots, set to true snapshot.update=true ################ M2 METADATA HANDLING #If you want the proxy to prevent looking for newer metadata, set to false (default is true) #metadata.update=false ################ M2 POM HANDLING #If you want the proxy to look for newer POMs, set to true (default is false) pom.update=true ################ PROMOTION HANDLING # ***** NOT CURRENTLY IMPLEMENTED ***** #Promotion describes the process by which new artifacts are loaded to global maven-proxy repository. It # is designed to be used by "higher security installations" that do not want to acquire artifacts from # remote repositories without approval. # #If promotion handling is enabled, then the proxy will not download remote artifacts without permission # (local repositories with copy=false are considered to be local) # #Permission to download is granted via the Promotion menu which will be enabled # when promotion handling is enabled. # #If promotion is false, artifacts are sourced from any repository as per normal. # #Promotion and snapshots: If promotion is enabled, snapshots are not downloadable. The concept of using # a snapshot in a production build (which is primarily what promotion is for) is counterintuitive. ## promotion=false ################ WEB INTERFACE # This defines the absolute URL the server should use to identify itself. # This can often be determined automatically, but we recommend you specify # it explicitly to prevent problems during startup. # The prefix will be added to this for the actual repository # i.e. proxy available at http://localhost:9999/, repository at http://localhost:9999/repository serverName=http://localhost:9999 #If true, the repository can be browsed browsable=true #If true, the repository can be searched searchable=true #Not currently implemented. Will allow webdav access to the repository at some point. webdav=true #Stylesheet - if configured, will override the default stylesheet shipped with maven-proxy - absolute URLs only #eg. /maven-proxy/style.css, http://www.example.com/style.css stylesheet=/maven-proxy/style.css #bgColor / bgColorHighlight are replaced in the built in stylesheet to produce a simple color scheme. #If a stylesheet is set, these are not used. bgColor=#14B bgColorHighlight=#94B #rowColor / rowColorHighlight are replaced in the built in stylesheet to produce a simple color scheme. #If a stylesheet is set, these are not used. rowColor=#CCF rowColorHighlight=#DDF ################ PROXIES #This is just a hack, it should auto discover them proxy.list=one #Unauthenticated proxy proxy.one.host=repo.test.com proxy.one.port=3128 #Authenticated proxy #proxy.two.host=proxy2.example.org #proxy.two.port=80 #proxy.two.username=username2 #proxy.two.password=password2 #Authenticated proxy #proxy.three.host=proxy3.example.net #proxy.three.port=3129 #proxy.three.username=username3 #proxy.three.password=password3 ################# REPOSITORIES #This is not just a hack, it specifies the order repositories should be checked #Note that the proxy adds a "/" which is why the urls aren't suffixed with a "/" repo.list=repo-test-com #local-store # The local store represents a location that local jars you host can be located. # This could also be achieved by having a local http repository, but this is less cumbersome repo.local-repo.url=file:///./target/repo-local repo.local-repo.description=Super Secret Custom Repository #If copy is true, jars are copied from the store to the proxy-repo. Only configurable for file:/// repos repo.local-repo.copy=false #If hardfail is true, any unexpected errors from the repository will cause #the client download to fail (typically with a 500 error) repo.local-repo.hardfail=true #Don't cache a file repository repo.local-repo.cache.period=0 repo.repo-test-com.url=http://repo.test.com/repository repo.repo-test-com.description=maven2 repo.repo-test-com.url.proxy=one repo.repo-test-com.url.cache.period=3600 repo.repo-test-com.hardfail=false repo.repo-test-com.cache.failures=true
配置文件中,url信息均是描述性信息,没有任何实际意义,这些信息会在你启动服务后,在页面上得以展示.
A)在系统中创建需要放置服务程序的路径:/usr/local/mvn-proxy
B)把maven-proxy-standalone-0.2-app.jar放在目录中,同时也把修改后的mavn-proxy.properties文件放在同一目录
C)在目录中,创建放置库文件的目录/target/repo(此文件夹名和properties中的配置信息保持一致)
D)新建一个命令文件:startup.sh
#!/bin/bash java -jar maven-proxy-standalone-0.2-app.jar maven-proxy.properties
同时给sh文件权限:chmod u+x startup.sh
到目前位置,mavn-proxy准备工作基本结束.只需实行startup.sh既可启动服务
服务的默认配置端口是9999,如果你的系统有防火墙(linux),请赦免9999端口访问..
此时你在浏览器中输入:http://你的服务器ip:9999/
既可看到maven-proxy服务的首页,通过点击repository,可以查看库的信息.
2.maven的配置
proxy已经可用,那么如何通过maven(客户端)来获取或者感知proxy中的依赖包呢??
灰常简单....一贯作风,编辑maven的setting.xml增加其proxy配置信息既可,找到profiles标签
在其尾部增加如下信息:
<profile> <id>central-repo</id> <repositories> <repository> <id>local-repo</id> <name>Internal Repository</name> <url>http://192.168.0.105:9999/repository</url> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>local-repo</id> <name>Internal Repository</name> <url>http://192.168.0.105:9999/repository</url> </pluginRepository> </pluginRepositories> </profile> </profiles>
注意请讲url修改为自己的proxy服务器地址.其他信息可以不变...
当配置完profile后,请你别忘去激活它,激活的方式有2中,一个是在项目的pom.xml配置引用激活,另一种方式就是全局激活,全局激活需要修改maven的setting.xml文件.在文件中增加
<activeProfiles> <activeProfile>central-repo</activeProfile> </activeProfiles>
activeProfile的值要和你配置的profile的id保持一致.
3.发布jar
上面我们曾经建立过一个文件路径:mven-proxy/target/repo
发布jar,需要手动在此目录中增加文件,比如我要发布一个message-client-1.1.0.jar
规范的目录结构应该是:mven-proxy/target/repo/com/test/message-client/1.1.0
然后把你的message-client-1.1.0.jar,message-client-1.1.0.pom,_maven.repositories三个问题全部放在目录中..
启动服务后,你就在页面上看到相应的jar...
程序对proxy中jar的引用和普通jar依赖一样,无须做更多的配置...