Maven安装和nexus搭建

本文以Ubuntu Linux为例

一、安装Maven

到官网去下载maven和nexus安装包

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

nexus:http://www.sonatype.org/nexus/thank-you-for-downloading/?dl=tgz

本文下载的maven是apache-maven-3.2.5-bin.tar.gz

下载完安装包后,将maven放到硬盘上,cd到maven所在目录,执行命令

tar -zxvf apache-maven-3.2.5-bin.tar.gz

将maven解压缩到硬盘上,打开/etc/profile

vi /etc/profile

 按G键找到文件末尾,按小写o进入下一行,输入环境变量

export MAVEN_HOME=/home/zhifang/local/apache-maven-3.2.5
export PATH=$PATH:$MAVEN_HOME/bin

 完成后执行

source /etc/profile

然后在控制台执行

mvn -version

 出现下面的内容表示maven安装成功

root@zhifang-OptiPlex-3020:/home/zhifang/.m2/repository# mvn -v
Apache Maven 3.2.5 (12a6b3acb947671f09b81f49094c53f426d8cea1; 2014-12-15T01:29:23+08:00)
Maven home: /home/zhifang/local/apache-maven-3.2.5
Java version: 1.7.0_72, vendor: Oracle Corporation
Java home: /home/zhifang/soft/jdk1.7.0_72/jre
Default locale: zh_CN, platform encoding: UTF-8
OS name: "linux", version: "3.11.0-15-generic", arch: "amd64", family: "unix"

二、安装Nexus

接下来我们安装nexus,将nexus放到和maven相同的目录,cd切换到该目录,执行下面的命令

tar -zxvf nexus-latest-bundle.tar.gz

 nexus解压缩完成后,我们进入nexus目录,去启动nexus

nexus启动是在bin目录下,首先看一下启动/关闭/重启等命令, 输入命令:
#cd /usr/local/nexus/bin
#./nexus

出现如下选项:
[root@test01 bin]# ./nexus
Usage: ./nexus { console | start | stop | restart | status | dump }

启动nexus:
#./nexus  start


关闭nexus: 
#./nexus  stop

 启动成功后,打开 http://localhost:8081/nexus,点击右上角login in,输入admin/admin123

登录nexus控制台后,点击左侧列表区的Respositories,右侧会打开一个列表区

依次点击Apache Snapshots,Central,Codehaus Snapshots这三个选项,在打开的窗口里选择Configuration标签,更改Remote Storage Location的值为true,该值默认是false

再右击这三个选项,分别选中Repair Index

三、修改Settings

 打开Maven目录下的Settings文件,在profiles选项里增加如下内容

<profile>
  <id>nexus</id>  
        <repositories>  
            <repository>  
                <id>nexus</id>  
                <name>Nexus</name>  
                <url>http://localhost:8081/nexus/content/groups/public/</url>  
                <releases>  
                    <enabled>true</enabled>  
                </releases>  
                <snapshots>  
                    <enabled>true</enabled>  
                </snapshots>  
            </repository>  
        </repositories>  
        <pluginRepositories>  
            <pluginRepository>  
                <id>nexus</id>  
                <name>Nexus</name>  
                <url>http://localhost:8081/nexus/content/groups/public/</url>  
                <releases>  
                    <enabled>true</enabled>  
                </releases>  
                <snapshots>  
                    <enabled>true</enabled>  
                </snapshots>  
            </pluginRepository>  
        </pluginRepositories>  
  </profile>

 在profiles外面增加如下内容,activeProfile的内容要和profile的id保持一致

<activeProfiles>  
    <activeProfile>nexus</activeProfile>  
 </activeProfiles>

 在serves里增加如下内容,这个id要和pom.xml的id保持一致

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

四、eclipse配置

在eclipse里选中window-Preferences-maven-Installactions,将本机安装的maven加载进来,再选中User Settings,在User Settings里加载本机安装的Maven/conf/settings,Local Repository可以不用变更。

