安装maven私服,引入代理

安装maven,配置私服

一、软件准备 

1、apache-maven-3.0-bin.tar.gz 下载地址:http://www.apache.org/dyn/closer.cgi/maven/binaries/apache-maven-3.0-bin.tar.gz 
2、nexus-oss-webapp-1.8.0-bundle.tar.gz 下载地址:http://nexus.sonatype.org/downloads/ 


二、maven安装配置 
1、创建需要操作maven的用户组以及用户(如果用root用户安装不用创建) 



    #groupadd configer  //创建用户组  
    #useradd -g configer configer  //创建用户并指定用户组  
    #passwd configer  //为用户分配密码  




2、创建解压目录,并将apache-maven-3.0-bin.tar.gz文件解压到指定目录 





    #cd /opt  
    #mkdir maven  
    #chown -R configer:configer /opt/maven  
    #chmod 755 /opt/maven  
    #su -l configer  
    #tar -zvxf apache-maven-3.0-bin.tar.gz  






2、配置环境变量 
Java代码 


    #vi /home/configer/.bash_profile  




在文件中添加如下行: 
Java代码 


    M2_HOME=/opt/maven/apache-maven-3.0  
    export M2_HOME  
    PATH=$PATH:$M2_HOME/bin  
    export PATH  






3、查看版本 
Java代码 


    #cd /opt/maven/apache-maven-3.0/bin  
    #mvn --version  




如果显示版本信息,应该会在${user}目录下创建.m2目录 


4、查看.m2目录 
Java代码 


    #cd /home/configer/.m2  
    如果没有.m2目录,则可以手动添加一个  
    #mkdir .m2  






5、如果需要把maven的repository目录指定到其他目录,则修改maven安装目录下conf中的配置文件settings.xml文件 
Java代码 


    #vi /opt/maven/apache-maven-3.0/conf/settings.xml  
    将文件中<localRepository>....</localRepository>的注释打开  
    或者在文件中增加 在这个注释下增加  
    <localRepository>your repository path</localRepository>  








二、搭建nexus私服 
1、解压nexus-oss-webapp-1.8.0-bundle.tar.gz文件到指定目录 
Java代码 


    #tar -zvxf nexus-oss-webapp-1.8.0-bundle.tar.gz  






2、启动nexus 
Java代码 


    #cd /opt/maven/nexus-oss-webapp-1.8.0/bin/jsw  
      
    选择自己机器的版本:  
      
    #cd linux-x86-32/  
    #./nexus start  
      
    重启:  
    #./nexus restart  
    停止:  
    #./nexus stop  






3、运行nexus 
在浏览器中输入:http://localhost:8081/nexus 
就可以看到nexus 的主页,点击右上角Log in 
默认用户名和密码是:admin/admin123 
运行后会自动生成一个nexus工作目录sonatype-work,nexus下载的jar包会存放在 
sonatype-work/nexus/storage中 


4、配置 
1)点击左侧菜单Repositories 
分别将右侧列表中 
Java代码 


    Apache Snapshots  
    Codehaus Snapshots  
    Maven Central  




三个repository 的Download Remote Index 配置改为True,并保存设置, 
然后在列表中分别右键点击三个Repository,点击ReIndex 


2)增加新的Repository,有一些比较常用jar包在nexus提供的repository中可能找不到, 
一般比较常用的有 
Java代码 


    JBOSS的两个:  
    http://repository.jboss.org/maven2/  
    http://repository.jboss.org/nexus/content/repositories/releases/  
    SUN的:  
    http://download.java.net/maven/2/  
    K-INT的:  
    http://developer.k-int.com/maven2/  
      
    因为找juel:juel-impl:2.2.1 这个jar包,所以我还添加了一个自己找的:  
    http://repository.exoplatform.org/content/groups/public/  






添加步骤: 
Java代码 


    点击Add->Proxy Repository->填写Repository ID, Repository Name, 以及Remote Storage Location 其他的默认即可。  






3) 将新增的Repository添加到Public Repositories中 
在Public Repositories 的Configuration中,将多选Select中的项全部添加到左边,然后保存。 


4) 添加自己的jar包 
Java代码 


    在repository列表中有一个3rd party,也就是第三方jar包,点击会看到一个Artifact Upload选项卡,点击后,填写相应的信息。  
    GAV Definition 一般选择 GAV Parameters  
    然后添加Group:Artifact:Version:Package  
    示例    juel:juel-impl:2.2.1:jar  






然后选择要上传的jar包,保存即可 


5) nexus中设置代理服务器 
选择左侧administrator菜单中的Server选项,在右侧打开的页面的中下部,有一个选择项:Default HTTP Proxy Settings(optional) 将复选框选中,填写相应的代理服务器信息即可。 


