1、编译测试java
<dependency>apache
……maven
<scope>test</scope>测试
</dependency>ui
Scope:依赖范围。若依赖范围为test则表示该依赖只对测试有效。若是不声明依赖范围,那么默认值为compile,表示该依赖对主代码和测试代码都有效。spa
Mvn clean compile :插件
执行过程:clean:clean --> resources:resources --> compiler:compileorm
Mvn clen testget
执行过程:clean:clean --> resources:resources --> compiler:compile -->resources:testResources –-> compiler:testCompile --> surefire:testio
Surefire是maven中负责执行测试的插件,会显示一共运行了多少测试,失败了多少 ,出错了多少,跳过了多少。
注意:3.1版本及以前的maven核心插件之一compiler插件默认只支持编译java1.3,所以须要配置该插件使其支持java5,以下:
<project>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
2、打包和运行
Pom中若是没有指定打包类型,则默认打包类型为jar。执行命令mvn clean package可进行打包。Maven在打包以前执行编译、测试等操做后会后执行jar:jar任务负责打包。默认包名命名规则为:artifact-version.jar
打包完执行mvn clean install 可将该bao安装到maven库中,供其余maven项目直接引用。
打包可运行程序
默认打包生成的jar是不可以直接运行的,由于带有main方法的类信息不回添加到manifest中。为了生成可执行的jar文件,须要借助maven-shade-plugin,配置位置:<project><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.mycom.mvntest.helloword.HelloWord</mainClass>
<transformer>
<transformers>
<configuration>
</execution>
</executions>
</plugin>
打包后执行命令:java-jar target \hello-world-1.0-SNAPSHOT.jar