前言

    但凡一个略有规模的项目都需要一个持续集成环境的支撑,为什么需要持续集成环境,我们来看一个例子。假如一个项目,由A、B两位程序员来协作开发,A负责前端模块,B负责后端模块,前端依赖后端。A和B都习惯使用SVN作为代码管理工具,他们分别开始工作,一个功能完成后会提交到SVN,以便对方能够使用。一天B告诉A,我的某个功能完成了,提交到SVN,你更新下来试一下。A便从SVN更新下来代码,结果一试,我檫报错,于是告诉B你的代码有问题,B检查了一下确实有问题,然后B经过1个小时修改后,重新上传到SVN,A更新下来可以使用了。又过了几个小时,A发现B的代码仍然有问题,B检查了半天,没找到问题所在,后来开发组长检查他们的代码发现是两个人使用不同版本的jar包导致的,这个问题确认难以发现,A把jar包的版本弄得和B一样,A和B又开始继续工作。终于有一天项目完成了,需要发布程序,这时A从自己的开发环境上编译一个版本,把他上传到服务器上,程序运行的前几天比较稳定,后来A有点急事请假了,但服务器上的程序这时好像出现了问题,这时B只能使用自己的电脑调试、解决问题并试着发一个版本,但因为他平时做后端,对版本发布不是很熟悉,又搞了半天终于弄好了。项目上线了,稳定运行了半年,后来A、B先后离职了,新来的C负责维护这个项目,尽管A、B在离职前做了一些工作交接,但当C真正接手的时候才发现头大,我檫居然注释都没写,有没有这么坑的,由于C开始硬着头皮看代码,发现代码中各种飘逸写法,各种为了临时需求而打的补丁,C几乎抓狂。

    以上的例子有点夸大,但或多或少都可能遇到过,我们需要一种规范来约束这个开发过程,使开发过程严格有序的进行。

从问题出发

就以上面这个例子,我们看其中暴露出了哪些问题:

1、团队彼此协作不流畅,出现彼此阻塞的情况;

2、使用的类库版本不统一,造成难以估计的风险;

3、代码未经严格测试就上传,造成不断的返工;

4、版本发布规范欠缺,版本质量不能保证;

5、代码质量低,其他人难以接手。

    团队开发时,我们常常约定好彼此之间的接口规范,然后开始各自开发,开发完成后,按照接口规范对接起来,这是一个理想的流程,但现实往往不那么顺利,问题常常在于,接口规范可能根据需求变化而经常变更,而且在实际对接时经常发现诸多问题。我们无法通过工具来解决这些问题,只能要求开发人员在制定接口规范时,为可能变更的需求多一些扩展,在开发时多考虑诸多素质,尽量将减少对接过程中的问题。下面将讲述如何使用Maven和Nexus保证开发人员引用统一的类库版本,以及如何将自己开发的模块上传至Nexus服务器,以提供给其他模块引用。

关于Maven和Nexus的搭建在Maven实战(Maven+Nexus建立私服【Linux系统】)中已经介绍过,

Nexus Repository Manager - Tutorial

1. Nexus Repository Manager

1.1. What is a Repository Manager

An repository manager allows to store and retrieve build artifacts. The most popular examples for repository manager are Maven Central Repository and jcenter at Bintray, which you can use to retrieve your dependencies for a Maven build.

1.2. What is Nexus?

A Nexus installation brings you such a repository for your company. So you can host your own repositories, but also use Nexus as a proxy for public repositories. With such a proxy the time to receive an artifact is reduced and it saves bandwidth. Nexus allows you to host your private build artifacts. Nexus is avaialable as commercial and Open Source distribution.

2. Installation of Nexus

In order to install the open source version of Nexus you need to visit Nexus OSS and download the TGZ version or the ZIP version. Currently the latest version is 3.13.0.

To start Nexus, after extracting, the nexus script must be executed with a start parameter in the nexus folder:

cd /NEXUS_FOLDER/nexus ./bin/nexus start

And in case you want to stop Nexus you just have to write stop instead of start:

cd /NEXUS_FOLDER/nexus ./bin/nexus stop

3. Configuration of Nexus

Once the nexus repository manager has been started, its web interface can be accessed under this URL:

