maven常用命令


1、常用命令
     mvn compile
     编译主程序源代码,不会编译test目录的源代码。第一次运行时,会下载相关的依赖包,可能会比较费时。

     mvn test-compile
     编译测试代码,compile之后会生成target文件夹,主程序编译在classes下面,测试程序放在test-classes下。

     mvn test
     运行应用程序中的单元测试

     mvn site

    生成项目相关信息的网站

     mvn clean

     清除目标目录中的生成结果
     mvn package
     依据项目生成 jar 文件,打包之前会进行编译,测试。
     mvn install
     在本地 Repository 中安装 jar。
     mvn eclipse:eclipse
     生成 Eclipse 项目文件及包引用定义,注意,需确保定义Classpath Variables: M2_REPO,指向本地maven类库 目录。

     mvn help:describe

     查看goal描述 例如参看compiler插件

     mvn help:describe -Dplugin=compiler -Dmojo=compile -Dfull-Dplugin=compiler -Dmojo=compile -Dfull

     mvn dependency:copy-dependencies -DoutputDirectory=lib   -DincludeScope=compile  

     导出依赖包到项目的lib目录

2. 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>test.demo</groupId>
	<artifactId>test</artifactId>
	<packaging>war</packaging>
	<version>0.0.1-SNAPSHOT</version>
	<name>sypro Maven Webapp</name>
	<url>http://maven.apache.org</url>
	<dependencies>
		<!-- 加入spring mvc依赖包 -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>3.2.2.RELEASE</version>
		</dependency>
		<!-- 加入orm依赖包 -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-orm</artifactId>
			<version>3.2.2.RELEASE</version>
		</dependency>
		<!-- 加入spring测试依赖包 -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-test</artifactId>
			<version>3.2.2.RELEASE</version>
		</dependency>

		<!-- 加入hibernate依赖包 -->
		<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-core</artifactId>
			<version>4.1.9.Final</version>
		</dependency>

		<!-- 加入junit依赖包 -->
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.11</version>
			<scope>test</scope>
		</dependency>

		<!-- 加入mysql驱动依赖包 -->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>5.1.25</version>
		</dependency>
		<!-- sql server数据库驱动 -->
		<!-- <dependency> <groupId>net.sourceforge.jtds</groupId> <artifactId>jtds</artifactId> <version>1.3.0</version> </dependency> -->
		<!-- oracle数据库驱动 -->
		<!-- <dependency> <groupId>com.oracle</groupId> <artifactId>ojdbc14</artifactId> <version>10.2.0.1.0</version> </dependency> -->

		<!-- 加入druid数据源依赖包 -->
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>druid</artifactId>
			<version>0.2.17</version>
		</dependency>
		<dependency>
			<groupId>org.aspectj</groupId>
			<artifactId>aspectjweaver</artifactId>
			<version>1.7.2</version>
		</dependency>

		<!-- 加入fastjson依赖包 -->
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>fastjson</artifactId>
			<version>1.1.30</version>
		</dependency>

		<!-- 加入jackson依赖包 -->
		<!-- <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-mapper-asl</artifactId> <version>1.9.12</version> </dependency> -->

		<!-- 加入slf4j依赖包 -->
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-log4j12</artifactId>
			<version>1.7.5</version>
		</dependency>

		<!-- 加入jstl依赖包 -->
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>jstl</artifactId>
			<version>1.2</version>
		</dependency>
		<dependency>
			<groupId>javax.servlet.jsp</groupId>
			<artifactId>jsp-api</artifactId>
			<version>2.2.1-b03</version>
			<scope>provided</scope>
		</dependency>
		<!-- servlet -->
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>servlet-api</artifactId>
			<version>2.5</version>
			<scope>provided</scope>
		</dependency>

		<!-- io包 -->
		<dependency>
			<groupId>commons-io</groupId>
			<artifactId>commons-io</artifactId>
			<version>2.4</version>
		</dependency>
		<!-- 加入fileupload依赖包 -->
		<dependency>
			<groupId>commons-fileupload</groupId>
			<artifactId>commons-fileupload</artifactId>
			<version>1.3</version>
		</dependency>

	</dependencies>
	<build>
		<finalName>sypro</finalName>
		<pluginManagement>
			<plugins>
				<!-- 编译的时候使用JDK6和UTF8编码 -->
				<plugin>
					<artifactId>maven-compiler-plugin</artifactId>
					<configuration>
						<source>1.6</source>
						<target>1.6</target>
						<encoding>UTF-8</encoding>
					</configuration>
				</plugin>
				<plugin>
	                <groupId>org.mortbay.jetty</groupId>
	                <artifactId>maven-jetty-plugin</artifactId>
	                <version>6.1.10</version>
	                <configuration>
	                	<contextPath>/sypro</contextPath>  
	                    <connectors>  
	                        <connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">  
	                            <port>80</port>  
	                            <maxIdleTime>60000</maxIdleTime>  
	                        </connector>  
	                    </connectors>  
	                    <scanIntervalSeconds>10</scanIntervalSeconds>
	                    <stopKey>foo</stopKey>
	                    <stopPort>9999</stopPort>
	                    <requestLog implementation="org.mortbay.jetty.NCSARequestLog">  
	                        <filename>target/access.log</filename>  
	                        <retainDays>90</retainDays>  
	                        <append>false</append>  
	                        <extended>false</extended>  
	                        <logTimeZone>GMT+8:00</logTimeZone>  
	                    </requestLog>  
	                    <systemProperties>  
	                        <systemProperty>  
	                            <name>productionMode</name>  
	                            <value>false</value>  
	                        </systemProperty>  
	                    </systemProperties>  
	                </configuration>
	                <executions>
	                    <execution>
	                        <id>start-jetty</id>
	                        <phase>pre-integration-test</phase>
	                        <goals>
	                            <goal>run</goal>
	                        </goals>
	                        <configuration>
	                            <scanIntervalSeconds>0</scanIntervalSeconds>
	                            <daemon>true</daemon>
	                        </configuration>
	                    </execution>
	                    <execution>
	                        <id>stop-jetty</id>
	                        <phase>post-integration-test</phase>
	                        <goals>
	                            <goal>stop</goal>
	                        </goals>
	                    </execution>
	                </executions>
	            </plugin>
				<plugin>
				    <groupId>org.codehaus.mojo</groupId>
				    <artifactId>tomcat-maven-plugin</artifactId>
				    <configuration>
					    <url>http://localhost:8080/manager</url>
					    <server>myserver</server>
				    </configuration>
				</plugin>
			</plugins>
		</pluginManagement>
	</build>