6) 编写自己的settings.xml,文件内容如下: 
Java代码 


    <settings>  
      <proxies>  
        <proxy>  
          <id>normal</id>  
          <active>true</active>  
          <protocol>http</protocol>  
          <username>deployment</username>  
          <password>deploy</password>  
          <host>localhost:8081/nexus</host>  
          <port>80</port>  
          <nonProxyHosts>localhost:8081/nexus</nonProxyHosts>  
        </proxy>  
      </proxies>  
      
      <mirrors>  
        <mirror>  
          <!--This is used to direct the public snapshots repo in the   
              profile below over to a different nexus group -->  
          <id>nexus-public-snapshots</id>  
          <mirrorOf>public-snapshots</mirrorOf>  
          <url>http://localhost:8081/nexus/content/groups/public-snapshots</url>  
        </mirror>  
        <mirror>  
          <!--This sends everything else to /public -->  
          <id>nexus</id>  
          <mirrorOf>*</mirrorOf>  
          <url>http://localhost:8081/nexus/content/groups/public</url>  
        </mirror>  
      </mirrors>  
        
      <profiles>  
        <profile>  
          <id>development</id>  
          <repositories>  
            <repository>  
              <id>central</id>  
              <url>http://central</url>  
              <releases><enabled>true</enabled></releases>  
              <snapshots><enabled>true</enabled></snapshots>  
            </repository>  
          </repositories>  
         <pluginRepositories>  
            <pluginRepository>  
              <id>central</id>  
              <url>http://central</url>  
              <releases><enabled>true</enabled></releases>  
              <snapshots><enabled>true</enabled></snapshots>  
            </pluginRepository>  
          </pluginRepositories>  
        </profile>  
        <profile>  
          <!--this profile will allow snapshots to be searched when activated-->  
          <id>public-snapshots</id>  
          <repositories>  
            <repository>  
              <id>public-snapshots</id>  
              <url>http://public-snapshots</url>  
              <releases><enabled>false</enabled></releases>  
              <snapshots><enabled>true</enabled></snapshots>  
            </repository>  
          </repositories>  
         <pluginRepositories>  
--------------------------------------------------------------------------------
遇到这种警告:


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


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.
大概意思就是要在环境配置export RUN_AS_USER=root,临时配置


在命令行下输入:


export RUN_AS_USER=root


然后执行,就不会再提示了


./nexus start
---------------------------------------------------------------
Linux在启动时,会自动执行/etc/rc.d目录下的初始化程序,因此我们可以把启动任务放到该目录下:
1、因为其中的rc.local是在完成所有初始化之后执行,因此可以把启动脚本写到里面; 


2、用root账号登陆Linux,vi /etc/rc.d/rc.local编辑文件,在最后加入需要执行的脚本程序:




su -l $username -c "/nexus/nexus-2.8.0-05/bin/nexus start"


发布不在远程仓库中jar包

使用maven一般都会配置引用资源库,默认使用官方中央库,也可以自己搭建速度更好的局域网资源库。

项目中可能会引用到一些较新版本的资源,而这些资源还未添加到maven资源库中,此时就需要自己手动来添加这些资源了。

如果仅自己使用,安装到本地库即可

一般直接mvn install就可以了,当然也可以设置更详细的参数

例如添加gwt-log包:

mvn install:install-file -DgroupId=com.google.code.gwt-log -DartifactId=gwt-log -Dversion=3.1.0 -Dpackaging=jar -Dfile=gwt-log-3.1.0.jar

如果项目组都需要添加引用,也可将资源部署到中央库(一般为私服)

发布到中央库需要具有一定权限

mvn deploy:deploy-file -DgroupId=com.google.inject.extensions -DartifactId=guice-servlet -Dversion=3.0 -Dpackaging=jar -Dfile=guice-servlet-3.0.jar -Durl=http://www.some-domain.com/some-path -DrepositoryId=nexus

mvn deploy:deploy-file -DgroupId=com.xy.oracle -DartifactId=ojdbc14 -Dversion=10.2.0.4.0 -Dpackaging=jar -Dfile=E:\ojdbc14.jar -Durl=http://localhost:9090/nexus-2.2-01/content/repositories/thirdparty/ -DrepositoryId=thirdparty

DgroupId和DartifactId构成了该jar包在pom.xml的坐标,项目就是依靠这两个属性定位。自己起名字也行。

Dfile表示需要上传的jar包的绝对路径。



settings.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">
 
  <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>
    
    <server>
    <id>mycompany-releases</id>
    <username>admin</username>
    <password>admin123</password>
    </server>
    <server>
    <id>mycompany-snapshots</id>
    <username>admin</username>
    <password>admin123</password>
    </server>

  </servers>
  <mirrors>
<mirror>
<id>nexus_group</id>
<mirrorOf>*</mirrorOf>
<name>nexus_group</name>
<url>http://192.168.10.88:8081/nexus/content/groups/public/</url>
</mirror>
  </mirrors>
  <profiles>
  
<!-- 执行:$ mvn release:prepare 命令时会打包并发布到该仓库。 -->
  <profile>
 <id>mycompany-releases</id>
 <repositories>
<repository>
 <id>mycompany-releases</id>
 <name>local private nexus</name>
 <url>http://192.168.10.88:8081/nexus/content/repositories/releases/</url>
</repository>
 </repositories>
</profile>

<!-- 执行:$ mvn deploy 命令时会打包并发布到该仓库。 -->
<profile>
 <id>mycompany-snapshots</id>
 <repositories>
<repository>
 <id>mycompany-snapshots</id>
 <name>local private nexus snapshots</name>
 <url>http://192.168.10.88:8081/nexus/content/repositories/snapshots/</url>
</repository>
 </repositories>
</profile>


  </profiles>


 <!-- 激活配置-->
  <activeProfiles>
    <activeProfile>mycompany-releases</activeProfile>
<activeProfile>mycompany-snapshots</activeProfile> 
  </activeProfiles>
  
</settings>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值