目前的工作一直都是研發(fā)人員把war包已經(jīng)打好侄泽,然后我們運(yùn)維通過(guò)手動(dòng)或者腳本懈贺,將war包放到tomcat指定目錄下來(lái)發(fā)布更新程序犀忱。而想實(shí)現(xiàn)devops钞它,自己還是需要了解java web項(xiàng)目的一些基礎(chǔ)知識(shí)的拜银。
1. maven安裝
下載對(duì)應(yīng)的安裝包:
http://maven.apache.org/download.cgi
我們這里是用:apache-maven-3.6.0-bin.tar.gz
解壓并復(fù)制到你的路徑,配置環(huán)境變量:
$ tar zxf apache-maven-3.6.0-bin.tar.gz
$ mv apache-maven-3.6.0 /usr/local/
$ vim /etc/profile
#在最后增加如下內(nèi)容
export MAVEN_HOME=/usr/local/apache-maven-3.6.0
export PATH=${PATH}:${MAVEN_HOME}/bin
# 環(huán)境變量立即生效
$ source /etc/profile
# 驗(yàn)證
$ mvn -v
# 輸出類(lèi)似如下內(nèi)容
Apache Maven 3.6.0 (97c98ec64a1fdfee7767ce5ffb20918da4f719f3; 2018-10-25T02:41:47+08:00)
Maven home: /usr/local/apache-maven-3.6.0
Java version: 1.8.0_181, vendor: Oracle Corporation, runtime: /usr/java/jdk1.8.0_181-amd64/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "3.10.0-862.11.6.el7.x86_64", arch: "amd64", family: "unix"
配置maven加速倉(cāng)庫(kù):
$ vim /usr/local/apache-maven-3.6.0/conf/settings.xml
# 找到<mirror></mirrors>遭垛,在其中新增一個(gè)mirror
<mirror>
<id>nexus-aliyun</id>
<mirrorOf>*</mirrorOf>
<name>Nexus aliyun</name>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
</mirror>
2. 用maven新建一個(gè)web項(xiàng)目
新建一個(gè)空白文件夾java-test
尼桶,進(jìn)入該文件夾,并運(yùn)行如下命令:
mvn archetype:generate -DgroupId=com.biezhi -DartifactId=chen.web -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false
得到類(lèi)似如下輸出:
...省略
Downloading from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/archetypes/maven-archetype-webapp/1.0/maven-archetype-webapp-1.0.jar
Downloaded from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/archetypes/maven-archetype-webapp/1.0/maven-archetype-webapp-1.0.jar (3.9 kB at 28 kB/s)
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Old (1.x) Archetype: maven-archetype-webapp:1.0
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: basedir, Value: /root/java-test
[INFO] Parameter: package, Value: com.biezhi
[INFO] Parameter: groupId, Value: com.biezhi
[INFO] Parameter: artifactId, Value: chen.web
[INFO] Parameter: packageName, Value: com.biezhi
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] project created from Old (1.x) Archetype in dir: /root/java-test/chen.web
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 23.476 s
[INFO] Finished at: 2018-11-07T10:29:37+08:00
[INFO] ------------------------------------------------------------------------
主要是去下載一些依賴(lài)pom文件和jar包锯仪。-DgroupId=com.biezhi -DartifactId=chen.web
可以自己指定泵督,一般是公司名稱(chēng),項(xiàng)目名稱(chēng)庶喜。
之后會(huì)在該目錄下生成一個(gè)chen.web
目錄小腊,其目錄結(jié)構(gòu)如下:
[root@centos7-test java-test]# tree chen.web/
chen.web/
├── pom.xml
└── src
└── main
├── resources
└── webapp
├── index.jsp
└── WEB-INF
└── web.xml
在chen.web/
目錄下執(zhí)行mvn compile
救鲤,進(jìn)行編譯:
...省略
Downloaded from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/shared/maven-shared-utils/0.1/maven-shared-utils-0.1.jar (155 kB at 140 kB/s)
Downloaded from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/com/google/collections/google-collections/1.0/google-collections-1.0.jar (0 B at 0 B/s)
Downloaded from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/junit/junit/3.8.2/junit-3.8.2.jar (121 kB at 90 kB/s)
[INFO] No sources to compile
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 21.721 s
[INFO] Finished at: 2018-11-07T11:11:26+08:00
[INFO] ------------------------------------------------------------------------
新目錄結(jié)構(gòu)如下:
[root@centos7-test chen.web]# tree
.
├── pom.xml
├── src
│ └── main
│ ├── resources
│ └── webapp
│ ├── index.jsp
│ └── WEB-INF
│ └── web.xml
└── target
└── classes
在chen.web/
目錄下執(zhí)行mvn package
,進(jìn)行打包:
...省略
Downloaded from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/com/thoughtworks/xstream/xstream/1.3.1/xstream-1.3.1.jar (431 kB at 1.1 MB/s)
Downloaded from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/codehaus/plexus/plexus-utils/3.0/plexus-utils-3.0.jar (226 kB at 461 kB/s)
[INFO] Packaging webapp
[INFO] Assembling webapp [chen.web] in [/root/java-test/chen.web/target/chen.web]
[INFO] Processing war project
[INFO] Copying webapp resources [/root/java-test/chen.web/src/main/webapp]
[INFO] Webapp assembled in [28 msecs]
[INFO] Building war: /root/java-test/chen.web/target/chen.web.war
[INFO] WEB-INF/web.xml already added, skipping
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 9.646 s
[INFO] Finished at: 2018-11-07T11:14:49+08:00
[INFO] ------------------------------------------------------------------------
新目錄結(jié)構(gòu)如下:
[root@centos7-test chen.web]# tree
.
├── pom.xml
├── src
│ └── main
│ ├── resources
│ └── webapp
│ ├── index.jsp
│ └── WEB-INF
│ └── web.xml
└── target
├── chen.web
│ ├── index.jsp
│ ├── META-INF
│ └── WEB-INF
│ ├── classes
│ └── web.xml
├── chen.web.war
├── classes
└── maven-archiver
└── pom.properties
拷貝war包到tomcat目錄下秩冈,啟動(dòng)tomcat本缠,并訪問(wèn)測(cè)試:
[root@centos7-test chen.web]# cp target/chen.web.war /usr/local/apache-tomcat-8.5.34/webapps/
[root@centos7-test chen.web]# /usr/local/apache-tomcat-8.5.34/bin/startup.sh
[root@centos7-test chen.web]# curl http://127.0.0.1:8080/chen.web/index.jsp
<html>
<body>
<h2>Hello World!</h2>
</body>
</html>
war包可以正常使用。
至此入问,一個(gè)簡(jiǎn)單的web項(xiàng)目打包發(fā)布完成搓茬。