`
Before_Morning
  • 浏览: 34975 次
文章分类
社区版块
存档分类
最新评论

maven项目--使用nexus搭建私服

 
阅读更多

私服简介

私服是架设在局域网的一种特殊的远程仓库,目的是代理远程仓库及部署第三方构件。一般很多项目都是在内网的情况下开发的,即使有外网,一个团队中的所有人都有重复的从maven仓库下载构建可能因为网络带宽而影响了开发的效率。有了私服之后,当 Maven 需要下载构件时,直接请求私服,私服上存在则下载到本地仓库;否则,私服请求外部的远程仓库,将构件下载到私服,再提供给本地仓库下载。


目前,专门的Maven仓库管理软件搭建私服的主要有Apache Archiva,Artifactory,Sonatype Nexus。本文主要介绍一下如何使用nexus来在本地搭建一个私服。在搭建私服前我们需要一些准备工作,首先必须要在机器上安装maven,然后设置好maven的环境变量。

二、私服的搭建

1、首先检查一下机器上的maven环境是否搭建好了。在cmd下输入mvn -v 如果安装正确,就会显示maven的版本等相关信息。如下图所示

2、maven 环境搭建好之后,接下来就是要安装Nexus

nexus下载地址:http://www.sonatype.org/nexus/go/

下载之后,解压文件,安装nexus和启动,如下图:

3、启动之后,我们到浏览器输入:http://localhost:8081/nexus,如果出现nexus的主界面,那么说明安装成功了。由于nexus是使用jetty 启动的,默认的端口号时8081,如果要修改端口号可以进入D:\nexus-latest-bundle\nexus-2.11.1-01\conf下找到nexus.properties修改 端口号即可

4、输入账号和密码之后,进入了管理界面:

5、这里要先介绍一下Nexus的仓库:

Nexus的仓库分为一下几类:

A、hosted:宿主仓库----主要用于部署无法从公共仓库获取的构件以及第三方的项目构件

B、proxy:代理仓库----代理公共的远程仓库

C、virtual:虚拟仓库----用于适配Maven 1

D、group:仓库组----Nexus通过仓库组的概念统一管理多个仓库,这样我们在项目中直接请求仓库组就可以请求到仓库组管理的多个仓 库。

首次登陆的时候,显示的是如下仓库:

6新搭建的neuxs环境只是一个空的仓库,需要手动和远程中心库进行同步,nexus默认是关闭远程索引下载,最重要的一件事情就是开启远程索引下载

登陆后点击左边菜单栏中的resposities,在视图窗中显示了所有的仓库,找到右边仓库列表中的三个仓库Apache Snapshots,Codehaus Sna

pshots和Central,然后分别将三个仓库的configuration下把Download Remote Indexes修改为true。

如图:

分别对这三个仓库设置好后,在分别在刚才的三个仓库上右击,选择Repari Index,这样Nexus就会去下载远程的索引文件。如图:

上面的步骤都执行了之后,我们可以检查一下远程下载是否开启,在右边的搜索框中输入要搜索的构件名称,例如:maven,如果成功的话,在右边的视图中可以看到搜索的结果,如图:

7、建立自己的宿主仓库

终于所有的准备工作都做好了,下面开始就搭建自己的宿主仓库和仓库组来构建本地的私服。

A、建立宿主仓库

如下,我们建立一个宿主仓库:

建立好了宿主仓库后,我们在建立一个仓库组

其实,Nexus 中仓库组的概念是Maven没有的,在Maven看来,不管你是hosted也好,proxy也好,或者group也好,对我都是一样的,我只管 根据 groupId,artifactId,version等信息向你要构件。为了方便Maven的配置,Nexus能够将多个仓库,hosted或者 proxy合并成一个group,这样 Maven只需要依赖于一个group,便能使用所有该group包含的仓库的内容。

如图:

到此,我们就将自己的私服搭建好了,下面介绍下如何在Maven 中使用我们自己构建的私服。

8、Maven 使用私服

A、首先建立一个maven项目:

B、配置setting.xml文件

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

<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">
	<!-- The path to the local repository maven will use to store artifacts. -->

	<!-- Default: ${user.home}/.m2/repository -->
	<localRepository>D:/21050319maven/maven_rep/reposity</localRepository>
	<servers>
		<server>
			<id>jy1321</id>
			<username>admin</username>
			<password>admin123</password>
		</server>

		<server>
			<id>snapshot</id>
			<username>admin</username>
			<password>admin123</password>
		</server>
	</servers>

	<profiles>
		<profile>
			<id>public</id>
			<repositories>
				<repository>
					<id>public</id>
					<url>http://localhost:8081/nexus/content/groups/jjyy1321</url>
					<releases>
						<enabled>true</enabled>
					</releases>
					<snapshots>
						<enabled>true</enabled>
					</snapshots>
				</repository>
			</repositories>
		</profile>
	</profiles>
	
	<activeProfiles>
		<activeProfile>public</activeProfile>
	</activeProfiles>
</settings>
如图:

C、配置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>
  <groupId>com.1321.jjyy</groupId>
  <artifactId>web-jy</artifactId>
  <version>1.0</version>
  <packaging>war</packaging>
  <name>web_jiangyu</name>
  <properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<logback.version>1.0.7</logback.version>
		<slf4j.version>1.7.6</slf4j.version>
		<vlf.version>0.0.3-SNAPSHOT</vlf.version>
		<spring.version>3.2.6.RELEASE</spring.version>
		<cxf.version>2.2.5</cxf.version>
		<spring.security.version>3.2.0.RELEASE</spring.security.version>
  </properties>
  <dependencies>
  	<!-- <dependency>
	  <groupId>mysql</groupId>
	  <artifactId>mysql-connector-mxj-db-files</artifactId>
	  <version>5.0.12</version>
	</dependency> -->

  	<dependency>
	  <groupId>commons-lang</groupId>
	  <artifactId>commons-lang</artifactId>
	  <version>2.4</version>
	</dependency>
  	
  	<dependency>
	  <groupId>org.springframework</groupId>
	  <artifactId>spring-context</artifactId>
	  <version>${spring.version}</version>
	  <exclusions>
			<exclusion>
				<groupId>commons-logging</groupId>
				<artifactId>commons-logging</artifactId>
			</exclusion>
		</exclusions>
	</dependency>
  	<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context-support</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-orm</artifactId>
		  <version>3.2.6.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.security</groupId>
			<artifactId>spring-security-web</artifactId>
			<version>${spring.security.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.security</groupId>
			<artifactId>spring-security-config</artifactId>
			<version>${spring.security.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-jdbc</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-test</artifactId>
			<version>${spring.version}</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>c3p0</groupId>
			<artifactId>c3p0</artifactId>
			<version>0.9.1.2</version>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>log4j-over-slf4j</artifactId>
			<version>${slf4j.version}</version>
		</dependency>
		<!-- <dependency>
			<groupId>org.josql</groupId>
			<artifactId>josql</artifactId>
			<version>2.2</version>
		</dependency>
		<dependency>
			<groupId>fakepath</groupId>
			<artifactId>gentlyweb-utils</artifactId>
			<version>1.1</version>
		</dependency>
		<dependency>
			<groupId>com.mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>5.0.8</version>
			<classifier>bin</classifier>
		</dependency> -->
  </dependencies>
  <build>
		<resources>
			<resource>
				<directory>src/main/resources</directory>
				<filtering>true</filtering>
				<includes>
					<include>**/jdbc.properties</include>
				</includes>
			</resource>
			<resource>
				<directory>src/main/resources</directory>
				<filtering>false</filtering>
				<excludes>
					<exclude>**/jdbc.properties</exclude>
				</excludes>
			</resource>
		</resources>
		<defaultGoal>install</defaultGoal>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-resources-plugin</artifactId>
				<version>2.5</version>
				<configuration>
					<encoding>${project.build.sourceEncoding}</encoding>
				</configuration>
			</plugin>
			 <plugin>   
		        <groupId>org.apache.maven.plugins</groupId>   
		        <artifactId>maven-surefire-plugin</artifactId>   
		        <version>2.12.4</version>   
		        <configuration>   
		          <skipTests>true</skipTests>   
		        </configuration>   
    	    </plugin>
			<plugin>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>2.3.2</version>
				<configuration>
					<source>1.7</source>
					<target>1.7</target>
				</configuration>
			</plugin>
			<plugin>
				<artifactId>maven-war-plugin</artifactId>
				<version>2.2</version>
				<configuration>
					<version>2.5</version>
				</configuration>
			</plugin>
		</plugins>
	</build>
	
	<profiles>
		<profile>
			<id>dev_jy</id>
			<properties>
				<label>dd</label>
				<jdbc.url><![CDATA[jdbc\:mysql\://127.0.0.1\:3306/web_jjyy?zeroDateTimeBehavior\=convertToNull&characterEncoding=utf8]]></jdbc.url>
				<jdbc.user>root</jdbc.user>
				<jdbc.password>1234</jdbc.password>
				<jdbc.url.slave><![CDATA[jdbc\:mysql\://127.0.0.1\:3306/web_jjyy?zeroDateTimeBehavior\=convertToNull&characterEncoding=utf8]]></jdbc.url.slave>
				<jdbc.user.slave>root</jdbc.user.slave>
				<jdbc.password.slave>1234</jdbc.password.slave>
			</properties>
		</profile>
	</profiles>
</project>

D、使用私服

下面选中项目,执行maven install,就会自动将项目从自己搭建的私服中下载构件到本地的库中,如下图:

终于,搭建一个私服和使用私服的过程都结束了。看完之后相信也会自己构建一个私服了。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics