maven学习笔记(1)--特点和简例

来源:互联网 发布:新东方烹饪 知乎 编辑:程序博客网 时间:2024/06/03 20:16

项目构建一直是让我头疼的问题,很多构建任务都是靠手动和IDE:依赖靠复制jar包,编译和部署靠eclipse,项目的集成靠eclipse或插件,没有测试环节....这就会导致项目构建过程不清晰,每个项目的构建各不相同,要求大量手工操作,各个工作相互独立。现在在研究《maven实战》这本书,做下笔记。

maven的特点

1.将软件开发中涉及的构建步骤抽象成一个完整的构建生命周期模型,只要遵循约定,每个maven项目的构建都是标准化的,避免了不必要的学习成本。

2.每个构建步骤都有成熟插件帮助完成,无需像ant那样将每个步骤配置在xml中,用约定消除了重复。

3.提供了中央仓库,对以往繁杂且经常冲突的依赖提供了精确的定位和下载,还包含了文档和源码的自动下载,非常方便。

安装

1.官网下载压缩包解压后如上图,配置环境变量M2_HOME指向安装路径D:\apps\maven311\apache-maven-3.1.1,PATH变量追加%M2_HOME%\bin

conf文件夹下有个settings.xml可以全局订制maven行为,也可以拷贝到用户/.m2/目录下在用户范围内订制行为。

2.我的eclipse kepler已经集成了m2eclipse的maven插件,推荐不要使用内嵌版本,在设置里将其版本指向M2_HOME

maven版hello world

1.编写pom.xml

<?xml version="1.0" encoding="UTF-8"?><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/xsd/maven-4.0.0.xsd">  <modelVersion>4.0.0</modelVersion>  <groupId>com.jacky.mavenexamples</groupId>  <artifactId>mavenexpamles-helloworld</artifactId>  <version>1.0-SNAPSHOT</version>  <name>maven hello world example</name>  <dependencies>      <dependency>        <groupId>junit</groupId>      <artifactId>junit</artifactId>        <version>4.7</version>        <scope>test</scope>     </dependency>  </dependencies></project>
hello world项目在配置时指定3个坐标来定位,groupId组织,artifactId模块
mavenexamples-helloworld
可以清楚知道该模块隶属哪一个实际项目),version版本,因为下面需要对其测试加入了junit依赖,该依赖仅仅对test范围有效

2.编写项目主文件

根据约定,主文件存放目录为src\main\java\com\jacky\mavenexamples\helloworld,注意其与groupId与artifactId的对应关系,在该目录中是主文件Helloworld.java

package com.jacky.mavenexamples.helloworld;public class Helloworld{public String sayhi(){return "hello world";}public static void main(String[] args){System.out.println(new Helloworld().sayhi());}}

3.编写项目测试文件

存放目录为src\test\java\com\jacky\mavenexamples\helloworld,目录中是HelloworldTest.java

package com.jacky.mavenexamples.helloworld;import static org.junit.Assert.assertEquals;import org.junit.Test;public class HelloworldTest{@Testpublic void testSayhi(){Helloworld h=new Helloworld();String result=h.sayhi();assertEquals("hello world",result);}}

4.在项目根路径执行mvn clean install

从控制台可以看到maven执行了以下过程:

maven-clean-plugin:2.5:clean 将target目录删除

maven-resources-plugin:2.6:resources处理主文件资源(现在没有资源文件跳过)

maven-compiler-plugin:2.5.1:compile编译主文件,生成在target目录中

maven-resources-plugin:2.6:testResources处理测试文件资源(现在没有资源文件跳过)

 maven-compiler-plugin:2.5.1:testCompile编译测试文件,生成在target目录中

 maven-surefire-plugin:2.12.4:test在控制台输出测试报告

maven-jar-plugin:2.4:jar打jar包,生成在target目录中,命名的规则是artifactId-version.jar,即helloworld-1.0-SNAPSHOT.jar

maven-install-plugin:2.4:install将项目安装到本地仓库,方便其他项目使用


5.这时执行java -jar target\helloworld-1.0-SNAPSHOT会说没有指定Main-Class

需要在META-INF/MENIFEST.MF文件中指定Main-Class,在构建到打包步骤时可以使用插件来帮助指定,在pom.xml中加入:

<build>    <plugins>    <plugin>      <groupId>org.apache.maven.plugins</groupId>      <artifactId>maven-shade-plugin</artifactId>      <version>1.2.1</version>      <executions>        <execution>          <phase>package</phase>          <goals>            <goal>shade</goal>          </goals>          <configuration>            <transformers>              <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">                <mainClass>com.jacky.mavenexamples.helloworld.Helloworld</mainClass>              </transformer>            </transformers>          </configuration>        </execution>      </executions>    </plugin>    </plugins>  </build>
这样就可以看到激动人心的结果啦



1

1.






0 0
原创粉丝点击