Maven 部署

Maven 是一个项目管理工具,可以对 Java项目进行构建、解决打包依赖等。

POM( Project Object Model,项目对象模型 ) 是 Maven 工程的基本工作单元,是一个 XML 文件,包含了项目的基本信息,用于描述项目如何构建,声明项目依赖等,在执行任务或目标时,Maven 会在当前目录中查找 pom 文件,通过读取pom文件获取所需的配置信息,然后执行目标。每个maven项目都有一个pom.xml文件。

pom.xml 文件中可以指定以下配置:

项目依赖
插件
执行目标
项目构建 profile
项目版本
项目开发者列表 相关邮件列表信息
用<packaging> 指定项目打包形式,jar或者war

安装maven前必须安装java 环境:

Maven 3.3 要求 JDK 1.7 或以上
Maven 3.2 要求 JDK 1.6 或以上
Maven 3.0/3.1 要求 JDK 1.5 或以上

注意: Maven所安装JDK的版本不影响编译应用的所依赖的JDK版本,比如Maven安装JDK11,而java应用Jpress 可以使用JDK8

maven 二进制安装

[root@ubuntu2204 opt]#wget https://mirrors.tuna.tsinghua.edu.cn/apache/maven/maven-3/3.8.6/binaries/apache-maven-3.8.6-bin.tar.gz
--2022-12-14 22:16:34-- https://mirrors.tuna.tsinghua.edu.cn/apache/maven/maven-3/3.8.6/binaries/apache-maven-3.8.6-bin.tar.gz
正在解析主机 mirrors.tuna.tsinghua.edu.cn (mirrors.tuna.tsinghua.edu.cn)... 101.6.15.130, 2402:f000:1:400::2
正在连接 mirrors.tuna.tsinghua.edu.cn (mirrors.tuna.tsinghua.edu.cn)|101.6.15.130|:443... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度: 8676320 (8.3M) [application/octet-stream]
正在保存至: ‘apache-maven-3.8.6-bin.tar.gz’

apache-maven-3.8.6-bin.tar.gz 100%[==========================================================================================>] 8.27M 1.14MB/s 用时 7.6s

2022-12-14 22:16:42 (1.09 MB/s) - 已保存 ‘apache-maven-3.8.6-bin.tar.gz’ [8676320/8676320])

[root@ubuntu2204 opt]#tar xf apache-maven-3.8.6-bin.tar.gz -C /usr/local/
[root@ubuntu2204 opt]#cd
[root@ubuntu2204 ~]#ln -s /usr/local/apache-
apache-maven-3.8.6/ apache-tomcat-8.5.84/
[root@ubuntu2204 ~]#ln -s /usr/local/apache-maven-3.8.6/ /usr/local/maven
[root@ubuntu2204 ~]#echo 'PATH=/usr/local/maven/bin:$PATH' > /etc/profile.d/maven.sh
[root@ubuntu2204 ~]#echo 'export MAVEN_HOME=/usr/local/maven' >> /etc/profile.d/maven.sh
[root@ubuntu2204 ~]#. /etc/profile.d/maven.sh
[root@ubuntu2204 ~]#mvn -v
Apache Maven 3.8.6 (84538c9988a25aec085021c365c560670ad80f63)
Maven home: /usr/local/maven
Java version: 1.8.0_351, vendor: Oracle Corporation, runtime: /usr/local/jdk1.8.0_351/jre
Default locale: zh_CN, platform encoding: UTF-8
OS name: "linux", version: "5.15.0-56-generic", arch: "amd64", family: "unix"

#镜像加速
[root@ubuntu2204 ~]#vim /usr/local/maven/conf/settings.xml
<mirrors>
<!--阿里云镜像-->
<mirror>
<id>nexus-aliyun</id>
<mirrorOf>*</mirrorOf>
<name>Nexus aliyun</name>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
</mirror>
</mirrors>

Maven工程构建的各个环节

  • clean: 以前编译得到的旧文件class字节码文件删除
  • compile:将java源程序编译成class字节码文件
  • test:自动测试,例如,自动调用junit程序
  • report:报告测试程序执行的结果
  • package:应用打包,动态Web工程打成war包,java工程打成jar包
  • install: 是指将打包得到的文件复制到仓库中指定的位置
  • deploy:将动态Web工程生成的war包复制到Servlet容器下,使其可以运行
