工作心得(二)——如何将Nexus本地仓库和Maven结合起来

来源:互联网 发布:php二维数组查找键值 编辑:程序博客网 时间:2024/04/30 20:19

  这篇文章承接上一篇文章,接下来讲如何将Nexus本地仓库和Maven结合起来,实现maven去本地仓库中获取jar包,而不是去中央仓库;实现将自己的jar包发布到本地仓库中,本文只介绍如何搭建这样的环境,并不讲解这种环境的适用范围。

---------------------------------------------------------------------------------------------------------------------------------------------

1.maven的安装

1).下载maven

访问maven官网:http://maven.apache.org/   下载自己需要的版本的maven,windows系统下载zip包,linux下载tar包,我这里下载了最新的

3.3.9版本。


2).解压下载的apache-maven-3.3.9-bin.zip,得到apache-maven-3.3.9文件。

3).配置maven的环境变量

a).添加一个变量M2_HOME 变量的值就是解压出来的apache-maven-3.3.9的路径.

b).在添加一个变量M2 变量的值就是%M2_HOME%\bin。

c).在path环境变量中增加:%M2%;

d).在命令提示行中输入mvn -v 验证环境变量是否配置成功.

看到上图的结果表示配置成功。

4).配置maven

apache-maven-3.3.9\conf\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">  <!-- localRepository   | The path to the local repository maven will use to store artifacts.   |   | Default: ${user.home}/.m2/repository    -->  <!-- 这里表示本地引用的mavenjar包的路径,从仓库上下载的jar包会保存在这个路径 --><localRepository>F:/Work/.m2/repository</localRepository>  <pluginGroups>     </pluginGroups>    <proxies>      </proxies> <!-- 这里配置的是nexus仓库的账号密码信息 这里的ID要跟项目中的pom文件中的配置一致 -->  <servers>       <server>       <id>releases</id>       <username>admin</username>       <password>admin123 </password>     </server>    <server>     <id>snapshots</id>     <username>admin</username>     <password>admin123</password>     </server>     </servers>  <!-- 这里是nexus仓库的地址 -->  <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>          <pluginRepository>            <id>public-snapshots</id>            <url>http://public-snapshots</url>            <releases><enabled>false</enabled></releases>            <snapshots><enabled>true</enabled></snapshots>          </pluginRepository>        </pluginRepositories>      </profile>     <profile>      <id>jdk-1.7</id>       <activation>            <activeByDefault>true</activeByDefault>            <jdk>1.7</jdk>        </activation>  <properties>  <maven.compiler.source>1.7</maven.compiler.source>  <maven.compiler.target>1.7</maven.compiler.target>  <maven.compiler.compilerVersion>1.7</maven.compiler.compilerVersion>  </properties>  </profile>     </profiles>  <activeProfiles>      <activeProfile>development</activeProfile>    </activeProfiles>  </settings>
2.nexus的配置(前一篇文章已经讲述如何安装nexus)

访问 http://localhost:8081/nexus/ 然后点击右上角登陆 初始账号密码就是我们上面配置的那个(不是因为配置那个才是这个账号密码,而是因为是这个账号密码才配置成那样) admin/admin123.

然后点击左边的Repositories 在单击 Central 在下面的标签中 点击 Configuration

Default Local Storage Location表示默认的仓库保存路径,这里保存了jar包,如果想修改目录则Override Local Storage Location 输入框中输入自定义的目录。

Download Remote Indexes:设置为ture,表示从中央仓库去下载索引。

然后右键点击Central repair index(亲身试验,这种方式更新中央仓库的索引,非常慢,所以还是按照下面的方法来吧).

手动更新索引:

访问http://repo.maven.apache.org/maven2/.index/下载中心仓库最新版本的索引文件,在一长串列表中,我们需要下载如下两个文件(一般在列表的末尾位置)

nexus-maven-repository-index.gz

nexus-maven-repository-index.properties

然后再到这个地址下载一个indexer-cli.jar包:http://search.maven.org/#search%7Cgav%7C1%7Cg%3A%22org.apache.maven.indexer%22%20AND%20a%3A%22indexer-cli%22 . 这里我下载的是5.1.1版本。

将这三个文件放到同一个目录,然后执行在命令行进入这个目录,执行如下命令:

java -jar indexer-cli-5.1.1.jar -u nexus-maven-repository-index.gz -d indexer

这个过程需要几分钟,稍后则会得到一个indexer目录,大概700多M,将里面的文件复制到sonatype-work\nexus\indexer\central-ctx(nexus的工作空间目录,参考前一篇文章,复制前删掉本目录内的文件)。

然后再进入nexus页面,就可以看到索引已经更新成功:

3.验证maven和nexus是否配置成功

在命令行输入 mvn help:system 看到build success 则表示配置成功。

4.eclipse的配置

1).首先打开eclipse菜单 windows>preferences>maven

点击add>directory 选择我们的maven目录.然后点击finish,在上面界面选择我们添加的maven.

点击user settings 将其中的setting目录配置成我们的maven中的settings目录。

4.创建maven项目

1).新建一个maven项目

2).pom.xml文件配置如下


 <build><plugins><!-- 编译插件 --><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.1</version><configuration><source>1.7</source><target>1.7</target><encoding>${project.build.sourceEncoding}</encoding></configuration></plugin><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-resources-plugin</artifactId><version>2.6</version><configuration><encoding>${project.build.sourceEncoding}</encoding></configuration></plugin><!-- 源码插件 --><plugin><artifactId>maven-source-plugin</artifactId><version>2.4</version><configuration><attach>true</attach></configuration><executions><execution><phase>package</phase><goals><goal>jar-no-fork</goal></goals></execution></executions></plugin><!-- 单元测试插件 --><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-surefire-plugin</artifactId><version>2.9</version><configuration><skip>true</skip></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></plugins></build><!-- 这里的id配置同maven settings中的server里的id相对应 --><distributionManagement><repository><id>releases</id><name>Nexus Release Repository</name><url>http://localhost:8081/nexus/content/repositories/releases/</url></repository><snapshotRepository><id>snapshots</id><name>Nexus Snapshot Repository</name><url>http://localhost:8081/nexus/content/repositories/snapshots/</url></snapshotRepository></distributionManagement>

然后右键项目,run as>maven install 看到build success 表示该项目可以正常构建.

3).将项目发布到nexus仓库上

打开命令提示行窗口,进入该项目的路径

输入mvn deploy 则可以把该项目发布到nexus上,发布到nexus上之后,其他项目就可以引入这个项目到maven中

如果发现build 失败,可以mvn deploy -X 查看详细构建过程以及错误信息。

右下角的信息表示在pom文件中插入这些内容就可以引入到这个jar包。

文章有点流水化,不过也都是我一步步操作的,如果有什么不明白的地方欢迎留言。

0 0