</project>

 

 

 

   说明:
    modelversion pom.xml 使用的对象模型版本
    groupId 创建项目的组织或团体的唯一 Id
    artifactId 项目唯一Id, 项目名
    packaging 打包扩展名(JAR、WAR、EAR)
    version 项目版本号
    name 显示名,用于生成文档
    url 组织站点,用于生成文档
    description 项目描述,用于生成文档
    dependency之scope 管理依赖部署,取值如下:
   compile 缺省值,用于所有阶段,随项目一起发布;
   provided 期望JDK、容器或使用者提供此依赖。如servlet.jar;
   runtime 只在运行时使用;
   test 只在测试时使用,不随项目发布;
   system 需显式提供本地jar,不在代码仓库中查找;


 3. 创建Maven Web项目
    mvn archetype:create -DgroupId=com.jlqu -DartifactId=javaweb -DarchetypeArtifactId=maven-archetype-webap        

     groupId 组织名,对应项目的package;artifactId 项目名;archetypeArtifactId 项目类型

     为给eclipse指定工作空间。c:\eclipse\workspace即eclipse的workspace
     mvn eclipse:eclipse 
     mvn –o war:exploded 离线发布修改过的jsp等工程文件
     mvn –o package –Dmaven.test.skip=true  离线打包工程,不含测试代码
     mvn compile hibernate3:hbm2ddl  会通过hibernate映射文件创建数据库表。
     mvn –o appfuse:gen –Dentity=Teacher
     mvn –o appfuse:install –Dentity=Teacher 通过appfuse框架自动生成与实体类对应的                            

             dao,service,action,test、jsp等程序文件

     mvn install:install-file -DgroupId=javax.transaction -DartifactId=jta

            -Dversion=1.0.1B -Dpackaging=jar -Dfile=D:\Test\ssh\WebRoot\WEB-INF\lib\jta.jar
     将本地jar包安装到应用中去。D:\Test\ssh\WebRoot\WEB-INF\lib\jta.jar&quot;即本地包

    Maven2 的运行命令为 : mvn ,
       常用命令为 :
             mvn compile :编译源代码
             mvn test : 运行应用程序中的单元测试
             mvn clean :清除目标目录中的生成结果
             mvn install :在本地 Repository 中安装 jar

       生成项目
          建一个 web 项目 : mvn archetype:create -DgroupId=com.demo

                                              -DartifactId=web-app -DarchetypeArtifactId=maven-archetype-webapp

       生成 Eclipse 项目
           Eclipse WTP 项目执行 : mvn eclipse:eclipse –Dwtpversion=1.0

 pom.xml 文件基本节点介绍