进入到包含有“pom.xml”的路径,执行:
mvn clean install package
有的时候受到测试的干扰,导致无法正在进行编译,这时候可以选择跳过测试:
mvn clean install package -Dmaven.test.skip=true
#选项"-Dmaven.test.skip=true":跳过测试,并且不编译测试下的源代码
#选项"-DskipTests":不执行测试,但是会进行测试代码的编译
如果需要编译的代码很庞大,需要考虑对编译环境做一些处理,提成编译效率:
#启动多线程编译:
mvn -T 4 clean install package -Dmaven.test.skip=true
#分配编译的CPU 个数:
mvn -T 2C clean install package -Dmaven.test.skip=true
#启用多线程编译:
mvn clean install package -Dmaven.test.skip=true -Dmaven.compile.fork=true
所有的 Maven 都是建立在 JVM 上的,所以进行编译的时候还需要考虑JVM 参数优化
#如果是windows找到“maven/bin/mvn.cmd”,如果 linux 找到“maven/bin/mvn”,配置参数是:“MAVEN_OPTS”
#打开属性配置文件:vim /etc/profile 指定内存配置:export MAVEN_OPTS="-Xmx6g -Xms6g" 注意不要超过物理内存一半
#使配置立即生效:
source /etc/profile

案例


#编译安装 spring-boot 项目
[root@ubuntu1804 ~]#apt -y install maven git
#镜像加速
[root@ubuntu1804 ~]#vim /etc/maven/settings.xml
<mirror>
<id>nexus-aliyun</id>
<mirrorOf>*</mirrorOf>
<name>Nexus aliyun</name>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
</mirror>
[root@ubuntu1804 ~]#cd /data
[root@ubuntu1804 data]#git clone https://gitee.com/lbtooth/spring-boothelloWorld.
git
[root@ubuntu1804 data]#cd spring-boot-helloWorld/
[root@ubuntu1804 spring-boot-helloWorld]#ls
deploy Dockerfile Jenkinsfile LICENSE pom.xml README.md src
[root@ubuntu1804 spring-boot-helloWorld]#ls src
main test
#编译
[root@ubuntu1804 spring-boot-helloWorld]#mvn -T 4 clean package -
Dmaven.test.skip=true
......
[INFO] --- maven-jar-plugin:3.1.1:jar (default-jar) @ spring-boot-helloworld ---
[INFO] Building jar: /root/spring-boot-helloWorld/target/spring-boot-helloworld-
0.9.0-SNAPSHOT.jar
[INFO]
[INFO] --- spring-boot-maven-plugin:2.1.3.RELEASE:repackage (repackage) @
spring-boot-helloworld ---
[INFO] Replacing main artifact with repackaged archive
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 57.183 s (Wall Clock)
[INFO] Finished at: 2022-06-15T09:53:14+08:00
[INFO] ------------------------------------------------------------------------
[root@ubuntu1804 spring-boot-helloWorld]#ls target/
classes maven-archiver spring-boothelloworld-
0.9.0-SNAPSHOT.jar.original
generated-sources maven-status surefirereports
generated-test-sources spring-boot-helloworld-0.9.0-SNAPSHOT.jar test-classes
#默认为8080端口
[root@ubuntu1804 spring-boot-helloWorld]#java -jar target/spring-boothelloworld-
0.9.0-SNAPSHOT.jar --server.port=8888
[root@ubuntu1804 ~]#ss -ntl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
Process
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 100 127.0.0.1:25 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 100 *:8888 *:*
LISTEN 0 100 [::1]:25 [::]:*
[root@ubuntu1804 ~]#curl 127.0.0.1:8888
Hello Spring Boot 2.0!
[root@ubuntu1804 ~]#curl 127.0.0.1:8888/hello
Hello World