http://localhost:8081/nexus/

The Nexus contains some repositories by default:

3.1. User Settings

The default user is admin and the password is admin123.

The login can be found in the top right corner.

After logging in the credentials can be changed in the profile settings.

4. Creating a repository

We want to have a separate repository for our p2 artifacts. This can be created like this:

  • Login to the nexus

    http://localhost:8081/nexus/

    with User admin and password admin123.
  • Click on Repositories on the right hand side.
  • Select Add..  Hosted Repository and use the following data:

Now you got a custom repository, which is hosted on your local nexus installation.

5. P2 Nexus Plugins

With the Nexus P2 Bridge Plugin and the Nexus P2 Repository Plugin it is possible to create a proxy repository for p2 update sites. Use the Search Maven to find these plugins.

5.1. Installing p2 Plugins

Download the bundle.zip of your search result.

These two downloaded bundle.zip files, which contain the nexus plugins have to be extracted into the sonatype-work/nexus/plugin-repository folder of the Nexus installation.

The nexus server has to be restarted to install the plugins.

./bin/nexus restart

To validate that these plugins are properly installed the plugin console can be used.

5.2. Creating a proxy for p2 update sites

In the repositories tab a Proxy Repository can be created.

Repository ID and a Repository Name has to be specifed. (1)

The previously installed p2 plugins offer additional providers like p2. (2)

The Remote Storage Location is the p2 update site url, e.g., http://download.eclipse.org/eclipse/updates/4.6milestones.

Finally this must be saved and then the new proxy will be created.

After settings this up, a target definition can make use of this proxy, but using the given URL:

http://localhost:8081/nexus/content/repositories/Eclipse-Neon-Milestones/

6. Tycho/Nexus Unzip Plugin

The Tycho/Nexus Unzip Plugin can be used to create virtual repositories for unzipped versions of zipped p2 update site.

6.1. Installing the Tycho/Nexus Unzip Plugin

Download the bundle.zip of the Tycho/Nexus Unzip Plugin

The downloaded bundle.zip file, which contains the nexus plugins has to be extracted into the sonatype-work/nexus/plugin-repository folder of the Nexus installation.

The nexus server has to be restarted to install the plugins. ./bin/nexus restart

To validate that these plugins are properly installed the plugin console can be used.

6.2. Setting up an unzip repository

See Tycho/Nexus Unzip Plugin for information how to setup the unzip plug-in.

 

7. Nexus online resources

Nexus Book

Nexus P2 Plugins

Tycho/Nexus Unzip Plugin

https://www.vogella.com/tutorials/Nexus/article.html

Nexus 的下载和部署

 

 

 

 

 

 

Nexus 的下载和部署

 

1. Nexus 的下载

下载的地址: https://www.sonatype.com/download-oss-sonatype

2. Nexus 解压和配置

Windows:

配置环境变量:

启动服务:

## 3. Nexus 的网页配置

默认:
    用户:admin
    密码:admin123

<!-- maven setting.xml 中配置 nexus 的权限 -->
	<server>
		<id>releases</id>
		<username>admin</username>
		<password>admin123</password>
	</server>
	<server>
		<id>snapshots</id>
		<username>admin</username>
		<password>admin123</password>
	</server>

	<!-- maven setting.xml 中配置 tomcat 的权限 -->
	
	<server>
	  	<id>tomcat9x</id>
	  	<username>admin</username>
	  	<password>admin123</password>
	</server>
	

	<!--配置 pom 将 jar 包可以上传到 nexus 仓库中-->
	<distributionManagement>
	    <repository>
	        <id>releases</id>
	        <url>http://localhost:8081/repository/maven-releases/</url>
	    </repository>
	    <snapshotRepository>
	        <id>snapshots</id>
	        <url>http://localhost:8081/repository/maven-snapshots/</url>
	    </snapshotRepository>
	</distributionManagement>
	
	
	<!-- 配置nexus仓库地址 从远程仓库下载jar包-->
	<profiles>
		<profile>
			<id>mynexus</id>
			<repositories>
				<repository>
					<id>nexus</id>
					<name>Nexus Repository</name>
					<url>http://localhost:8081/repository/maven-public/</url>
					<releases>
						<enabled>true</enabled>
					</releases>
					<snapshots>
						<enabled>true</enabled>
					</snapshots>
				</repository>
			</repositories>
		</profile>
	</profiles>
	
	<!--激活profiles--