配置完成后,在左侧工程区右键新建一个Maven Project工程,Group Id的Artifact Id选中Maven-archetype-quickstart,新建成功。

此时,右键选中新建的maven工程,在run as里能看到maven build等一系列命令,说明maven集成eclipse成功。
 
五、修改pom.xml
pom.xml默认是去请求Maven而不是nexus,在这里我们让pom.xml直接去请求nexus,因此需要在pom.xml里增加下面的内容,需要注意的是,这里的id和刚才settings里配的serves里的id保持一致。
<distributionManagement>  
  <repository>  
    <id>nexus-releases</id>  
      <name>Nexus Release Repository</name>  
      <url>http://localhost:8081/nexus/content/repositories/releases/</url>  
  </repository>  
  <snapshotRepository>  
    <id>nexus-snapshots</id>  
    <name>Nexus Snapshot Repository</name>  
    <url>http://localhost:8081/nexus/content/repositories/snapshots/</url>  
  </snapshotRepository>  
</distributionManagement>
 
六、测试
配置完成后,我们在工程中单击右键,选择run as,在新窗口中的Goals输入complie,控制台如果出现http://localhost:8081/这样的jar路径,就说明我们的pom.xml是通过nexus去下载jar包的。
如果要打包工程,在Goals里输入package,maven就会将当前工程打包后放在target目录下。
 
七、windows下安装maven和nexus参考
在windows里配置maven不需要在nexus控制台进行设置,也不需要在maven的settings里定义profile,但是需要在settings的mirrors定义中央仓库的请求地址
<mirror>
      <id>central</id>
      <mirrorOf>central</mirrorOf>
      <name>Human Readable Name for this Mirror.</name>
      <url>http://192.168.125.77:8081/nexus/content/groups/public/</url> 
</mirror>
定义之后,maven在请求时默认就去私服中下载了,如果私服和maven不在同一台服务器上,需要把localhost改为nexus所在服务器的IP地址。

本文以Ubuntu Linux为例

一、安装Maven

到官网去下载maven和nexus安装包

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

nexus:http://www.sonatype.org/nexus/thank-you-for-downloading/?dl=tgz

本文下载的maven是apache-maven-3.2.5-bin.tar.gz

下载完安装包后,将maven放到硬盘上,cd到maven所在目录,执行命令

tar -zxvf apache-maven-3.2.5-bin.tar.gz

将maven解压缩到硬盘上,打开/etc/profile

vi /etc/profile

 按G键找到文件末尾,按小写o进入下一行,输入环境变量

export MAVEN_HOME=/home/zhifang/local/apache-maven-3.2.5
export PATH=$PATH:$MAVEN_HOME/bin

 完成后执行

source /etc/profile

然后在控制台执行

mvn -version

 出现下面的内容表示maven安装成功

root@zhifang-OptiPlex-3020:/home/zhifang/.m2/repository# mvn -v
Apache Maven 3.2.5 (12a6b3acb947671f09b81f49094c53f426d8cea1; 2014-12-15T01:29:23+08:00)
Maven home: /home/zhifang/local/apache-maven-3.2.5
Java version: 1.7.0_72, vendor: Oracle Corporation
Java home: /home/zhifang/soft/jdk1.7.0_72/jre
Default locale: zh_CN, platform encoding: UTF-8
OS name: "linux", version: "3.11.0-15-generic", arch: "amd64", family: "unix"

二、安装Nexus

接下来我们安装nexus,将nexus放到和maven相同的目录,cd切换到该目录,执行下面的命令

tar -zxvf nexus-latest-bundle.tar.gz

 nexus解压缩完成后,我们进入nexus目录,去启动nexus

nexus启动是在bin目录下,首先看一下启动/关闭/重启等命令, 输入命令:
#cd /usr/local/nexus/bin
#./nexus

出现如下选项:
[root@test01 bin]# ./nexus
Usage: ./nexus { console | start | stop | restart | status | dump }

启动nexus:
#./nexus  start


