maven+svn版本控制详谈

        LZ最近在公司里开发一套公司本身的项目框架,而且封装了一些类库,采用的是maven来构建项目,每一个项目都会涉及到版本的更新,maven项目通常的升级步骤是:html

        一、经过mvn deploy命令发布当前版本到maven仓库
java

        二、将当前版本在svn服务器上打上一个标签web

        三、修改每一个maven项目的pom.xml,改变全部版本为新版本redis

        这样作固然没有任何问题,可是若是版本更新速度很快,一天甚至可能发布N个项目,那么每次都重复这样的步骤,那你离死也不远了,全部LZ想到了版本的自动管理,要作这样的工做首先须要准备工做:apache

         一、搭建maven仓库服务器

         二、本地计算机安装可以在命令行工做的svn框架

       

       三、下载mavenmaven

       四、安装maven,而且设置环境变量编辑器

       在使用过程当中,LZ也遇到了不少问题,而且网上也没有什么解决方案,为了避免让你们走不少弯路,LZ把本身遇到的一些问题提出来。svn

       咱们把maven下载后,解压到当前要使用版本管理的项目的根目录下,并改命为EMBEDDED,如图:

      

        由于当你构建的时候,maven会到该目录下去找bin/mvn.bat,文件,而后进入EMBEDDED/bin将mvn.cmd改命为mvn.bat,再用文本编辑器打开该文件,搜索“mvn.cmd”,改为"mvn.bat",如:

           

        设置环境变量:

        在path中加入:

       而后打开命令行,输入mvn -version 没有报错说明maven安装成功

      接下来,咱们来看怎么样才能实现版本自动管理,要作自动版本管理,少不了maven的一个插件:maven-release-plugin,咱们将该插件加入到plugin中:

     

<plugin>  
      <groupId>org.apache.maven.plugins</groupId>  
      <artifactId>maven-release-plugin</artifactId>  
      <version>2.5.3</version>
      <configuration>  
        <tagBase>http://192.168.1.242/svn/java/sunsharp/tags/</tagBase>
        <username>liyi</username>
  		<password>li897yi</password>
  		<releaseProfiles>release</releaseProfiles>
  		<checkModificationExcludes>
  			<checkModificationExclude>.project</checkModificationExclude>
  			<checkModificationExclude>EMBEDDED</checkModificationExclude>
  			<checkModificationExclude>release.properties</checkModificationExclude>
  			<checkModificationExclude>sunsharp-module\.project</checkModificationExclude>
  			<checkModificationExclude>sunsharp-module\sunsharp-dao\.classpath</checkModificationExclude>
  			<checkModificationExclude>sunsharp-module\sunsharp-dao\.project</checkModificationExclude>
  			<checkModificationExclude>sunsharp-module\sunsharp-dao\.settings</checkModificationExclude>
  			<checkModificationExclude>sunsharp-module\sunsharp-encrypt\.classpath</checkModificationExclude>
  			<checkModificationExclude>sunsharp-module\sunsharp-encrypt\.project</checkModificationExclude>
  			<checkModificationExclude>sunsharp-module\sunsharp-encrypt\.settings</checkModificationExclude>
  			<checkModificationExclude>sunsharp-module\sunsharp-http\.classpath</checkModificationExclude>
  			<checkModificationExclude>sunsharp-module\sunsharp-http\.project</checkModificationExclude>
  			<checkModificationExclude>sunsharp-module\sunsharp-mail\.classpath</checkModificationExclude>
  			<checkModificationExclude>sunsharp-module\sunsharp-mail\.project</checkModificationExclude>
  			<checkModificationExclude>sunsharp-module\sunsharp-redis\.classpath</checkModificationExclude>
  			<checkModificationExclude>sunsharp-module\sunsharp-redis\.project</checkModificationExclude>
  			<checkModificationExclude>sunsharp-module\sunsharp-upload\.classpath</checkModificationExclude>
  			<checkModificationExclude>sunsharp-module\sunsharp-upload\.project</checkModificationExclude>
  			<checkModificationExclude>sunsharp-module\sunsharp-utils\.classpath</checkModificationExclude>
  			<checkModificationExclude>sunsharp-module\sunsharp-utils\.project</checkModificationExclude>
  			<checkModificationExclude>sunsharp-module\sunsharp-web\.classpath</checkModificationExclude>
  			<checkModificationExclude>sunsharp-module\sunsharp-web\.project</checkModificationExclude>
  		</checkModificationExcludes>
      </configuration>  
    </plugin>

由于在构建的时候,他会提交到svn上去,全部发现有.project,.classpath都会报错,因此须要本身过滤掉,而后再配置scm:

<scm>  
      <connection>scm:svn:http://192.168.1.242/svn/java/sunsharp/sunsharp/</connection>  
      <developerConnection>scm:svn:http://192.168.1.242/svn/java/sunsharp/sunsharp/</developerConnection>
      <url>http://192.168.1.242/svn/java/sunsharp/sunsharp/</url>  
    </scm>
你要发布到maven仓库,确定须要指定maven仓库地址:

<distributionManagement>
		<repository>
			<id>releases</id>
			<name>Internal Releases</name>
			<url>
				http://192.168.1.241:8081/nexus/content/repositories/releases
			</url>
		</repository>
		<snapshotRepository>
			<id>snapshots</id>
			<name>Internal Snapshots</name>
			<url>
				http://192.168.1.241:8081/nexus/content/repositories/snapshots
			</url>
		</snapshotRepository>
	</distributionManagement>
若是采用插件自动发布,是会发布到releases中去的,若是仓库配置了登陆权限的,这里须要在settings.xml中配置,如:

<server>    
		<id>releases</id>    
		<username>admin</username>    
		<password>******</password>    
	  </server>
这样准备工做算是完成了,咱们可使用命令:

mvn release:clean 清除记录

mvn release:prepare:准备工做,他会检查是否有代码没有提交,若是没有提交会报错,若是版本号不是以SNAPSHOT结束的也会报错,在执行前须要在指定的svn tag目录下建立一个目录,命名规则是项目名-版本号,例如LZ这里就是sunsharp-1.0.0,不然他会提示找不到目录

mvn release:perform 执行,他会改变pom.xml的版本号为下一个版本,执行deploy发布到maven仓库的release中

mvn release:rollback 回滚,若是以为当前有问题能够执行回滚

具体release的用法能够百度查找,里面有不少参数设置,能够设置当前版本,下一个版本,能够设置试运行(-DtryRun=true),在测试阶段最好加上这个参数,加上后执行perform命令,不会真正的提交到仓库,等没有报错了再去掉这个参数,而后就能够真正的发布到maven仓库中了。

LZ在使用过程当中也遇到了不少问题,经过不懈努力才得以解决,各位童鞋在使用过程当中可能也会遇到一些问题,若是不懂的能够给我留言,我看到后会第一时间回复。

相关文章
相关标签/搜索