1 简介
Maven是一个项目管理和综合工具。Maven提供给开发人员构建一个完整的生命周期框架。开发团队可以自动完成该项目的基础设施建设,Maven使用标准的目录结构和默认构建生命周期。
它是Apache的开源项目,主要服务于JAVA平台的构建、依赖管理、项目管理。
Project Object Model,项目对象模型。通过xml格式保存的pom.xml文件。
1.1 Jenkins集成Java流程图
2 Maven安装
2.1 下载解压
在https://maven.apache.org/download.cgi页面下载压缩文件
下载后,将解压的目录
mv
到/usr/local/
下,做一个软链接
[root@jenkins ~]$ wget https://mirrors.bfsu.edu.cn/apache/maven/maven-3/3.6.3/binaries/apache-maven-3.6.3-bin.tar.gz
[root@jenkins ~]$ tar -xf apache-maven-3.6.3-bin.tar.gz
[root@jenkins ~]$ mv apache-maven-3.6.3 /usr/local/
[root@jenkins local]$ ln -s apache-maven-3.6.3/ maven
2.2 配置环境变量
配置/etc/profile
文件,在最后一行添加
export PATH=/usr/local/maven/bin:$PATH
2.3 安装JDK依赖
Maven 3.3+ 需要 JDK 1.7 或更高的版本,安装openjdk
yum install -y java-1.8.0-openjdk
3 常用命令
以下命令需要在项目目录下运行
mvn package
:打包,源码编译后,用合适的格式(例如JAR)对编译后的源码进行打包
mvn clean
:清除上次的编译结果
4 Maven仓库管理
4.1 本地仓库
Maven 的本地仓库,在安装 Maven 后并不会创建,它是在第一次执行 maven 命令的时候才被创建。
运行 Maven 的时候,Maven 所需要的任何构件都是直接从本地仓库获取的。如果本地仓库没有,它会首先尝试从远程仓库下载构件至本地仓库,然后再使用本地仓库的构件。
默认情况下,不管Linux还是 Windows,每个用户在自己的用户目录下都有一个路径名为 .m2/respository/ 的仓库目录。
Maven 本地仓库默认被创建在 %USER_HOME%
目录下。要修改默认位置,在 %M2_HOME%\conf
目录中的 Maven 的 settings.xml
文件中定义另一个路径。
4.2 使用阿里云仓库
配置maven程序目录下的conf/settings.xml
文件,当前机器使用的文件是/usr/local/maven/conf/settings.xml
,mirrors
部分添加aliyun
仓库
<mirrors>
<!-- mirror
| Specifies a repository mirror site to use instead of a given repository. The repository that
| this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used
| for inheritance and direct lookup purposes, and must be unique across the set of mirrors.
|
<mirror>
<id>mirrorId</id>
<mirrorOf>repositoryId</mirrorOf>
<name>Human Readable Name for this Mirror.</name>
<url>http://my.repository.com/repo/path</url>
</mirror>
-->
<mirror>
<id>nexus-aliyun</id>
<mirrorOf>*</mirrorOf>
<name>Nexus aliyun</name>
<url>https://maven.aliyun.com/repository/public</url>
</mirror>
</mirrors>
4.3 配置Nexus私服
私服是指私有服务器,是架设在局域网的一种特殊的远程仓库,目的是代理远程仓库及部署第三方构建。有了私服之后,当 Maven 需要下载构件时,直接请求私服,私服上存在则下载到本地仓库;否则,私服请求外部的远程仓库,将构件下载到私服,再提供给本地仓库下载。
Nexus是一个强大的Maven仓库管理器,它极大地简化了本地内部仓库的维护和外部仓库的访问。如果使用了公共的Maven仓库服务器,可以从Maven中央仓库下载所需要的构件(Artifact),但这通常不是一个好的做法。正常做法是在本地架设一个本地Maven仓库服务器,利用Nexus私服可以只在一个地方就能够完全控制访问和部署在你所维护仓库中的每个Artifact。
4.3.1 下载nexus
下载页面https://www.sonatype.com/nexus/repository-oss-download,将下载的tar包解压,并放到/usr/local
目录下
tar xf nexus-3.29.2-02-unix.tar.gz
mv nexus-3.29.2-02 /usr/local
制作软链接,并创建工作目录sonatype-work
,Nexus的构件仓库都保存在sonatype-work
目录中。在nexus/bin/nexus.vmoptions
文件,可以配置工作目录位置。
cd /usr/local
ln -s nexus-3.29.2-02 nexus
mkdir sonatype-work
启动nexus
cd /usr/local/nexus/bin
./nexus start
#查看nexus状态
./nexus status
4.3.2 安装JDK依赖
安装openjdk
4.3.3 配置nexus页面
nexus默认使用TCP 8081端口,要更改默认端口,修改目录下的etc/nexus-default.properties
文件
application-port=8081
默认用户名是admin,密码在sonatype-work/nexus3/admin.password
文件中,本例在/usr/local/sonatype-work/nexus3/admin.password
文件中。
登陆后,修改nexus中maven-central
的proxy
的远程仓库为阿里云,这样当nexus本地仓库没有文件时,从阿里云远程拉取,默认是从maven的官方仓库。
同时记录下本地nexus的
maven-public
仓库的URL地址,以便后续maven更改仓库地址4.3.4 更改maven使用nexus仓库
修改/usr/local/maven/conf/settings.xml
文件,找到<servers>
标签,添加nexus认证信息
<servers>
<server>
<id>my-nexus-releases</id>
<username>admin</username>
<password>LNhnsya1</password>
</server>
<server>
<id>my-nexus-snapshot</id>
<username>admin</username>
<password>LNhnsya1</password>
</server>
<!-- server
| Specifies the authentication information to use when connecting to a particular server, identified by
| a unique name within the system (referred to by the 'id' attribute below).
|
| NOTE: You should either specify username/password OR privateKey/passphrase, since these pairings are
| used together.
|
<server>
<id>deploymentRepo</id>
<username>repouser</username>
<password>repopwd</password>
</server>
-->
<!-- Another sample, using keys to authenticate.
<server>
<id>siteServer</id>
<privateKey>/path/to/private/key</privateKey>
<passphrase>optional; leave empty if not used.</passphrase>
</server>
-->
</servers>
找到<mirrors>
标签,添加nexus仓库地址,地址为之前记录的maven-public
仓库的URL
<mirrors>
<!-- mirror
| Specifies a repository mirror site to use instead of a given repository. The repository that
| this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used
| for inheritance and direct lookup purposes, and must be unique across the set of mirrors.
|
<mirror>
<id>mirrorId</id>
<mirrorOf>repositoryId</mirrorOf>
<name>Human Readable Name for this Mirror.</name>
<url>http://my.repository.com/repo/path</url>
</mirror>
-->
<mirror>
<id>nexus</id>
<mirrorOf>*</mirrorOf>
<name>Nexus local</name>
<url>http://192.168.50.44:8081/repository/maven-public/</url>
</mirror>
</mirrors>
找到<profiles>
标签,添加nexus仓库信息
<profiles>
<profile>
<id>nexus</id>
<!--Enable snapshots for the built in central repo to direct -->
<!--all requests to nexus via the mirror -->
<repositories>
<repository>
<id>central</id>
<url>http://192.168.50.44:8081/repository/maven-public/</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>central</id>
<url>http://192.168.50.44:8081/repository/maven-public/</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
找到<activeProfiles>
标签,启用nexus配置
<!-- activeProfiles
| List of profiles that are active for all builds.
|
<activeProfiles>
<activeProfile>alwaysActiveProfile</activeProfile>
<activeProfile>anotherAlwaysActiveProfile</activeProfile>
</activeProfiles>
-->
<activeProfiles>
<!--make the profile active all the time -->
<activeProfile>nexus</activeProfile>
</activeProfiles>
</settings>
5 一个java项目
5.1 推送源码到gitlab
gitlab上新建jeesns项目
推送源码到gitlab
cd existing_folder
git init
git remote add origin git@10.0.0.200:test/jeesns.git
git add .
git commit -m "Initial commit"
git push -u origin master
5.2 jenkins拉取代码、打包、推送
jenkins添加maven关联
新建jeesns项目
配置git仓库
配置项目构建
配置构建后的推送
5.3 web服务器环境准备
web服务器需安装tomcat,jdk
yum install -y tomcat tomcat-webapps tomcat-admin-webapps
yum install -y java-1.8.0-openjdk
maven打包的项目war包需要放在tomcat的webapp
目录下,yum安装的tomcat在目录/usr/share/tomcat/webapps
下。