3. 重要文件内容

下面链接是 settings.xml 文件内容

下面链接是 pom.xml 文件内容

下面链接是 tomcat-users.xml 文件添加内容

4. 查看 Nexus

https://www.pianshen.com/article/1840322822/

Maven教程-使用Nexus搭建私服

前言

使用Maven可以对项目的各种依赖进行统一管理,在pom文件中定以好依赖,就可以从Maven中央库或者第三方库中下载到本地。但在企业内部使用也会遇到一些问题,每个使用者都需要去下载相应的依赖包或者插件,效率低下,浪费带宽,所以搭建企业内部的私服就很有必要。

搭建私服后,所有的依赖就可以从私服下载,私服会自动判定,如果私服库里没有这个资源,则私服会自动去网上下载,如果私服已经包含所需资源,则可以通过内网提供给使用者,大大提高工作效率。

Nexus是Maven常用的私服,安装使用都还算方便,用于搭建企业内部的maven私服。

准备工作

下载JDK:http://download.oracle.com/otn-pub/java/jdk/8u74-b02/jdk-8u74-linux-x64.tar.gz?AuthParam=1457320414_0a8f6e90d7f0837453418473460f3742 
下载Maven:http://apache.fayea.com/maven/maven-3/3.3.9/binaries/apache-maven-3.3.9-bin.tar.gz 
下载Nexus:http://www.sonatype.org/nexus/go,Nexus所有版本下载地址: 
http://www.sonatype.org/nexus/archived/

安装步骤

1、安装JDK 
解压缩

> tar xzf jdk-8u74-linux-x64.gz

配置环境变量

> vi /etc/profile

在文件最后一行追加:

export JAVA_HOME=/home/jdk/jdk1.8.0_74

export PATH=$PATH:$JAVA_HOME/bin

让系统重新加载/etc/profile,命令:

> source /etc/profile

验证是否安装成功

> java -version

出现如下界面表示安装成功了

Java version “1.8.0_74” 
Java(TM) SE Runtime Environment (build 1.8.0_74-b02) 
Java HotSpot(TM) 64-Bit Server VM (build 25.74-b02, mixed mode)

2、安装Maven 
解压文件

> tar -zvxf apache-maven-3.3.9-bin.tar.gz

配置环境变量

> vi /etc/profile

最近文件最后追加如下两行:

export MAVEN_HOME=/home/maven/apache-maven-3.3.9 

export PATH=$PATH:$MAVEN_HOME/bin

让系统重新加载/etc/profile,命令:

> source /etc/profile

验证是否安装成功

mvn -version

3、安装Nexus 
解压文件

tar xzf nexus-2.12.0-01-bundle.tar.gz

解压后的目录结构如下图所示:

使用Maven+Nexus+Jenkins+Svn+Tomcat+Sonar搭建持续集成环境_上传

上图中的 nexus-2.12.0-01和sonatype-work目录就是解压tar包后的两个目录,nexus-2.12.0-01中是nexus的核心文件,sonatype-work,会将下载来的开发包放置在其中。

启动Nexus

[root@w157-bigdata-Maven nexus-2.12.0-01]# ./bin/nexus

出现如下警告:

[root@w157-bigdata-Maven nexus-2.12.0-01]# ./bin/nexus 

Usage: ./bin/nexus { console | start | stop | restart | status | dump } 

[root@w157-bigdata-Maven nexus-2.12.0-01]# ./bin/nexus start 

**************************************** 

WARNING - NOT RECOMMENDED TO RUN AS ROOT 

**************************************** 

If you insist running as root, then set the environment variable RUN_AS_USER=root before running this script.

默认情况下,不建议以root用户运行Nexus,我们可以修改bin/nexus中的配置跳过警告

vi bin/nexus

使用Maven+Nexus+Jenkins+Svn+Tomcat+Sonar搭建持续集成环境_maven_02

修改完成后保存退出即可。

 

[root@w157-bigdata-Maven nexus-2.12.0-01]# ./bin/nexus start 

