研发管理>Maven应用介绍

1....maven能做些什么?.. 2 
 
1.1                  概念... 2 
 
1.2                  依附经管... 2 
 
1.3                  生成项目骨架... 2 
 
1.4                  主动化测试... 3 
 
1.5                  连气儿集成构建... 3 
 
2.... 景象设备.. 3 
 
2.1                  嵌入式运行tomcat. 3 
 
2.2                  外部运行tomcat. 3 
 
2.3                  Nexus创建... 4 
 
3.... 参考材料.. 5 
 
3.1                  重视事项... 5 
 
3.2                  Maven常用号令... 5 
 
3.3                  Tomcat- Maven-Plugin常用号令... 6 
 
3.4                  Maven-POM设置: 7 
 
3.5                  Maven-Settings设置: 7 
 
 
 
maven能做些什么? 
1.1      概念 
Maven是基于项目对象模型(POM),可以经由过程一小段描述信息来经管项目标编译,测试,打包,清理,安排等构建任务,以及生成测试呈报和javadoc文档的软件项目经管对象。。 
 
1.2      依附经管 
简单说就是同一经管jar包(也叫artifact:构件)以及之间的关系,包含版本定义和切换,构件的,经管构件依附和调剂构件冲突等。 
 
1.3      生成项目骨架 
已经有很多组织开辟了特定于某种类型的项目骨架,可以用来创建特定类型的项目原型。比如:创建基于seam框架的项目原型。 
 
archetype? 是一个内建插件,他的create任务将建树项目骨架 
 
可用项目骨架有: 
 
* maven-archetype-archetype 
 
* maven-archetype-j2ee-simple 
 
* maven-archetype-mojo 
 
* maven-archetype-portlet 
 
* maven-archetype-profiles (currently under development) 
 
* maven-archetype-quickstart 
 
* maven-archetype-simple (currently under development) 
 
* maven-archetype-site 
 
* maven-archetype-site-simple, and 
 
* maven-archetype-webapp 
 
1.4      主动化测试 
应用surefire插件来进行主动化测试,支撑Junit和TestNG测试框架 
 
1.5      连气儿集成构建 
于连气儿集成(continuous integeration)共同办事器,可以进行连气儿的构建和测试项目,还可以把守一些按期履行的任务, 生成JUnit/TestNG测试呈报。常用的ci办事器有:Eclipse 基金会的项目Hudson和Bamboo等。 
 
2       景象设备 
1) 、推敲到兼容题目,eclipse选择galileo(3.5.0),tomcat6.0.18,jdk 1.6.0_17,maven3.0.4. 
 
2) 、给eclipse集成maven:m2eclipse的安装,在eclipse的Help菜单中点击Install New Software, 
 
输入: http://m2eclipse.sonatype.org/sites/m2e,然后按提示安装. 
 
3) 、批改eclipse的eclipse.ini文件,便于maven应用,不然启动eclipse时会提示异常,添加(重视是两行,不消写到一行去): 
 
-vm 
 
C:/Java/jdk1.6.0_17/bin/javaw.exe 
 
2.1      嵌入式运行tomcat 
1)      、在pom.xml中添加: 
 
<plugin> 
 
<groupId>org.codehaus.mojo</groupId>        <artifactId>tomcat-maven-plugin</artifactId> 
 
<version>1.1</version> 
 
<configuration><path>/</path> <port>9090</port> <uriEncoding>UTF-8</uriEncoding>   </configuration> 
 
</plugin> 
 
2)、新建一个MavenBuild条目:在Goals中填写: tomcat:run deploy --DskipTests,意思是让maven驱动tomcat-maven-plugin插件以启动tomcat,然以不进行测试就安排项目到tomcat上. 
 
2.2      外部运行tomcat 
1) 、起首到apache-tomcat-6.0.18conf在此中增长一个admin用户,暗码是password,角色是经管员: 
 

<tomcat-users> <user username="admin" password="password" roles="manager"/> </tomcat-users> 

 
2)、 启动tomcat,然后接见 http://localhost:8080/manager/html,输入admin/password,若是呈现以下界面默示tomcat一切OK. 
 
