目前的工作一直都是研发人员把war包已经打好,然后我们运维通过手动或者脚本,将war包放到tomcat指定目录下来发布更新程序。而想实现devops,自己还是需要了解java web项目的一些基础知识的。
1. maven安装
下载对应的安装包:
http://maven.apache.org/download.cgi
我们这里是用:apache-maven-3.6.0-bin.tar.gz
解压并复制到你的路径,配置环境变量:
$ tar zxf apache-maven-3.6.0-bin.tar.gz
$ mv apache-maven-3.6.0 /usr/local/
$ vim /etc/profile
#在最后增加如下内容
export MAVEN_HOME=/usr/local/apache-maven-3.6.0
export PATH=${PATH}:${MAVEN_HOME}/bin
# 环境变量立即生效
$ source /etc/profile
# 验证
$ mvn -v
# 输出类似如下内容
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加速仓库:
$ vim /usr/local/apache-maven-3.6.0/conf/settings.xml
# 找到<mirror></mirrors>,在其中新增一个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新建一个web项目
新建一个空白文件夹java-test
,进入该文件夹,并运行如下命令:
mvn archetype:generate -DgroupId=com.biezhi -DartifactId=chen.web -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false
得到类似如下输出:
...省略
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] ------------------------------------------------------------------------
主要是去下载一些依赖pom文件和jar包。-DgroupId=com.biezhi -DartifactId=chen.web
可以自己指定,一般是公司名称,项目名称。
之后会在该目录下生成一个chen.web
目录,其目录结构如下:
[root@centos7-test java-test]# tree chen.web/
chen.web/
├── pom.xml
└── src
└── main
├── resources
└── webapp
├── index.jsp
└── WEB-INF
└── web.xml
在chen.web/
目录下执行mvn compile
,进行编译:
...省略
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] ------------------------------------------------------------------------
新目录结构如下:
[root@centos7-test chen.web]# tree
.
├── pom.xml
├── src
│ └── main
│ ├── resources
│ └── webapp
│ ├── index.jsp
│ └── WEB-INF
│ └── web.xml
└── target
└── classes
在chen.web/
目录下执行mvn package
,进行打包:
...省略
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] ------------------------------------------------------------------------
新目录结构如下:
[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目录下,启动tomcat,并访问测试:
[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包可以正常使用。
至此,一个简单的web项目打包发布完成。
网友评论