**************************************** 

WARNING - NOT RECOMMENDED TO RUN AS ROOT 

**************************************** 

Starting Nexus OSS... 

Started Nexus OSS. 

[root@w157-bigdata-Maven nexus-2.12.0-01]#

另外,默认端口为8081,如需修改请查看配置文件 conf/nexus.properties

配置Nexus

启动Nexus

[root@w157-bigdata-Maven nexus-2.12.0-01]# ./bin/nexus start 

**************************************** 

WARNING - NOT RECOMMENDED TO RUN AS ROOT 

**************************************** 

Starting Nexus OSS... 

Started Nexus OSS. 

[root@w157-bigdata-Maven nexus-2.12.0-01]#

访问网址:http://172.18.19.157:8081/nexus 


使用Maven+Nexus+Jenkins+Svn+Tomcat+Sonar搭建持续集成环境_上传_03

点击右上角的 Log In 按钮即可登陆了。默认登录账号/密码为: admin/admin123 ,登陆成功后的界面

使用Maven+Nexus+Jenkins+Svn+Tomcat+Sonar搭建持续集成环境_上传_04

将列表中所有Type为proxy 的项目的 Configuration 中的 Download Remote Indexes 设置为True

使用Maven+Nexus+Jenkins+Svn+Tomcat+Sonar搭建持续集成环境_上传_05



将Releases仓库的Deployment Policy设置为 Allow ReDeploy 

使用Maven+Nexus+Jenkins+Svn+Tomcat+Sonar搭建持续集成环境_上传_06

当然我们也避免不了会使用到一些第三方的 jar ,而这些jar包也不存在于互联网上的maven中央仓库中,这时我们可以手工添加jar 到我们的私服中。 

添加第三方 jar 如下: 

使用Maven+Nexus+Jenkins+Svn+Tomcat+Sonar搭建持续集成环境_maven_07

填写完必选字段,点击Upload Artifact(s)按钮即可。

如果需要删除的话,如下: 

使用Maven+Nexus+Jenkins+Svn+Tomcat+Sonar搭建持续集成环境_bundle_08

本地项目配置引用私服

一. 自动发布构件到远程仓库 
1. 在工程的pom.xml中添加:

<distributionManagement> 
  

<repository> 

<id>releases</id><!--这个ID需要与你的release仓库的Repository ID一致--> 

<url>http://172.18.19.157:8081/nexus/content/repositories/releases</url> 

</repository> 

<snapshotRepository> 

<id>snapshots</id><!--这个ID需要与你的snapshots仓库的Repository ID一致--> 

<url>http://172.18.19.157:8081/nexus/content/repositories/snapshots</url> 

</snapshotRepository> 
  

</distributionManagement>

2.修改$MAVEN_HOME\conf目录下的settings.xml配置文件,添加如下配置:

<servers> 

<server> 

<id>releases</id> 

<username>admin</username> 

<password>dev123</password> 

</server> 

<server> 

<id>snapshots</id> 

<username>admin</username> 

<password>dev123</password> 

</server> 

</servers>

3.打开windows命令行,在工程所在目录下执行

mvn deploy

所部署的包就自动上传到了nexus安装目录下的 

使用Maven+Nexus+Jenkins+Svn+Tomcat+Sonar搭建持续集成环境_bundle_09

二.配置Maven从Nexus下载构件 
1.在POM中配置Nexus私服,这样的配置只对当前的Maven项目有效。

<repositories> 

<repository> 

<id>public</id> 

<name>Team Maven Repository</name> 

<url>http://172.18.19.157:8081/nexus/content/groups/public/</url> 

<releases> 

<enabled>true</enabled> 

</releases> 

<snapshots> 

<enabled>true</enabled> 

</snapshots> 

</repository> 

</repositories>

2.在settings.xml中配置profile元素,这样就能让本机所有的Maven项目都使用自己的Maven私服。

 

在此介绍下主要步骤:

1、下载maven(读:妹吻)和nexus(读:耐克色儿丝):

http://maven.apache.org/download.cgi

http://nexus.sonatype.org/downloads/

2、将下载的nexus放置到linux服务器解压并启动:

 