3)、 在maven的setting.xml中定义本机的tomcat,增长如下内容(记住这里的id,等会要用到): 
 
<servers> 
 
<server> <id>tomcat</id> <username>admin</username><password>password</password> </server> 
 
</servers> 
 
4)、 在Eclipse中建树一个打包类型为war的maven项目:demo,并批改pom.xml文件 
 
格局如下: 
 
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> 
 
<modelVersion>4.0.0</modelVersion> <groupId>com.world</groupId> 
 
<artifactId>demo</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> 
 
<build> 
 
<plugins> <plugin> 
 
<groupId>org.codehaus.mojo</groupId> <artifactId>tomcat-maven-plugin</artifactId> <version>1.0-beta-1</version> 
 
<configuration> <url>http://localhost:8080/manager/html</url> <server>tomcat</server> </configuration> 
 
</plugin> </plugins> 
 
</build> 
 
</project> 
//<url>标签指明tomcat的经管器地址,<server>标签指明应用的是那个办事器。 
 
5)、 在项目中增长web.xml和一个测试文件HotDeplyTest.jsp。 HotDeplyTest.jsp内容随便写。 
 
6)、 Demo项目,鼠标右键,Run As 选择 Maven build,在Goals中写maven号令:package tomcat:redeploy 
 
,意思是打包,同时安排到tomcat上。 
 
7、 点击Run按钮,重视看Console,看看有没有错误,没有错误的话,接见: http://localhost:8080/demo/HotDeployTest.jsp, ,若是呈现界面,则默示一切万事大吉. 
 
2.3      Nexus创建 
1)、Nexus :http://nexus.sonatype.org/downloads/。 
 
Nexus 是典范的java Web应用,有两种安装包,一种是包含jetty容器的Bundle包,另一种是不包含web容器的war包。 
 
Nexus-webapp/:目次包含了Nexus 运行所须要的文件、如启动脚本、依附jar包 
 
Sonatype-work/:该目次包含Nexus生成的设备文件、日记文件、仓库文件。 
 
在window操纵体系,用户须要进入nexus-webpp/bin/jsw/windows-x86-32/子目次,然后直接运行nexus.bat 启动Nexus. 
 
2)、打开http://localhost:8081/nexus/ 
 
就能看到Nexus 的界面。默认的登录用户是admin/admin123。 
 
3)、创建仓库 
 
仓库分四种类型:group(仓库组)、hosted(宿主)、proxy()、virtual(虚拟)。 
 
Maven 可以直接从宿主仓库构件,maven也可以从仓库构件,而仓库会间接从长途仓库并缓存构件。在Repositories 界面创建对应的仓库后在sonatype-work 目次下会临盆一个对应的路径。比如在宿主仓库下创建一个id 为010的仓库D:/nexus1.9/sonatype-work/nexus/storage/010。 
 
Nexus 三个首要的仓库,有中心仓库,Apache 快照仓库,和Codehaus 快照仓库。这三个仓库保护了成千上万的构建,全手到本地不切实际,所以大项目组都保护了一个编录全部内容的Lucene 索引,以便供给快速和有效的搜刮。 
 
a、Apache Snapshots:这个仓库包含了来自于Apache软件基金会的快照版本。http://people.apache.org/repo/m2-snapshot-repository 
 
b、Codehaus Snapshots:这个仓库包含了来自于Codehaus的快照版本。 http://snapshots.repository.codehaus.org/ 
 
c、Central Maven Repository :这是中心Maven仓库(公布版本)。 http://repo1.maven.org/maven2/ 
 
4)、上传构件,在创建好的仓库中有Artifat ?Upload 选项卡。上传构件的体式格式有两种,第一种是maven构件的,那么可以在GAV Definition 下拉列表中选择From POM ,不然就选GAV Parameters。用户须要为该构件定义一个Maven 坐标。Group,artifact,version。 
 
5)、Nexus 权限经管 
 
组织中应用Nexus 有一些安然性的须要,比如只有经管员才干设备nexus,某些团队成员才干安排构件,Nexus 按照需求供给了周全的权限把握特点,能让用户的按照需求设备Nexus用户、角色、权限。 
 
