码迷,mamicode.com
首页 > 其他好文 > 详细

《Maven实战》笔记之《三、Maven使用入门》

时间:2014-11-28 09:56:28      阅读:131      评论:0      收藏:0      [点我收藏+]

标签:style   blog   io   ar   color   使用   sp   java   for   

一、编写POM

<groupId>com.shine.myapp</groupId>
<artifactId>hello-world</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
  • groupId:定义了项目属于哪个组。
  • artifactId:定义了当前Maven项目在组中唯一的ID。
  • version:定义了项目当前版本。

二、编写主代码

  • 1、编写主代码HelloWord。
  • 2、执行mvn clean compile,执行顺序:clean:cleanresources:resourcescompile:compile
    •   clean告诉Maven清理输出目录target/。
    •   compile告诉Maven编译项目主代码。

三、编写测试代码

1、添加一个POM的依赖Junit:

<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.7</version>
        <scope>test</scope>
    </dependency>
</dependencies>
  • dependencies元素:改元素下可以包含多个dependency元素,以申明项目的依赖。
  • groupId、artifactId、version:Maven能自动在仓库下载unit-4.7.jar。
  • scope:为申明范围,若依赖范围为test则表示该依赖对应测试有效;默认值为compile,表示该依赖对主代码和测试代码都有效。

2、编写单元测试代码。

3、执行mvn clean test,执行顺序:clean:clean、resources:resources、compile:compile、resources:testResources、compile:testCompile。

4、配置maven-compile-plugin支持Java6:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
        <source>1.6</source>
        <target>1.6</target>
    </configuration>
</plugin>

四、打包和运行

1、打包:mvn clean package

2、安装:mvn clean install

3、打可执行Main函数的jar,添加maven-shade-plugin插件:

<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.shine.helloworld.HelloWorld</mainClass>
                    </transformer>
                </transformers>
            </configuration>
        </execution>
    </executions>
</plugin>

运行mvn clean package出现两个jar:hello-world-0.0.1-SNAPSHOT.jar、original-hello-world-0.0.1-SNAPSHOT.jar前者是可执行的jar、后者是原始jar

 

总结:

mvn clean compile、mvn clean test、mvn clean package、mvn clean install中,

mvn clean test 会执行 mvn clean compile;

mvn clean package 会执行 mvn clean test;

mvn clean install 会执行 mvn clean package。

 

《Maven实战》笔记之《三、Maven使用入门》

标签:style   blog   io   ar   color   使用   sp   java   for   

原文地址:http://www.cnblogs.com/shine-xh/p/4127703.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!