cd /data/program/nexus-2.3.1-01/bin/jswll

使用Maven+Nexus+Jenkins+Svn+Tomcat+Sonar搭建持续集成环境_maven_10

选择自己的系统并进入:

使用Maven+Nexus+Jenkins+Svn+Tomcat+Sonar搭建持续集成环境_上传_11

启动nexus

./nexus start

使用Maven+Nexus+Jenkins+Svn+Tomcat+Sonar搭建持续集成环境_bundle_12

nexus启动成功,访问:http://192.168.6.204:8081/nexus/

使用Maven+Nexus+Jenkins+Svn+Tomcat+Sonar搭建持续集成环境_maven_13

3、将下载的maven解压放置在开发机上(windows),并配置conf目录下的setting.xml文件:

(1)在<settings><profiles></profiles></settings>节点之间添加配置:

<profile> <id>dev</id> <repositories> <repository> <id>local-nexus</id> <url>http://192.168.6.204:8081/nexus/content/groups/public/</url> <releases><enabled>true</enabled> </releases> <snapshots> <enabled>true</enabled> </snapshots> </repository> </repositories> </profile>

接着在<settings></settings>节点之间添加配置:

<activeProfiles> <activeProfile>dev</activeProfile> </activeProfiles>

以上配置好了连接Nexus私服。

(2)安装eclipse的m2eclipse插件,并配置maven。

m2eclipse的安装地址:http://m2eclipse.sonatype.org/sites/m2e(安装过程略)

安装完成后配置maven:

使用Maven+Nexus+Jenkins+Svn+Tomcat+Sonar搭建持续集成环境_bundle_14

使用Maven+Nexus+Jenkins+Svn+Tomcat+Sonar搭建持续集成环境_bundle_15

配置eclipse的maven为本地下载的maven地址。主要为了方便配置和版本管理。

至此,nexus和maven就搭建配置完成了,下面我们要利用这个环境解决两个问题:

1、不同的模块项目都使用统一的类库版本;如spring都使用3.1.2.RELEASE,log4j都使用1.2.17。这样避免因为jar包版本的不同,引发的诸多问题。

2、模块开发完毕后,将编译的库文件发布到nexus私服上,以便其他模块引用。

这里说一下maven的一个特性,继承。简单来说继承就是先建立一个父项目,其他项目如果继承这个父项目,就可以继承这个项目定义的库文件及版本。利用这个特性,我们可以让多个模块都继承一个父项目,而在父项目中定义好类库的版本,这样所有模块都使用统一的类库文件。例如:

建立名为maven-parent的父项目,pom配置为:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.cnblogs.leefreeman</groupId>
    <artifactId>maven-parent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>pom</packaging>
    <properties>
        <!-- junit -->
        <junit.version>4.10</junit.version>
        <!-- servlet -->
        <servlet-api.version>2.5 </servlet-api.version>
        <!-- log4j -->
        <log4j.version>1.2.17 </log4j.version>
        <!-- commons.codec -->
        <commons.codec.version>1.7</commons.codec.version>
        <!-- maven-support -->
        <maven-support.version>0.0.1-SNAPSHOT</maven-support.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <dependencyManagement>
        <dependencies>
            <!-- junit -->
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>${junit.version}</version>
                <scope>test</scope>
            </dependency>
            <!-- log4j -->
            <dependency>
                <groupId>log4j</groupId>
                <artifactId>log4j</artifactId>
                <version>${log4j.version}</version>
            </dependency>
            <!-- commons包 -->
            <dependency>
                <groupId>commons-codec</groupId>
                <artifactId>commons-codec</artifactId>
                <version>${commons.codec.version}</version>
            </dependency>
            <!-- servlet -->
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>servlet-api</artifactId>
                <version>${servlet-api.version}</version>
                <scope>provided</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
</project>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

这个pom定义了一些类库以及它的版本。

然后再建立模块项目:maven-sample(web项目)、maven-support(支撑模块,包括一些工具类以及业务封装,提供给maven-sample引用)。

maven-sample的pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.cnblogs.leefreeman</groupId>
        <artifactId>maven-parent</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <artifactId>maven-sample</artifactId>
    <packaging>war</packaging>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
        </dependency>
        <dependency>
            <groupId>commons-codec</groupId>
            <artifactId>commons-codec</artifactId>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
        </dependency>
    </dependencies>
    <build>
        <finalName>maven-sample</finalName>
    </build>
