`

Maven command learning.

阅读更多

一、安装Maven并设定环境变量

首先到 http://maven.apache.org 去下一个maven最新的安装包,比如2.0.8,随便解压到一个目录,假设 C:\mvn208。

 

然后设定环境变量:

 

SET M2_HOME=%install-dir%
SET MAVEN_HOME=%install-dir%/bin

 

默认的本地 Repositories-Path 在 %M2_HOME%/conf/settings.xml中,找到<localRepository>修改之即可。

这步也很重要,如果出现mvn运行时错误请检查此处。

 

查看Maven版本:

mvn -v | -ver | -verson

 

二、创建简单的Maven-project 

建个目录C:\mvnTest,命令行输入> mvn archetype:create -DgroupId=com.test -DartifactId=mytest

 

-DgroupId    -- 项目组名称,也可以理解为默认的包名称。

-DartifactId  -- 项目名称、工程名,等价于 project-name

 

这样就在目录C:\mvnTest\mytest下生成了一个mvn-project了。

 

进入该目录发现有个pom.xml(以下简称pom)文件,mvn的全部配置(对于此项目)都在这了。

 

通常开发需要集成到Eclipse,生成.project,.classpath
在pom-path下输入:

 

mvn eclipse:eclipse

 

三、使用Help插件

在pom-path下尝试输入一下命令:

 

mvn help:active-profiles       -- 列出当前构建中活动的 Profile(项目的,用户的,全局的)。

mvn help:effective-pom       -- 显示当前构建的实际 POM,包含活动的 Profile。 

mvn help:effective-settings -- 打印出项目的实际 settings, 包括从全局的 settings 和用户级别 settings继承的配置。

mvn help:describe               -- 描述插件的属性。它不需要在项目目录下运行。但是你必须提供你想要描述插件的 groupId 和 artifactId。 

 

describe加上参数就会像下面这样:
mvn help:describe -Dcmd=install

mvn help:describe -Dcmd=help:describe

mvn help:describe -Dplugin=org.apache.maven.plugins:maven-help-plugin

mvn help:describe -DgroupId=org.apache.maven.plugins -DartifactId=maven-help-plugin

 

具体作用,光看名字就一目了然了。

 

四、使用Archetype 插件

 

创建一个maven项目:

 

mvn archetype:create -DgroupId=com.sunnysoft -DartifactId=sunnyware -DpackageName=com.sunnysoft.sunnyware

 

...

-DpackageName -- 指定包名称(不写的话就是-DgroupId了)

 

执行后生成的pom如下:

 

<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.sunnysoft</groupId>
	<artifactId>sunnyware</artifactId>
	<packaging>jar</packaging>
	<version>1.0-SNAPSHOT</version>
	<name>sunnyware</name>
	<url>http://maven.apache.org</url>
	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</dependency>
	</dependencies>
</project>

 

项目的目录:

 

生成的目录及文件:
\---sunnyware
    |   pom.xml
    |   
    \---src
        +---main
        |   \---java
        |       \---com
        |           \---sunnysoft
        |               \---sunnyware
        |                       App.java
        |                       
        \---test
            \---java
                \---com
                    \---sunnysoft
                        \---sunnyware
                                AppTest.java

 

运行 mvn install 来初始化项目。

 

 

构建后的目录及文件:
\---sunnyware
    |   pom.xml
    |   
    +---src
    |   +---main
    |   |   \---java
    |   |       \---com
    |   |           \---sunnysoft
    |   |               \---sunnyware
    |   |                       App.java
    |   |                       
    |   \---test
    |       \---java
    |           \---com
    |               \---sunnysoft
    |                   \---sunnyware
    |                           AppTest.java
    |                           
    \---target
        |   sunnyware-1.0-SNAPSHOT.jar
        |   
        +---classes
        |   \---com
        |       \---sunnysoft
        |           \---sunnyware
        |                   App.class
        |                   
        +---surefire-reports
        |       com.sunnysoft.sunnyware.AppTest.txt
        |       TEST-com.sunnysoft.sunnyware.AppTest.xml
        |       
        \---test-classes
            \---com
                \---sunnysoft
                    \---sunnyware
                            AppTest.class

 

Look!目录中多了测试和部署文件!

用 java -cp target/sunnyware-1.0-SNAPSHOT.jar com.sunnysoft.sunnyware.App 来测试App.java 吧!

 

CONTINUE

 

当 Maven 经过以 package 为结尾的默认生命周期的时候,下面的目标按顺序被执行:

 

resources:resources
Resources 插件的 resources 目标绑定到了 resources  阶段。这个目标复制 src/main/resources 下的所有资源和其它任何配置的资源目录,到输出目录。  

 

compiler:compile
Compiler 插件的 compile 目标绑定到了 compile  阶段。这个目标编译src/main/java 下的所有源代码和其他任何配置的资源目录,到输出目录。

 

resources:testResources 
Resources 插件的 testResources 目标绑定到了 test-resources  阶段。这个目标复制 src/test/resources 下的所有资源和其它任何的配置的测试资源目录,到测试输出目录。  

 

compiler:testCompile 
Compiler 插件的 testCompile 目标绑定到了 test-compile  阶段。这个目标编译 src/test/java 下的测试用例和其它任何的配置的测试资源目录,到测试输出目录。

 

surefire:test
Surefire 插件的 test 目标绑定到了 test  阶段。这个目标运行所有的测试并且创建那些捕捉详细测试结果的输出文件。默认情况下,如果有测试失败,这个目标会终止。

 

jar:jar 
Jar 插件的jar 目标绑定到了 package  阶段。这个目标把输出目录打包成JAR文件。

 

Maven exec 插件:

mvn exec:java -Dexec.mainClass=org.sonatype.mavenbook.weather.Main 相当于测试一个有main方法的class文件。(比如 java Main)

 

Maven dependency 插件:


mvn dependency:analyze  -- 分析项目依赖性


mvn dependency:resolve  -- 显示项目依赖


mvn dependency:tree       -- 显示项目依赖的树形结构

 

单元测试:

mvn test -- 直接运行Maven单元测试。实际上在 mvn install 或 mvn package 同时也会运行单元测试。test 依赖在pom中的定义像这样:

 

<dependency>
	<groupId>junit</groupId>
	<artifactId>junit</artifactId>
	<version>3.8.1</version>
	<scope>test</scope>
</dependency>

 

如果你打算在构建时忽略掉错误的信息,就这样定义:

 

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <configuration>
                <!-- 此值为true则忽略测试中的构建错误 -->
                <testFailureIgnore>true</testFailureIgnore>

                <!-- 此值为true则不执行单元测试 -->
                <skip>true</skip>
            </configuration>
        </plugin>
    </plugins>
</build>

 关于此插件可以参考:http://maven.apache.org/plugins/maven-surefire-plugin/test-mojo.html

 

命令行的话:

 

mvn test -Dmaven.test.failure.ignore=true -- 忽略错误构建

 

另外,这行最常用到的:

 

mvn package -Dmaven.test.skip=true         -- 在打包时跳过测试

 

Maven Assembly (创建分发包) 插件:

 

pom定义:

<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <configuration>
        <descriptorRefs>
            <!-- 这样可以把项目依赖的class或jar文件也一并构建进去 -->
            <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
    </configuration>
</plugin>

 

创建webapp:

 

就像创建一个普通的JavaProject一样简单:


mvn archetype:create -DgroupId=com.sunnysoft -DartifactId=sunnywareweb -DpackageName=com.sunnyware -DarchetypeArtifactId=maven-archetype-webapp

 

-DarchetypeArtifactId=maven-archetype-webapp 加上即可。

 

最后,要清理项目时候就用:

 

mvn clean install -- 清理项目。

分享到:
评论

相关推荐

    spring-boot-reference.pdf

    5. Learning about Spring Boot Features 6. Moving to Production 7. Advanced Topics II. Getting Started 8. Introducing Spring Boot 9. System Requirements 9.1. Servlet Containers 10. Installing Spring ...

    groovy_in_action_draft_ch_01.pdf

    Documentation is essential for learning and mastering Groovy. There are several resources available to help you: **1.7.1 Using Online Resources** The official Groovy website is a primary source of ...

    OSGi and Apache Felix 3.0.pdf

    Learn everything needed about the Felix Framework and get familiar with Gogo, its command-line shell to start developing your OSGi applications. Simplify your OSGi development experience by learning ...

    mastering-spring-cloud2018

    Maven Git client Chapter 1, Introduction to Microservices, will introduce you to the microservices architecture, cloud environment, etc. You will learn the difference between a microservice based ...

    spring boot 学习demo

    Spring Boot 的核心特性之一是其起步依赖,这些是预定义的Maven或Gradle配置模块,可以轻松地引入到项目中,例如:spring-boot-starter-web用于web应用,spring-boot-starter-data-jpa用于数据库访问等。...

    spring boot 1.3.5 release

    - **了解Spring Boot特性**(Learning about Spring Boot features):深入探讨了Spring Boot提供的各种功能。 - **迁移至生产环境**(Moving to production):讲述了如何将Spring Boot应用部署到生产环境。 - **...

Global site tag (gtag.js) - Google Analytics