您的位置:首页 > 其它

maven+nexus的创建和常见操作

2017-11-19 15:56 141 查看
Maven 是软件项目管理工具,除了以程序构建能力为特色之外,还提供高级项目管理工具。nexus是一种私服软件,我们常常在搭建maven的时候,会搭建nexus私有仓库,当项目获取项目依赖时(pom.xml中配置),系统会首先在本地仓库寻找需要的依赖包,当没有找到会去私有仓库查找,还没有找到,会去中央仓库查找。还有另一种场景,一些公司有一些自己的jar包,不需要外面的人获取或者是中央央仓库找不到的包,会将jar包发布到私有仓库,而私有仓库会搭建在内网上,这样外面的人就无法获取。另附mavenSearch平台maven-search

常常有人在问maven有什么用,为什么要用maven,其实我自己也不是很懂,就简单说一下我知道的.

maven可以管理项目的jar包,统一项目的jar版本,便于多开发人员的jar包统一。

maven可以对于多项目可以统一配置依赖,也可以使用模块开发。

maven 集成了一连串的命令,一键化编译测试发布打包 等等。

使用maven构建的模块可以一串代码重复使用,提高开发速度。

一:Linux 上搭建maven

先下载maven的安装文件apache-maven-3.0.3-bin.tar.gz

进入下载文件夹找到下载的文件解压:

tar -xvf apache-maven-3.0.3-bin.tar.gz

解压后的文件夹为apache-maven-3.0.3

使用mv命令将apache-maven-3.0.3文件夹拷贝到自己指定的文件夹,比如/usr/local/下

mv -rf apache-maven-3.0.3 /usr/local/

配置环境变量,编辑/etc/profile文件,添加如下代码

export MAVEN_HOME=/usr/local/apache-maven-3.0.3

export PATH=${PATH}:${MAVEN_HOME}/bin

保存文件,并运行如下命令使环境变量生效

source /etc/profile

在控制台输入如下命令,如果能看到Maven相关版本信息,则说明Maven已经安装成功

mvn -version

二:Linux 上搭建本地仓库

下载nexus,下载地址为http://www.sonatype.org/nexus/go得到nexus-2.14.5-02-bundle.tar.gz

下载完成后将nexus-latest-bundle.tar.gz拷贝到/usr/local/目录下,并进入/usr/local目录

sudo cp nexus-2.14.5-02-bundle.tar.gz /usr/local/

cd /usr/local

解压nexus-2.14.5-02-bundle.tar.gz

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

编辑nexus脚本,配置RUN_AS_USER参数

cd /usr/local/nexus-2.14.5-02/bin

vi nexus

将#RUN_AS_USER=改为RUN_AS_USER=root

保存退出

在/usr/local/nexus-2.14.5-02/conf/nexus.properties中配置的application-port=8081

vi nexus.properties

运行nexus

./bin/nexus start

在浏览器中访问:ip:8081/nexus ,输入用户名与密码(默认的admin、admin123)就可以管理自己的maven本地仓库了.

三:私有仓库的使用

登录nexus网址,我们会看到如下画面:



简单介绍一下文件的目录:

public Reoisitories:group类型,在下面的configuration下可以将各个仓库放在一起,引入资源包的时候就可以使用一个url

3rd party:hosted类型,我们自己的不希望外部的人访问到的包,都放在这个仓库

central:proxy类型,我们在私有仓库找不到的包,会通过上面的映射地址去下载即中央仓库下载,下载下来的包都会放在这个仓库中。

releases:hosted类型,一般放置自己项目中发布的构建,通常是release版本

snapshots:hosted类型,发布一些非release版本,非稳定版本。

当然这些都是默认的库,你也可以自己建库,这里不再细讲。

如何上传自己的包:







四:maven的简单配置

maven最常规的用法是作为管理包的工具

在pom.xml文件中配置dependencies标签将自动导入jar包。

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependencies>