</project>
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

maven-support的pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.cnblogs.leefreeman</groupId>
        <artifactId>maven-parent</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <artifactId>maven-support</artifactId>
    <packaging>jar</packaging>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
        </dependency>
        <dependency>
            <groupId>commons-codec</groupId>
            <artifactId>commons-codec</artifactId>
        </dependency>
    </dependencies>
</project>
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

通过以上配置,maven-sample和maven-support引用的jar包,都会使用相同的版本:

使用Maven+Nexus+Jenkins+Svn+Tomcat+Sonar搭建持续集成环境_maven_16

接下来我们解决协作开发的问题,maven-support开发完毕之后,将之发布到nexus服务器,以便maven-sample可以引用。我们需要3步:

1、本地maven的conf目录下的setting.xml,在<servers></servers>节点之间添加:

<server>
<id>releases</id>
<username>admin</username>
<password>admin123</password>
</server>
<server>
<id>snapshots</id>
<username>admin</username>
<password>admin123</password>
</server>

2、在maven-support项目的pom.xml中,<project></project>节点之间添加:

<!-- 构件部署仓库 -->
    <distributionManagement>
        <repository>
            <id>releases</id>
            <name>发布版本仓库</name>
            <url>http://192.168.0.109:8081/nexus/content/repositories/releases/</url>
        </repository>
        <snapshotRepository>
            <id>snapshots</id>
            <name>快照版本仓库</name>
            <url>http://192.168.0.109:8081/nexus/content/repositories/snapshots/</url>
        </snapshotRepository>
    </distributionManagement>
<distributionManagement>

3、对maven-support项目进行编译,并执行发布命令:

使用Maven+Nexus+Jenkins+Svn+Tomcat+Sonar搭建持续集成环境_上传_17

使用Maven+Nexus+Jenkins+Svn+Tomcat+Sonar搭建持续集成环境_上传_18

可以看到maven-support已经成功发布到nexus私服中:

使用Maven+Nexus+Jenkins+Svn+Tomcat+Sonar搭建持续集成环境_上传_19

使用Maven+Nexus+Jenkins+Svn+Tomcat+Sonar搭建持续集成环境_bundle_20

这样maven-sample就可以引用maven-support模块,而开发maven-support的人则可以继续开发,持续发布新版本。

使用Maven+Nexus+Jenkins+Svn+Tomcat+Sonar搭建持续集成环境_bundle_21

小结

    本文主要介绍maven和nexus的环境搭建,以及怎么使用maven和nexus统一管理库文件和版本,怎么讲自己的模块上传至nexus私服,供其他模块引用。这样我们基本解决了两个问题:

1、团队彼此协作不流畅,出现彼此阻塞的情况;

2、使用的类库版本不统一,造成难以估计的风险;

下一篇将使用jenkins、svn、tomcat以及sonar解决自动化测试、自动化版本发布、代码质量检查等问题。

前言

    上一篇随笔Maven+Nexus+Jenkins+Svn+Tomcat+Sonar搭建持续集成环境(一)介绍maven和nexus的环境搭建,以及如何使用maven和nexus统一管理库文件和版本,以及怎么将自己的模块上传至nexus私服,供其他模块引用。下面将主要介绍如何利用Jenkins、SVN、Tomcat以及Sonar来完成项目的自动化编译、测试和发布,以及检查项目的代码质量。

利器Jenkins

    Jenkins源于另外一个持续集成工具Hudson,Hudson在oracle收购sun之前,是开源社区在开发和维护,oracle收购sun之后声明拥有hudson的商标使用权,后来开发hudson的开源力量只能重新起个名字“Jenkins”来继续开发,原Hudson则由oracle持续维护,但更新速度比较慢。

jenkins地址:http://jenkins-ci.org/

hudson地址:http://hudson-ci.org/

可以看到hudson官网右下角oracle的版权logo。我们这里就使用Jenkins来搭建持续集成环境。

