如何用maven创建nexus私服并将包打包发布到私服

来源:互联网 发布:淘宝直通车没流量 编辑:程序博客网 时间:2024/06/14 17:28

下载好nexus后输入:localhost:8081/nexus 进入nexus的管理界面,默认用户名:admin 密码:admin123


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


登录后进入管理界面




将此处设为true保存后就可以在如下的位置看到包的信息(刚设置好刷新并不会有架包显示出来,等配好私服下次maven从

私服下载时,私服会从远程仓库下载过来)






下面简单介绍下仓库的种类

一般用到的仓库种类是hosted、proxy。Hosted代表宿主仓库,用来发布一些第三方不允许的组件,比如oracle驱动、比如商业软件jar包。Proxy代表代理远程的仓库,最典型的就是Maven官方中央仓库、JBoss仓库等等。如果构建的Maven项目本地仓库没有依赖包,那么就会去这个代理站点去下载,那么如果代理站点也没有此依赖包,就回去远程中央仓库下载依赖,这些中央仓库就是proxy。代理站点下载成功后再下载至本机。笔者认为,其实Maven这个自带的默认仓库一般情况下已经够大多数项目使用了。特殊情况时在配置新的仓库,指定url即可,一般熟悉ExtJS的人操作这个Nexus都没什么问题,单词不是很难,不明白的查查单词基本差不多。就是如果Sonatype公司对其做了国际化的处理就更好了。

hosted   类型的仓库,内部项目的发布仓库
releases 内部的模块中release模块的发布仓库
snapshots 发布内部的SNAPSHOT模块的仓库

3rd party 第三方依赖的仓库,这个数据通常是由内部人员自行下载之后发布上去

proxy   类型的仓库,从远程中央仓库中寻找数据的仓库

group   类型的仓库,组仓库用来方便我们开发人员进行设置的仓库



maven上传架包到私服的两种方式


1.手动上传




这样jar包就传到了私服上


2.通过maven上传


首先就是配置setting.xml将下载路径指向私服


setting.xml配置如下:


此处配置便于之后直接的打包发布


     <server>          <id>my-nexus-releases</id>          <username>admin</username>          <password>admin123</password>        </server>        <server>          <id>my-nexus-snapshot</id>          <username>admin</username>          <password>admin123</password>        </server>  </servers>


镜像配置,连接本地的私服


  <mirrors>    <mirror>            <id>nexus</id>             <url>http://127.0.0.1:8081/nexus/content/groups/public/</url>            <mirrorOf>*</mirrorOf>          </mirror>   </mirrors>


文件配置


<profiles>        <profile>          <id>nexus</id>          <repositories>                <repository>                    <id>central</id>                                                       <url>http://central</url>                                          <releases>                        <enabled>true</enabled>        <updatePolicy>always</updatePolicy>                </releases>                    <snapshots>                        <enabled>true</enabled>    <updatePolicy>always</updatePolicy>                </snapshots>                </repository>            </repositories>                <pluginRepositories>                <pluginRepository>                  <id>central</id>                  <url>http://central</url>                  <releases>                    <enabled>true</enabled><updatePolicy>always</updatePolicy>              </releases>                  <snapshots>                    <enabled>true</enabled><updatePolicy>always</updatePolicy>              </snapshots>                </pluginRepository>            </pluginRepositories>        </profile>    </profiles>  


激活文件


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


配置好之后,maven就会向本地的私服下载jar包了


接下来讲一下如何如何通过maven打包发布到私服


pom.xml配置


<repositories>           <repository>             <id>nexus</id>             <name>my-nexus-repository</name>             <url>http://127.0.0.1:8081/nexus/content/groups/public/</url>             <releases>               <enabled>true</enabled>             </releases>             <snapshots>               <enabled>false</enabled>             </snapshots>           </repository>         </repositories>         <pluginRepositories>           <pluginRepository>             <id>nexus</id>             <name>my-nexus-repository</name>             <url>http://127.0.0.1:8081/nexus/content/groups/public/</url>             <releases>               <enabled>true</enabled>             </releases>             <snapshots>               <enabled>false</enabled>             </snapshots>                </pluginRepository>         </pluginRepositories>    <distributionManagement>           <repository>               <id>my-nexus-releases</id>               <url>http://127.0.0.1:8081/nexus/content/repositories/releases/</url>           </repository>                      <snapshotRepository>               <id>my-nexus-snapshot</id>               <url>http://127.0.0.1:8081/nexus/content/repositories/snapshots/</url>           </snapshotRepository>      </distributionManagement>


maven打包的一些插件


<build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>2.3.2</version><configuration><source>1.8</source><target>1.8</target><encoding>UTF-8</encoding><compilerArguments><verbose /><bootclasspath>${JAVA_HOME}/jre/lib/rt.jar;${JAVA_HOME}/jre/lib/jce.jar</bootclasspath></compilerArguments></configuration></plugin><plugin><!-- 发布插件 --><groupId>org.apache.maven.plugins</groupId><artifactId>maven-deploy-plugin</artifactId><version>2.5</version></plugin><plugin><!-- 打包插件 --><groupId>org.apache.maven.plugins</groupId><artifactId>maven-jar-plugin</artifactId><version>2.3.1</version></plugin><plugin><!-- 安装插件 --><groupId>org.apache.maven.plugins</groupId><artifactId>maven-install-plugin</artifactId><version>2.3.1</version></plugin><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-war-plugin</artifactId><version>2.1.1</version><configuration><failOnMissingWebXml>false</failOnMissingWebXml></configuration></plugin><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-resources-plugin</artifactId><version>2.4.3</version></plugin><plugin><!-- 单元测试插件 --><groupId>org.apache.maven.plugins</groupId><artifactId>maven-surefire-plugin</artifactId><version>2.18.1</version><configuration><skipTests>true</skipTests></configuration></plugin></plugins></build>


这里特别要注意JAVA_HOME的路径和此处对应,否则打包时会报错


<bootclasspath>${JAVA_HOME}/jre/lib/rt.jar;${JAVA_HOME}/jre/lib/jce.jar</bootclasspath>


配置完成后,运行一下命令就行了

mvn clean package 打包项目,会在target下生成jar包

mvn install 将包上传到本地的仓库中

mvn deploy 将包部署发布到私服





0 0