jenkins+nexus+maven+svn配置集成开发环境

之前一直没搞过这种集成开发环境,最近抽空玩了一下,现在做个记录,方便以后自己查看。

参考资料:(未完全列出)

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

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

http://www.cnblogs.com/helenMemery/p/6646978.html

一  准备工作

开发环境:IDEA+MAVEN+SVN

集成环境:Jenkins+Nexus

二  具体配置过程

idea创建maven项目以及配置svn就不赘述了,直接进入配置。

配置过程这里分成两个部分:即两种发布方式,ide发布和jenkins发布

第一个部分为不配置jenkins的情况下,即单纯通过idea发布项目到nexus下。

第二部分为通过jenkins集成,自动构建发布到nexus下

第二种发布方式会通用第一部分中的相关配置,下面会依次按照全部的步骤来写,不再单独区分哪些是第一种方式,哪些是第二种方式

1、给idea配置maven

idea默认集成了maven3,默认的maven配置文件(setting.xml)位于C:Users【你自己的用户名】.m2下

如需更改自定义的setting.xml文件,只需要把后面的override打钩即可。本地仓库为setting.xml中配置的localRepository

setting.xml  (maven配置文件)

<?xml version="1.0" encoding="UTF-8"?>

<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements.  See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.  The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License.  You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied.  See the License for the
specific language governing permissions and limitations
under the License.
-->

<!--
 | This is the configuration file for Maven. It can be specified at two levels:
 |
 |  1. User Level. This settings.xml file provides configuration for a single user,
 |                 and is normally provided in ${user.home}/.m2/settings.xml.
 |
 |                 NOTE: This location can be overridden with the CLI option:
 |
 |                 -s /path/to/user/settings.xml
 |
 |  2. Global Level. This settings.xml file provides configuration for all Maven
 |                 users on a machine (assuming they're all using the same Maven
 |                 installation). It's normally provided in
 |                 ${maven.home}/conf/settings.xml.
 |
 |                 NOTE: This location can be overridden with the CLI option:
 |
 |                 -gs /path/to/global/settings.xml
 |
 | The sections in this sample file are intended to give you a running start at
 | getting the most out of your Maven installation. Where appropriate, the default
 | values (values used when the setting is not specified) are provided.
 |
 |-->
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
  <!-- localRepository
   | The path to the local repository maven will use to store artifacts.
   |
   | Default: ${user.home}/.m2/repository
  <localRepository>/path/to/local/repo</localRepository>
  -->
  <!--本地仓库,即本地jar保存的位置-->
<localRepository>E:/mymaven/rep</localRepository> <!-- interactiveMode | This will determine whether maven prompts you when it needs input. If set to false, | maven will use a sensible default value, perhaps based on some other setting, for | the parameter in question. | | Default: true <interactiveMode>true</interactiveMode> --> <!-- offline | Determines whether maven should attempt to connect to the network when executing a build. | This will have an effect on artifact downloads, artifact deployment, and others. | | Default: false <offline>false</offline> --> <!-- pluginGroups | This is a list of additional group identifiers that will be searched when resolving plugins by their prefix, i.e. | when invoking a command line like "mvn prefix:goal". Maven will automatically add the group identifiers | "org.apache.maven.plugins" and "org.codehaus.mojo" if these are not already contained in the list. |--> <pluginGroups> <!-- pluginGroup | Specifies a further group identifier to use for plugin lookup. <pluginGroup>com.your.plugins</pluginGroup> --> </pluginGroups> <!-- proxies | This is a list of proxies which can be used on this machine to connect to the network. | Unless otherwise specified (by system property or command-line switch), the first proxy | specification in this list marked as active will be used. |--> <proxies> <!-- proxy | Specification for one proxy, to be used in connecting to the network. | <proxy> <id>optional</id> <active>true</active> <protocol>http</protocol> <username>proxyuser</username> <password>proxypass</password> <host>proxy.host.net</host> <port>80</port> <nonProxyHosts>local.net|some.host.com</nonProxyHosts> </proxy> --> </proxies> <!-- servers | This is a list of authentication profiles, keyed by the server-id used within the system. | Authentication profiles can be used whenever maven must make a connection to a remote server. |--> <servers>
  <!--server信息,用于打包发布到nexus,即nexus用户名密码,这里配置了两个,分别对应releases正式发布版本和snapshots快照版本,id对应于项目pom.xml中配置的distributionManagement仓库id-->  