:文件的根节点 .
: pom.xml 使用的对象模型版本 .
:创建项目的组织或团体的唯一 Id.
:项目的唯一 Id, 可视为项目名 .
:打包物的扩展名,一般有 JAR,WAR,EAR 等
:产品的版本号 .
:项目的显示名,常用于 Maven 生成的文档。
:组织的站点,常用于 Maven 生成的文档。
:项目的描述,常用于 Maven 生成的文档。

目前  可以使用 5 个值:

provided :类似 compile ,期望 JDK 、容器或使用者会提供这个依赖。如 servlet.jar 。
test :只在测试时使用,用于编译和运行测试代码。不会随项目发布。

定义自己的结构

mvn archetype:create\
-DgroupId=cn.prof\
-DartifactId=prof-archetype\
-DarchetypeArtifactId=maven-archetype-archetype

 修改其中内容
       安装此项目 : mvn install

mvn archetype:create -DgroupId=com.mergere.mvnbook \
-DartifactId=proficio-example\
-DarchetypeGroupId=com.xxx.mvn\
-DarchetypeArtifactId= prof-archetype \
-DarchetypeVersion=1.0-SNAPSHOT

Maven2  Directory

目录
二级目录
三级目录
四级目录
说明

pom.xml Maven2 的项目设置文件
src/ 源码目录
main/ 项目主体目录根
java/ 源代码目录
resources/ 所需资源目录
filters/ 资源过滤文件目录
assemby/ Assembly descriptors
config/ 配置文件根目录
webapp/ web 应用目录
WEB-INF/ WEB-INF 目录
test/ 项目测试目录根
java/ 测试代码目录
resources/ 测试所需资源目录
filters/ 测试资源过滤文件目录
site/ 与site 相关的资源目录
target/ 输出目录根
classes/ 项目主体输出目录
test_classes/ 项目测试输出目录
site/ 项目site 输出目录

在Maven2中有了明确的生命周期概念,而且都提供与之对应的命令,使得项目构建更加清晰明了。主要的生命周期阶段:
1. validate,验证工程是否正确,所有需要的资源是否可用。
2. compile,编译项目的源代码。
3. test-compile,编译项目测试代码。
4. test,使用已编译的测试代码,测试已编译的源代码。
5. package,已发布的格式,如jar,将已编译的源代码打包。
6. integration-test,在集成测试可以运行的环境中处理和发布包。
7. verify,运行任何检查,验证包是否有效且达到质量标准。
8. install,把包安装在本地的repository中,可以被其他工程作为依赖来使用
9. deploy,在整合或者发布环境下执行,将最终版本的包拷贝到远程的repository,使得其他的开发者或者工程可以共    享。
10. generate-sources,产生应用需要的任何额外的源代码,如xdoclet。
     如果要执行项目编译,那么直接输入:mvn compile即可,对于其他的阶段可以类推。阶段之间是存在依赖关系(dependency)的,如test依赖test-compile。在执行 mvn test时,会先运行mvn test-compile,然后才是mvn 12. test
      因为maven2 是有生命周期这一概念的,所以如果你执行package,相应的以前步骤,如compile,test等都会自动执行。 刚开始执行会比较慢,需要从maven2远程库中下载所有的文件到本地。如果你的本地没有相应的依赖包,则每次maven都会去远程下载,所以配置一个镜像库就比较重要了。

1.  当 M2eclipse 插件装上之后,会报这个错
    Some Maven plugins may not work when importing projects or updating source folders.
    创建一个快捷方式,设定目标位置为 D:\eclipse_jee\eclipse.exe -vm D:\Java\jdk1.5.0_10\bin\javaw
    解决办法 :在 pom.xml 中 plugins 加入说明就可以了
    maven-compiler-plugin1.51.5maven-compiler-plugin1.51.5
2. 在使用命令mvn eclipse:eclipse 之后,import到Eclipse,然后在Eclipse项目上点右键,选择插件Maven>Enable Dependency Management后,会报重复引用依赖的错误
解决办法 :再执行mvn eclipse:eclipse然后刷新Eclipse的项目,取消掉maven插件的引用依赖,错误就会消除
3. Failure executing javac, but could not parse the error:
编译器 (1.5.0_10) 中出现异常。 如果在 Bug Parade 中没有找到该错误,请在 Java Developer Connection (http://java.sun.com/webapps/bugreport )  对该错误进行归档。请在报告中附上您的程序和以下诊断信息。谢谢您的合作。
java.nio.BufferOverflowException
解决办法 :(临时)在控制面板中将区域与语言选项中,把当地语言设为英语

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值