首先从官方上下载jenkins,http://mirrors.jenkins-ci.org/war/ 可以选择不同的版本。下载完成后放置在服务器某个目录下,cd到该目录下,执行:

java -jar jenkins.war

jenkins内置一个jetty容器,默认使用8080端口,如果你的8080被占用了,请执行:

java -jar jenkins.war --httpPort=8081

 

当然你也可以直接把war包放到tomcat目录下启动,这样jenkins就启动起来了。

使用Maven+Nexus+Jenkins+Svn+Tomcat+Sonar搭建持续集成环境_maven_22

来到这个界面我们先做一个事情:系统配置,主要是配置maven和发布插件。

1、进入“系统管理”——>“系统设置”:

新增maven,将linux上安装好的maven配置上去。

使用Maven+Nexus+Jenkins+Svn+Tomcat+Sonar搭建持续集成环境_bundle_23

其他保持默认配置,保存!

2、进入“系统管理”——>“管理插件”——>“高级”上传插件

使用Maven+Nexus+Jenkins+Svn+Tomcat+Sonar搭建持续集成环境_maven_24

上传完成之后,jenkins就具备了发布web应用到tomcat各类容器的能力。

注:deploy.hpi插件是我事先从网上下载好了,你也可以使用jenkins自带的插件安装功能完成插件安装,但那个比较慢。

3、配置linux服务器上的maven,跟上篇类似你也需要配置linux服务器上的maven,使之连接到nexus私服,以及支持发布模块到nexus上。

在<settings><servers></servers></settings>节点之间添加:

  1:    <server>  
   2:      <id>snapshots</id>  
   3:      <username>deployment</username>  
   4:      <password>123456</password>  
   5:    </server>  
   6:      <server>  
   7:      <id>releases</id>  
   8:      <username>deployment</username>  
   9:      <password>123456</password>  
  10:    </server>
2:      <id>snapshots</id>

在<settings></settings>节点之间添加:

 1:  <profiles>
   2:          <profile>
   3:              <id>dev</id>
   4:              <repositories>
   5:                  <repository>
   6:                      <id>local-nexus</id>
   7:                      <url>http://192.168.0.109:8081/nexus/content/groups/public/</url>
   8:                      <releases>
   9:                          <enabled>true</enabled>
  10:                      </releases>
  11:                      <snapshots>
  12:                          <enabled>true</enabled>
  13:                      </snapshots>
  14:                  </repository>
  15:              </repositories>
  16:          </profile>
  17:      </profiles>
  18:   
  19:      <!-- activeProfiles | List of profiles that are active for all builds. | -->
  20:      <activeProfiles>
  21:          <activeProfile>dev</activeProfile>
  22:      </activeProfiles>
2:          <profile>

 

完成这些配置,我们就可以在jenkins上新建项目了。

我们事先在eclipse中开发的三个项目:maven-parent(父项目,用于统一类库版本)、maven-support(后端模块,支撑web项目)、maven-sample(web项目,依赖maven-support),依次来添加:

使用Maven+Nexus+Jenkins+Svn+Tomcat+Sonar搭建持续集成环境_上传_25

下一步进入后,主要配置两个地址,svn和maven命令。

使用Maven+Nexus+Jenkins+Svn+Tomcat+Sonar搭建持续集成环境_maven_26

根据提示进行SVN用户名、密码认证即可。

使用Maven+Nexus+Jenkins+Svn+Tomcat+Sonar搭建持续集成环境_maven_27

对于maven-parent项目,我们执行install命令即可。保存!

接着创建maven-support项目,由于该项目是后端模块,需要发布到nexus服务器上,所以稍有不同:

使用Maven+Nexus+Jenkins+Svn+Tomcat+Sonar搭建持续集成环境_上传_28

这里需要执行deploy命令,将该项目发布到nexus上。另外你最好配置该项目构建在maven-paremt执行之后开始:

使用Maven+Nexus+Jenkins+Svn+Tomcat+Sonar搭建持续集成环境_bundle_29

最好保存!

最好创建maven-sample项目,这是个web项目,配置又稍有不同:

使用Maven+Nexus+Jenkins+Svn+Tomcat+Sonar搭建持续集成环境_maven_30

执行package命令将项目打包成war包。下面做最重要的事情:将打包后的war包自动发布到tomcat下:

使用Maven+Nexus+Jenkins+Svn+Tomcat+Sonar搭建持续集成环境_上传_31

注意几点:

1、WAR/EAR files,一定要填写相对地址;

2、Manager username、Manager password是你安装的tomcat用户名、密码,如果没有请通过tomcat的配置文件tomcat-users.xml设置。

3、tomcat url为你安装的tomcat访问地址。

最后还是设置下构建顺序,让它在maven-support执行完成之后构建:

使用Maven+Nexus+Jenkins+Svn+Tomcat+Sonar搭建持续集成环境_上传_32

保存!

jenkins首页上就出现了三个我们创建好的项目,我们只需要构建maven-parent就可以了,因为我们设置了构建顺序:

使用Maven+Nexus+Jenkins+Svn+Tomcat+Sonar搭建持续集成环境_maven_33

查看构建信息,可以看到构建的进程,项目先从svn更新下来,然后进行编译,运行测试用例,打包,发布的过程。整个过程都由jenkins完成,

使用Maven+Nexus+Jenkins+Svn+Tomcat+Sonar搭建持续集成环境_上传_34

使用Maven+Nexus+Jenkins+Svn+Tomcat+Sonar搭建持续集成环境_maven_35

至此jenkins就实现了自动化编译、测试(自己写测试用例)、打包发布、部署。

使用Maven+Nexus+Jenkins+Svn+Tomcat+Sonar搭建持续集成环境_bundle_36

可以看到tomcat上已经多了maven-sample应用。

使用Maven+Nexus+Jenkins+Svn+Tomcat+Sonar搭建持续集成环境_bundle_37

部署Sonar

http://www.sonarqube.org/官网上下载sonar安装包,解压到系统目下,进入到bin目录下,选择你的系统版本,进入后执行:

./sonar.sh start

 

sonar就启动起来了,默认的端口是9000,你也可以到conf/sonar.properties配置文件中修改:

sonar.web.port:                           8066

 

使用Maven+Nexus+Jenkins+Svn+Tomcat+Sonar搭建持续集成环境_上传_38

在Jenkins安装sonar插件

和安装deploy插件方法类似,我们在系统管理——插件管理中,上传sonar插件。安装完成之后,请重启jenkins。

使用Maven+Nexus+Jenkins+Svn+Tomcat+Sonar搭建持续集成环境_上传_39

配置jenkins的sonar插件:

进入系统配置

使用Maven+Nexus+Jenkins+Svn+Tomcat+Sonar搭建持续集成环境_上传_40

根据你的情况配置sonar,我这里仅仅修改过sonar的应用端口,所以只需要配置这个,其他保留默认值。

进入特定项目配置sonar:

使用Maven+Nexus+Jenkins+Svn+Tomcat+Sonar搭建持续集成环境_maven_41

使用Maven+Nexus+Jenkins+Svn+Tomcat+Sonar搭建持续集成环境_上传_42

选择下maven版本,其他保留默认值。保存!

然后开始构建项目,构建完成后,可以看到该项目的代码检查结果已经被导入到sonar平台上,通过sonar系统就可以方便查看该项目的代码质量情况:

sonar可以检查代码的复杂度、代码重复、单元测试覆盖率、是否有注释、潜在bug等代码问题,对于sonar的项目介绍,可以进一步去研究摸索,在这里仅介绍jenkins和sonar整合的方法,可以再实际工作时进一步研究。

使用Maven+Nexus+Jenkins+Svn+Tomcat+Sonar搭建持续集成环境_maven_43

使用Maven+Nexus+Jenkins+Svn+Tomcat+Sonar搭建持续集成环境_上传_44

小结

    本文主要介绍如何搭建jenkins以及怎么利用maven、deploy插件打包、发布你的项目,最后使用jenkins和sonar配合进行项目的代码质量检查。完成整个过程我们可以发现,以往的测试、打包、发布、代码检查的工作都可以由人工转化工具来完成,让程序员的宝贵时间集中在开发上。回过头看Maven+Nexus+Jenkins+Svn+Tomcat+Sonar搭建持续集成环境(一)开头的故事,我们可能会有不同的想法。

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6
标签: Tomcat