3       参考材料 
3.1      重视事项 
防止MAVEN/OutOfMemoryError,须要在maven build->jre中设备:-Xms128m -Xmx512m 
 
关于Setting的申明: 一般我们复制一个setting.xml 文件到~/.m2/目次下,~默示用户目次,在我的文档中。settting.xml用于设备一些全局性的器材,比如仓库,镜像等. 
 
关于仓库的申明: 仓库可以分为两大类,本地仓库和长途仓库。当maven按照坐标寻找构件的时辰,起首在本地仓库查找,若是本地仓库存在则直接应用,若是本地仓库不存在,或者查看是否有更新的版本,maven 则归去长途仓库查看,发明今后到本地仓库应用,若是都没有则会报错。是一种特别的长途仓库。 
 
关于坐标的申明: 
 
Groupid:定义maven 项目附属的实际项目。比如springFramework 
 
Artifactid:定义实际项目中的一个模块。比如spring-core,spring-context 
 
Version:版本号。2.0.0 
 
Packageing:打包体式格式,.jar,.war。 
 
Classifier:该元素用来帮组定义构件输出的一些从属构件。 
 
此中groupid,artifactid,version 是必须定义的,packaging 是可选的,默认为jar. 
 
3.2      Maven常用号令 
   mvn archetype:create 创建Maven项目 
 
  mvn eclipse:eclipse 生成eclipse项目文件 
 
  mvn compile 编译源代码 
 
  mvn test-compile 编译测试源代码 
 
  mvn test 运行应用法度中的单位测试 
 
  mvn site 生成项目相干信息的网站 
 
  mvn clean 清除项目目次中的生成成果 
 
  mvn package 按照项陌生成的jar 
 
  mvn install 在本地Respository中安装jar 
 
mvn deploy 将包公布到办事器或长途仓库 
 
3.3      Tomcat- Maven-Plugin常用号令 
Goals available for this plugin: 
 
Goal  Description  
tomcat:deploy  Deploy a WAR to Tomcat.  
tomcat:deploy-only  Deploy a WAR to Tomcat witjout forking the package lifecycle  
tomcat:exploded  Deploy an exploded WAR to Tomcat.  
tomcat:help  Display help information on tomcat-maven-plugin. 
Call 
mvn tomcat:help -Ddetail=true -Dgoal=<goal-name> 
to display parameter details.  
tomcat:info  Lists information about the Tomcat version, OS, and JVM properties.  
tomcat:inplace  Deploy a WAR in-place to Tomcat.  
tomcat:list  Lists all the currently deployed web applications in Tomcat.  
tomcat:redeploy  Redeploy a WAR in Tomcat. Deploy with forcing flag to true  
tomcat:reload  Reload a WAR in Tomcat.  
tomcat:resources  Lists JNDI resources in Tomcat.  
tomcat:roles  Lists security roles in Tomcat.  
tomcat:run  Runs the current project as a dynamic web application using an embedded Tomcat server.  
tomcat:run-war  Runs the current project as a packaged web application using an embedded Tomcat server.  
tomcat:run-war-only  Runs the current project as a packaged web application using an embedded Tomcat server without forking the package cycle.  
tomcat:sessions  Lists session information for a WAR in Tomcat.  
tomcat:shutdown  Shuts down all possibly started embedded tomcat servers. This will be automatically down through a shutdown hook or you may call this Mojo to shut them down explictly. 
By default the shutdown goal is not bound to any phase. For integration tests you might want to bind it to post-integration-test.  
tomcat:start  Start a WAR in Tomcat.  
tomcat:stop  Stop a WAR in Tomcat.  
tomcat:undeploy  Undeploy a WAR Tomcat.  
 
 
 
 
3.4      Maven-POM设置: 
http://maven.apache.org/ref/3.0.4/maven-model/maven.html 
 
3.5      Maven-Settings设置: 
http://maven.apache.org/ref/3.0.4/maven-settings/settings.html 
 
原文地址:https://www.cnblogs.com/huapox/p/3251605.html