<server> <id>releases</id> <username>admin</username> <password>admin123</password> </server> <server> <id>snapshots</id> <username>admin</username> <password>admin123</password> </server> <!-- server | Specifies the authentication information to use when connecting to a particular server, identified by | a unique name within the system (referred to by the 'id' attribute below). | | NOTE: You should either specify username/password OR privateKey/passphrase, since these pairings are | used together. | <server> <id>deploymentRepo</id> <username>repouser</username> <password>repopwd</password> </server> --> <!-- Another sample, using keys to authenticate. <server> <id>siteServer</id> <privateKey>/path/to/private/key</privateKey> <passphrase>optional; leave empty if not used.</passphrase> </server> --> </servers> <!-- mirrors | This is a list of mirrors to be used in downloading artifacts from remote repositories. | | It works like this: a POM may declare a repository to use in resolving certain artifacts. | However, this repository may have problems with heavy traffic at times, so people have mirrored | it to several places. | | That repository definition will have a unique id, so we can create a mirror reference for that | repository, to be used as an alternate download site. The mirror site will be the preferred | server for that repository. |--> <mirrors> <!-- mirror | Specifies a repository mirror site to use instead of a given repository. The repository that | this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used | for inheritance and direct lookup purposes, and must be unique across the set of mirrors. | <mirror> <id>mirrorId</id> <mirrorOf>repositoryId</mirrorOf> <name>Human Readable Name for this Mirror.</name> <url>http://my.repository.com/repo/path</url> </mirror> -->
  <!--镜像地址,即所有的jar通过nexus私服下载到本地-->
<mirror> <id>central</id> <mirrorOf>*</mirrorOf> <name>Human Readable Name for this Mirror.</name> <url>http://192.168.0.128:8081/nexus/content/groups/public/</url> </mirror> </mirrors> <!-- profiles | This is a list of profiles which can be activated in a variety of ways, and which can modify | the build process. Profiles provided in the settings.xml are intended to provide local machine- | specific paths and repository locations which allow the build to work in the local environment. | | For example, if you have an integration testing plugin - like cactus - that needs to know where | your Tomcat instance is installed, you can provide a variable here such that the variable is | dereferenced during the build process to configure the cactus plugin. | | As noted above, profiles can be activated in a variety of ways. One way - the activeProfiles | section of this document (settings.xml) - will be discussed later. Another way essentially | relies on the detection of a system property, either matching a particular value for the property, | or merely testing its existence. Profiles can also be activated by JDK version prefix, where a | value of '1.4' might activate a profile when the build is executed on a JDK version of '1.4.2_07'. | Finally, the list of active profiles can be specified directly from the command line. | | NOTE: For profiles defined in the settings.xml, you are restricted to specifying only artifact | repositories, plugin repositories, and free-form properties to be used as configuration | variables for plugins in the POM. | |--> <profiles> <!-- profile | Specifies a set of introductions to the build process, to be activated using one or more of the | mechanisms described above. For inheritance purposes, and to activate profiles via <activatedProfiles/> | or the command line, profiles have to have an ID that is unique. | | An encouraged best practice for profile identification is to use a consistent naming convention | for profiles, such as 'env-dev', 'env-test', 'env-production', 'user-jdcasey', 'user-brett', etc. | This will make it more intuitive to understand what the set of introduced profiles is attempting | to accomplish, particularly when you only have a list of profile id's for debug. | | This profile example uses the JDK version to trigger activation, and provides a JDK-specific repo. <profile> <id>jdk-1.4</id> <activation> <jdk>1.4</jdk> </activation> <repositories> <repository> <id>jdk14</id> <name>Repository for JDK 1.4 builds</name> <url>http://www.myhost.com/maven/jdk14</url> <layout>default</layout> <snapshotPolicy>always</snapshotPolicy> </repository> </repositories> </profile> --> <!-- | Here is another profile, activated by the system property 'target-env' with a value of 'dev', | which provides a specific path to the Tomcat instance. To use this, your plugin configuration | might hypothetically look like: | | ... | <plugin> | <groupId>org.myco.myplugins</groupId> | <artifactId>myplugin</artifactId> | | <configuration> | <tomcatLocation>${tomcatPath}</tomcatLocation> | </configuration> | </plugin> | ... | | NOTE: If you just wanted to inject this configuration whenever someone set 'target-env' to | anything, you could just leave off the <value/> inside the activation-property. | <profile> <id>env-dev</id> <activation> <property> <name>target-env</name> <value>dev</value> </property> </activation> <properties> <tomcatPath>/path/to/tomcat/instance</tomcatPath> </properties> </profile> --> <profile>
    <!--自定义jar仓库地址,需要active才可用-->