#编译 java 程序 Jpress
#下载源码
[root@rocky8 ~]#git clone https://gitee.com/JPressProjects/jpress.git
#或者
[root@rocky8 ~]#wget wget -O jpress-v4.2.0.tar.gz
'https://gitee.com/JPressProjects/jpress/repository/archive/v4.2.0?
format=tar.gz'
[root@rocky8 ~]#tar xf jpress-v4.2.0.tar.gz
[root@rocky8 ~]#cd jpress-v4.2.0/
[root@rocky8 jpress-v4.2.0]#ls
changes.txt docker Dockerfile jpress-commons jpress-service
jpress-template module-article pom.xml starter-tomcat
codegen docker-build.sh install.sh jpress-core jpress-serviceprovider
jpress-web module-page README.md upgrade.sh
doc docker-compose.yml jpress-addons jpress-model jpress.sql
LICENSE module-product starter
[root@rocky8 jpress-v4.2.0]#mvn clean install package -Dmaven.test.skip=true
...
[INFO] module-product-search-db ........................... SUCCESS [ 0.095 s]
[INFO] module-product-search-lucene ....................... SUCCESS [ 0.101 s]
[INFO] module-product-search-es ........................... SUCCESS [ 0.160 s]
[INFO] module-product-search-opensearch ................... SUCCESS [ 0.120 s]
[INFO] module-product-service-provider .................... SUCCESS [ 0.228 s]
[INFO] module-product-web ................................. SUCCESS [ 0.274 s]
[INFO] starter ............................................ SUCCESS [01:20 min]
[INFO] starter-tomcat 4.0 ................................. SUCCESS [ 8.736 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 03:00 min
[INFO] Finished at: 2021-12-15T18:18:06+08:00
[INFO] ------------------------------------------------------------------------
[root@rocky8 jpress-v4.2.0]#cp starter-tomcat/target/starter-tomcat-4.0.war
/usr/local/tomcat/webapps/jpress.war

私有仓库 Nexus

32-Java 程序编译及私有仓库Nexus_ubuntu

编译安装Nexus

[root@ubuntu2204 ~]#java --version
openjdk 11.0.17 2022-10-18
OpenJDK Runtime Environment (build 11.0.17+8-post-Ubuntu-1ubuntu222.04)
OpenJDK 64-Bit Server VM (build 11.0.17+8-post-Ubuntu-1ubuntu222.04, mixed mode, sharing)
[root@ubuntu2204 ~]#cd /opt/
[root@ubuntu2204 opt]#ll
总用量 10176
drwxr-xr-x 3 root root 4096 12月 14 22:16 ./
drwxr-xr-x 20 root root 4096 12月 10 00:33 ../
-rw-r--r-- 1 root root 1679 12月 8 02:30 1index.html
-rw-r--r-- 1 root root 8676320 6月 7 2022 apache-maven-3.8.6-bin.tar.gz
drwxrwxr-x 8 wang wang 12288 12月 10 23:54 memcached-1.6.17/
-rw-r--r-- 1 root root 1713186 8月 27 08:37 memcached-1.6.17.tar.gz
[root@ubuntu2204 opt]#rz -E
rz waiting to receive.
[root@ubuntu2204 opt]#ll
总用量 215596
drwxr-xr-x 3 root root 4096 12月 15 18:49 ./
drwxr-xr-x 20 root root 4096 12月 10 00:33 ../
-rw-r--r-- 1 root root 1679 12月 8 02:30 1index.html
-rw-r--r-- 1 root root 8676320 6月 7 2022 apache-maven-3.8.6-bin.tar.gz
drwxrwxr-x 8 wang wang 12288 12月 10 23:54 memcached-1.6.17/
-rw-r--r-- 1 root root 1713186 8月 27 08:37 memcached-1.6.17.tar.gz
-rw-r--r-- 1 root root 210342986 12月 10 17:45 nexus-3.43.0-01-unix.tar.gz
[root@ubuntu2204 opt]#tar xf nexus-3.43.0-01-unix.tar.gz -C /usr/local/
[root@ubuntu2204 opt]#cd /usr/local/
[root@ubuntu2204 local]#file nexus/bin/nexus
nexus/bin/nexus: POSIX shell script, ASCII text executable, with very long lines (862)
[root@ubuntu2204 local]#vim nexus/bin/nexus.rc
[root@ubuntu2204 local]#cat nexus/bin/nexus.rc
run_as_user="root"
[root@ubuntu2204 ~]#vim /lib/systemd/system/nexus.service
[root@ubuntu2204 ~]#cat /lib/systemd/system/nexus.service
[Unit]
Descriptinotallow=nexus service
After=network.target
[Service]
Type=forking
LimitNOFILE=65536
ExecStart=/usr/local/nexus/bin/nexus start
ExecStop=/usr/local/nexus/bin/nexus stop
User=root
#User=nexus
Restart=on-abort
[Install]
WantedBy=multi-user.target
[root@ubuntu2204 ~]#systemctl daemon-reload
[root@ubuntu2204 ~]#systemctl enable --now nexus.service
Created symlink /etc/systemd/system/multi-user.target.wants/nexus.service → /lib/systemd/system/nexus.service.
#查看JVM配置文件
[root@ubuntu2204 ~]#cat /usr/local/nexus/bin/nexus.vmoptions
-Xms2703m 默认内存需要2.7G 所以最少准备4G内存 可以改小,我这里改到Xms和Xmx 都是512m
-Xmx2703m
......

[root@ubuntu2204 ~]#ln -s /usr/local/nexus-3.43.0-01/ /usr/local/nexus
[root@ubuntu2204 ~]#ln -s /usr/local/nexus/bin/nexus /usr/bin/
[root@ubuntu2204 ~]#file /usr/local/nexus/bin/nexus
/usr/local/nexus/bin/nexus: POSIX shell script, ASCII text executable, with very long lines (862)
[root@ubuntu2204 ~]#nexus run
WARNING: ************************************************************
WARNING: Detected execution as "root" user. This is NOT recommended!
WARNING: ************************************************************
Nexus data directory already in use: /usr/local/sonatype-work/nexus3
[root@ubuntu2204 ~]#nexus start
WARNING: ************************************************************
WARNING: Detected execution as "root" user. This is NOT recommended!
WARNING: ************************************************************
Starting nexus
[root@ubuntu2204 ~]#nexus status
WARNING: ************************************************************
WARNING: Detected execution as "root" user. This is NOT recommended!
WARNING: ************************************************************
nexus is running.
[root@ubuntu2204 ~]#ss -ntlp|grep java
LISTEN 0 50 0.0.0.0:8081 0.0.0.0:* users:(("java",pid=1519,fd=815))
LISTEN 0 1 127.0.0.1:35849 0.0.0.0:* users:(("java",pid=1519,fd=116))
LISTEN 0 100 *:8080 *:* users:(("java",pid=782,fd=56))
LISTEN 0 1 [::ffff:127.0.0.1]:8005 *:* users:(("java",pid=782,fd=64))

32-Java 程序编译及私有仓库Nexus_Nexus_02


[root@ubuntu2204 ~]#cat /usr/local/sonatype-work/nexus3/admin.password
d6ccd108-3523-4f36-a768-5f4ea172f239

生产建议打开匿名访问功能,无需登录就可以下载资源

32-Java 程序编译及私有仓库Nexus_maven_03

默认仓库有以下 type 类型

Hosted:本地仓库,通常我们会部署自己的构件到这一类型的仓库,比如公司的第三方库
Proxy:代理仓库,它们被用来代理远程的公共仓库,如maven 中央仓库(官方仓库)
Group:仓库组,用来合并多个 hosted/proxy 仓库,当你的项目希望在多个repository 使用资源时就
不需要多次引用了,只需要引用一个 group 即可

案例 使用 Nexus 构建私有 Yum 和 Apt 仓库

创建自定义存储存放仓库数据
[root@ubuntu1804 ~]#mkdir -p /data/blobs/

32-Java 程序编译及私有仓库Nexus_maven_04

创建 Yum 仓库

32-Java 程序编译及私有仓库Nexus_ubuntu_05

32-Java 程序编译及私有仓库Nexus_Nexus_06

32-Java 程序编译及私有仓库Nexus_maven_07

32-Java 程序编译及私有仓库Nexus_maven_08

将 Nexus 服务仓库配置为yum仓库

[root@centos7 ~]#cat > /etc/yum.repos.d/zabbix.repo <<EOF
> [zabbix-nexus]
> name=zabbix-nexus
> baseurl=http://10.0.0.100:8081/repository/zabbix-5.0-yum-centos7/
> gpgcheck=0
> EOF
[root@centos7 ~]#yum repolist
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base:
repo id repo name status
base/7/x86_64 CentOS 10,070
epel/7/x86_64 EPEL 13,523
extras/7/x86_64 extras 451
zabbix-nexus zabbix-nexus 100
repolist: 24,144
[root@centos7 ~]#yum install --downloadonly sl
[root@centos7 ~]#yum -y install zabbix-agent
#如果是Centos8,下载包存放在/var/cache/dnf/下面
[root@centos7 ~]#ls /var/cache/yum/x86_64/7/epel/packages/
sl-5.02-1.el7.x86_64.rpm
#在nexus服务器存储目录下自动下载相关文件
[root@ubuntu1804 ~]#ls /data/blobs/blob-zabbix-5.0-yum-centos7
899D2379-D9D78DB2-4B816C59-1418D257-7A0183F0-deletions.index content
899D2379-D9D78DB2-4B816C59-1418D257-7A0183F0-metrics.properties
metadata.properties

刷新Nexus网址即可看到yum源中已经缓存了相关的包

32-Java 程序编译及私有仓库Nexus_Nexus_09

maven和apt 也使用相同的方式可以将下载源集中到Nexus上去管理。


我是moore,大家一起加油!