首页 > 代码库 > 利用apache+svn+jenkins+maven 实现java环境的自动化构建和部署(四)(网内首发超详细版)

利用apache+svn+jenkins+maven 实现java环境的自动化构建和部署(四)(网内首发超详细版)

6安装maven

下载地址http://maven.apache.org/download.cgi

解压安装

unzip –o apache-maven-3.2.1-bin.zip

mv apache-maven-3.2.1 maven


配置环境变量

MAVEN_HOME=/usr/local/maven

export PATH=$MAVEN_HOME/bin:$PATH

7 java环境自动构建部署


7.1 新建job


输入名称如下图


wKiom1Nxy4aDp2YRAABsOJKAvXs812.jpg

输入SVN服务器地址



wKioL1Nxy4TReWkLAAEG37k2Zyk638.jpg


注意:上面已经认证过了,因此没有报错,当第一次会提示点enter_credential输入svn的用户名密码,后如下图,不再报错。


pom.xml文件设定


wKiom1Nxy8virC8GAAEmMFzpvxI829.jpg


这里将在6.2详细讲述这里的pom.xml配置过程,因为很多人新手配置时都会范迷糊,pom.xml定义所有的项目配置信息通过该文件,Maven可以管理项目的整个声明周期,包括编译,构建,测试,发布,报告等等

7.2利用maven命令创建web项目


命令格式mvn archetype:generate -DgroupId={project-packaging} -DartifactId={project-name} -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false


cd /data/webapp/mywebapp


mvn archetype:generate -DgroupId=com.myweb -DartifactId=mywebapp -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false


#ls                                          生成一个pom.xml和src目录


pom.xml  src


Maven的Web程序目录结构


# cd src


# tree


.


`-- main


   |-- resources


   `-- webapp


       |-- WEB-INF


       |   `-- web.xml


       `-- index.jsp



4 directories, 2 files


执行构建


# mvn install


[INFO] Scanning for projects...


[INFO]


[INFO] Using the builder org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder with a thread count of 1


[INFO]                                                                        


[INFO] ------------------------------------------------------------------------


[INFO] Building mywebapp Maven Webapp 1.0-SNAPSHOT


[INFO] ------------------------------------------------------------------------


[INFO]


[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ mywebapp ---


[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!


[INFO] Copying 0 resource


[INFO]


[INFO] --- maven-compiler-plugin:2.5.1:compile (default-compile) @ mywebapp ---


[INFO] No sources to compile


[INFO]


[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ mywebapp ---


[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!


[INFO] skip non existing resourceDirectory /data/webapp/mywebapp/src/test/resources


[INFO]


[INFO] --- maven-compiler-plugin:2.5.1:testCompile (default-testCompile) @ mywebapp ---


[INFO] No sources to compile


[INFO]


[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ mywebapp ---


[INFO] No tests to run.


[INFO]


[INFO] --- maven-war-plugin:2.2:war (default-war) @ mywebapp ---


[INFO] Packaging webapp


[INFO] Assembling webapp [mywebapp] in [/data/webapp/mywebapp/target/mywebapp]


[INFO] Processing war project


[INFO] Copying webapp resources [/data/webapp/mywebapp/src/main/webapp]


[INFO] Webapp assembled in [33 msecs]


[INFO] Building war: /data/webapp/mywebapp/target/mywebapp.war


[INFO] WEB-INF/web.xml already added, skipping


[INFO]


[INFO] --- maven-install-plugin:2.4:install (default-install) @ mywebapp ---


[INFO] Installing /data/webapp/mywebapp/target/mywebapp.war to /root/.m2/repository/com/myweb/mywebapp/1.0-SNAPSHOT/mywebapp-1.0-SNAPSHOT.war


[INFO] Installing /data/webapp/mywebapp/pom.xml to /root/.m2/repository/com/myweb/mywebapp/1.0-SNAPSHOT/mywebapp-1.0-SNAPSHOT.pom


[INFO] ------------------------------------------------------------------------


[INFO] BUILD SUCCESS


[INFO] ------------------------------------------------------------------------


[INFO] Total time: 1.689 s


[INFO] Finished at: 2014-04-16T17:45:42+08:00


[INFO] Final Memory: 10M/240M


[INFO] ------------------------------------------------------------------------


生成了一个target目录


# pwd


/data/webapp/mywebapp/target


[root@s2 target]# tree


.


|-- classes


|-- maven-archiver


|   `-- pom.properties


|-- mywebapp


|   |-- META-INF


|   |-- WEB-INF


|   |   |-- classes


|   |   `-- web.xml


|   `-- index.jsp


`-- mywebapp.war



6 directories, 4 files


另外这里/root/.jenkins/jobs/test1/workspace/target/mywebapp.war也生成了一份。


pom.xml  src  target导入到对应项目的SVN版本库在按照上图配置即可。

最后一部分为FAQ需要的请:http://auskangaroo.blog.51cto.com/740826/1410563


本文出自 “技术改变未来” 博客,请务必保留此出处http://auskangaroo.blog.51cto.com/740826/1410558