<id>central</id> <repositories> <repository> <id>central</id> <name>central</name> <url>http://192.168.0.128:8081/nexus/content/groups/public/</url> <layout>default</layout> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>true</enabled> </snapshots> </repository> </repositories> </profile> </profiles> <!-- activeProfiles | List of profiles that are active for all builds. | <activeProfiles> <activeProfile>alwaysActiveProfile</activeProfile> <activeProfile>anotherAlwaysActiveProfile</activeProfile> </activeProfiles> -->
  <!--激活profile对应的repository,配置mirror后会被replace-->
<activeProfiles> <activeProfile>central</activeProfile> </activeProfiles> </settings>

2、项目配置发布到对应的nexus仓库——直接通过idea

1> 项目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>
  <groupId>com.flysand</groupId>
  <artifactId>ShiroTest</artifactId>
  <packaging>war</packaging>
<!--版本信息,后缀加了SNAPSHOT就会默认发布到下面的snapshotRepository快照仓库下,不加则发布到repository仓库下-->
<version>1.0-SNAPSHOT</version> <name>ShiroTest Maven Webapp</name> <url>http://maven.apache.org</url> <properties> <spring-version>4.3.7.RELEASE</spring-version> <spring-security-version>4.2.2.RELEASE</spring-security-version> <spring-security-oauth2-version>2.1.0.RELEASE</spring-security-oauth2-version> <spring-data-redis>1.8.1.RELEASE</spring-data-redis> <spring-security-jwt-version>1.0.7.RELEASE</spring-security-jwt-version> <mybatis-version>3.4.2</mybatis-version> <mybatis-spring-version>1.3.1</mybatis-spring-version> <druid-version>1.0.29</druid-version> <fastjson-version>1.2.30</fastjson-version> <logback-version>1.2.2</logback-version> <mysql-version>5.1.41</mysql-version> <servlet-api-version>4.0.0-b03</servlet-api-version> <jstl-version>1.2</jstl-version> <commons-codec-version>1.10</commons-codec-version> <jackson-version>2.8.7</jackson-version> <pageHelper-version>4.2.1</pageHelper-version> <redis-client-version>2.9.0</redis-client-version> <shiro-version>1.3.2</shiro-version> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql-version}</version> </dependency> <dependency> <groupId>commons-codec</groupId> <artifactId>commons-codec</artifactId> <version>${commons-codec-version}</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>${jackson-version}</version> </dependency> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>${pageHelper-version}</version> </dependency> <!--spring springMvc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring-version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring-version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring-version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring-version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${spring-version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${spring-version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spring-version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>${spring-version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring-version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring-version}</version> </dependency> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId> <version>${spring-data-redis}</version> </dependency> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>${redis-client-version}</version> </dependency> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-jwt</artifactId> <version>${spring-security-jwt-version}</version> </dependency> <!--mybatis 数据库相关--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>${mybatis-version}</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>${mybatis-spring-version}</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>${druid-version}</version> </dependency> <!--tools--> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>${fastjson-version}</version> </dependency> <!--logback --> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>${logback-version}</version> </dependency> <!--servlet jstl --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>${servlet-api-version}</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>${jstl-version}</version> </dependency> <!--shiro--> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-core</artifactId> <version>${shiro-version}</version> </dependency> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-web</artifactId> <version>${shiro-version}</version> </dependency> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-spring</artifactId> <version>${shiro-version}</version> </dependency> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-ehcache</artifactId> <version>${shiro-version}</version> </dependency> </dependencies> <build> <finalName>ShiroTest</finalName> <plugins> <!--编译插件--> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.6.1</version> </plugin> <!--打包插件--> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <version>3.0.0</version> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-deploy-plugin</artifactId> <version>2.8.2</version> </plugin> </plugins> </build> <distributionManagement>
  <!--配置项目发布到nexus的仓库地址,id可以为任意字符串,但必须与setting.xml中的server节点的id对应server对应id吓得用户名密码用于登录当前的nexus并发布到对应的仓库目录下-->