如果要使用私有仓库,在要使用私有仓库的项目pom文件中,添加

<repositories>
<repository>
<id>nexus</id>
<name>nexus name</name>
<url>htttp://10.66.106.21:8081/nexus/content/group/public</url>
<releases>
<enable>true</enable>
</releases>
<!--默认是关闭的-->
<snapshots>
<enable>true</enable>
</snapshots>
<repository>
</repositories>


如果需要在多个项目中使用私有仓库,
4000
可以直接在maven的setting文件中配置

<profiles>
<profile>
<id>nexusProfile</id>
<repositories>
<repository>
<id>nexus</id>
<name>Nexus repository</name>
<url>http://10.66.106.21:8081/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
</profile>

</profiles>
<!--只有激活才生效-->
<activeProfiles>
<activeProfile>nexusProfile</activeProfile>
</activeProfiles>


说句题外话,如果你不想更改系统的setting文件也可以另外建一个本地仓库,以eclipse为例,在window->preferences->maven->user Setting下更改本地仓库目录,复制一份setting文件到新目录下,更改里面的本地仓库目录:

<localRepository>D:\debug\apache-maven-3.5.0\localmaven\repo</localRepository>


如果想要覆盖中央仓库的默认地址,强制依赖的东西都到nexus中找,即使nexus关闭也不会到中央仓库下载可以在setting文件中设置镜像:

<mirror>
<id>nexusMirror</id>
<mirrorOf>,nexus,central</mirrorOf>
<name>Human Readable Name for this Mirror.</name>
<url>http://10.66.106.21:8081/nexus/content/groups/public</url>
</mirror>


因为网络原因下载不了或者缓慢,也可以在镜像标签更换中央仓库的地址。

五:maven的多项目管理

maven也常常应用于多模块的项目。

搭建多模块的项目,必须要有一个packageing为pom的根目录,新建一个新的maven项目,如parent

<modelVersion>4.0.0</modelVersion>

<groupId>com.example</groupId>
<artifactId>parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<name>parent</name>
<url>http://maven.apache.org</url>


然后新建maven module,如core,web1,web2,webMain等,web1,web2,webMain模块是web项目,其他都是java项目。以web1模块的pom为例:

<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.example</groupId>
<artifactId>parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>com.example</groupId>
<artifactId>web1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>web Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.example</groupId>
<artifactId>core</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
<build>
<finalName>web1</finalName>
</build>


parent标签下是我们的主管理项目

groupId是项目组的编号是独一无二的。

artifactId是项目ID,通常是项目的名字

versosn是项目的版本号

packing是整个项目的类型

dependencis是项目的依赖,一旦用到的jar包或者其他项目模块都要在上面声明。

一旦创建了子模块,在父模块中要声明如:

<modules>
<module>core</module>
<module>web1</module>
<module>web2</module>
<module>webMain</module>
<module>core</module>
</modules>


webMain模块是统合所有的web模块,在webMain模块中需要添加web1,web2的依赖,并且声明其打包类型

<dependency>
<groupId>com.example</groupId>
<artifactId>web1</artifactId>
<version>${project.version}</version>
<type>war</type>
</dependency>
<dependency>
<groupId>com.example</groupId>
<artifactId>web2</artifactId>
<version>${project.version}</version>
<type>war</type>
</dependency>


然后调用maven的命令,对parent这个项目打包,使用maven build命令统一使用各种命令如clean install 先清空在打包,在项目各个项目目录下会产生各自的jar包或者war包。

六:maven的常用命令

maven可以对一些操作以命令的形式统一执行以eclipse为例:

项目右键run as->maven->maven build中执行多条语句:

package 项目打包

clean 删除target目录下编译的内容

compile 编译项目

test 运行测试

install 在本地Repository中安装jar/war

deploy 发布到远程仓库(需要配置插件)

网上还有很多插件的命令如启动tomact等等。。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  nexus maven 项目管理