关闭nexus: 
#./nexus  stop

 启动成功后,打开 http://localhost:8081/nexus,点击右上角login in,输入admin/admin123

登录nexus控制台后,点击左侧列表区的Respositories,右侧会打开一个列表区

依次点击Apache Snapshots,Central,Codehaus Snapshots这三个选项,在打开的窗口里选择Configuration标签,更改Remote Storage Location的值为true,该值默认是false

再右击这三个选项,分别选中Repair Index

三、修改Settings

 打开Maven目录下的Settings文件,在profiles选项里增加如下内容

<profile>
  <id>nexus</id>  
        <repositories>  
            <repository>  
                <id>nexus</id>  
                <name>Nexus</name>  
                <url>http://localhost:8081/nexus/content/groups/public/</url>  
                <releases>  
                    <enabled>true</enabled>  
                </releases>  
                <snapshots>  
                    <enabled>true</enabled>  
                </snapshots>  
            </repository>  
        </repositories>  
        <pluginRepositories>  
            <pluginRepository>  
                <id>nexus</id>  
                <name>Nexus</name>  
                <url>http://localhost:8081/nexus/content/groups/public/</url>  
                <releases>  
                    <enabled>true</enabled>  
                </releases>  
                <snapshots>  
                    <enabled>true</enabled>  
                </snapshots>  
            </pluginRepository>  
        </pluginRepositories>  
  </profile>

 在profiles外面增加如下内容,activeProfile的内容要和profile的id保持一致

<activeProfiles>  
    <activeProfile>nexus</activeProfile>  
 </activeProfiles>

 在serves里增加如下内容,这个id要和pom.xml的id保持一致

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

四、eclipse配置

在eclipse里选中window-Preferences-maven-Installactions,将本机安装的maven加载进来,再选中User Settings,在User Settings里加载本机安装的Maven/conf/settings,Local Repository可以不用变更。

配置完成后,在左侧工程区右键新建一个Maven Project工程,Group Id的Artifact Id选中Maven-archetype-quickstart,新建成功。

此时,右键选中新建的maven工程,在run as里能看到maven build等一系列命令,说明maven集成eclipse成功。   五、修改pom.xml pom.xml默认是去请求Maven而不是nexus,在这里我们让pom.xml直接去请求nexus,因此需要在pom.xml里增加下面的内容,需要注意的是,这里的id和刚才settings里配的serves里的id保持一致。
<distributionManagement>  
  <repository>  
    <id>nexus-releases</id>  
      <name>Nexus Release Repository</name>  
      <url>http://localhost:8081/nexus/content/repositories/releases/</url>  
  </repository>  
  <snapshotRepository>  
    <id>nexus-snapshots</id>  
    <name>Nexus Snapshot Repository</name>  
    <url>http://localhost:8081/nexus/content/repositories/snapshots/</url>  
  </snapshotRepository>  
</distributionManagement>
  六、测试 配置完成后,我们在工程中单击右键,选择run as,在新窗口中的Goals输入complie,控制台如果出现http://localhost:8081/这样的jar路径,就说明我们的pom.xml是通过nexus去下载jar包的。 如果要打包工程,在Goals里输入package,maven就会将当前工程打包后放在target目录下。   七、windows下安装maven和nexus参考 http://www.cnblogs.com/quanyongan/archive/2013/04/24/3037589.html http://www.csdn123.com/html/itweb/20130921/129043_129045_129047.htm 在windows里配置maven不需要在nexus控制台进行设置,也不需要在maven的settings里定义profile,但是需要在settings的mirrors定义中央仓库的请求地址
<mirror>
      <id>central</id>
      <mirrorOf>central</mirrorOf>
      <name>Human Readable Name for this Mirror.</name>
      <url>http://192.168.125.77:8081/nexus/content/groups/public/</url> 
</mirror>
定义之后,maven在请求时默认就去私服中下载了,如果私服和maven不在同一台服务器上,需要把localhost改为nexus所在服务器的IP地址。

猜你喜欢

转载自wzf7065.iteye.com/blog/2185348