<repository> <id>releases</id> <name>release repo</name> <url>http://192.168.0.128:8081/nexus/content/repositories/releases</url> </repository> <snapshotRepository> <id>snapshots</id> <name>Snapshots</name> <url>http://192.168.0.128:8081/nexus/content/repositories/snapshots</url> </snapshotRepository> </distributionManagement> </project>

2> 使用idea发布项目到nexus

 创建一个maven configuration,命令栏写deploy,运行会自动调用mvn deploy命令,注意pom.xml中添加相应的编译发布插件。

3、nexus配置

上面谈到了nexus私服的用法,下面写一下nexus的配置

先看一下,正常配置完成后,nexus如图所示,右上角显示的是admin,即使用admin/admin123登录后的界面,即要使用全部配置功能需要先点击右上角的login进行登录。

每个仓库的path显示当前仓库的url路径,可以在项目和maven配置文件中进行配置,如jar包的group,public是一个仓库组,会把包含的所有仓库的jar包、war包等全部引进来,当然也可以自定义建立group

1> 安装nexus

nexus官网下载地址:http://www.sonatype.org/nexus/downloads/

到官网下载nexus的oss开源版本,上传到自己的linux服务器,解压后会有两个目录,上面的是nexus的配置目录,下面是仓库、插件等目录

切换到nexus的bin目录,./nexus start 启动nexus,默认8081端口

2> 更新central index(非必须)

选中Repositories中的Central仓库,在configuration下的Download Remote Indexs选择true,即更新远程central仓库indexs,保存后,nexus会自动建立一个task schedule

这个过程非常长,因为要下载几个G的文件用于更新indexs,一定要确保网速及服务器空间。

更新索引的好处在于central中会下载所有的jar信息,方便查询,但这个步骤不是必须的。

3> nexus使用

public group默认包含全部的jar包、war包的仓库,因此在maven配置文件setting.xml中配置mirror镜像仓库时,一般都直接指到public,也可以自己建立group。

snapshots为默认快照版本的发布仓库,releases为默认的发布版本的仓库,即对应于项目pom.xml中配置的发布和快照仓库路径。

central对应中央仓库,3d party用于存放central中不存在的第三方jar包,如Oracle的驱动包。

4、maven安装配置

官网下载maven并上传到服务器,编辑maven配置文件maven安装目录/conf/setting.xml,因为这里主要是用于jenkins集成,即项目发布,因此要配置对应于项目pom.xml中发布仓库的server,配置maven环境变量,vim /etc/profile

5、jenkins配置

1> jenkins安装

jenkins官网下载地址:https://jenkins.io/download/

下载jenkins的war包,上传到服务器的tomcat(tomcat下载配置这里不赘述)下。

启动tomcat,即自动发布jenkins

2> jenkins配置

根据服务器地址访问/8080/jenkins

第一次启动jenkins,需要使用admin登录,默认密码在/root/.jenkins/secrets/initialAdminPassword里面,/root为当前登录服务器的用户

admin默认密码为一段字符串:

登录成功后,一般会提示安装一些插件,完成后进入下面的界面

进入 系统管理,可配置默认jdk、maven等,以及其他需要的插件,如maven插件、发布插件(可能在开始已经安装过)

3>新建项目

选择 新建 - 构建一个自由风格的软件项目,进入项目编辑页

主要配置项目svn地址,即svn登录名用户

构建,选择之前配置的maven

保存,选择立即构建,即可自行完成从svn下载最新代码并打包发布到nexus上。

 至此,整个集成环境的配置完毕。

原文地址:https://www.cnblogs.com/flysand/p/6836612.html