刚刚入门Maven,记录一下踩过的坑。希望对大家有帮助。
(1)下载依赖jar时,默认仓库太慢了
可以换源,这里以阿里的源为例。修改配置文件\apache maven\conf\settings.xml,在mirrors中添加下述信息:
<mirrors>
<mirror>
<id>alimaven</id>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<mirrorOf>central</mirrorOf>
</mirror>
</mirrors>
另外在项目pom.xml中添加下述定义远程库为阿里库:
<repositories>
<repository>
<id>alimaven</id>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
(2)“[ERROR] 不再支持源选项 1.5。”
在项目的pom.xml中添加下述属性指定编译版本,顺便指定UTF编码:
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
(3)利用junit进行单元测试无法找到jar / @Test注解报错
junit的3.x版本为编程方式,而4.x是注解方式,因此需要使用高版本替换默认低版本。在pom.xml中添加下述依赖说明:
<repositories>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</repositories>
(4)mvn test没有测试输出,或者缺失测试统计信息
这个不清楚是不是版本问题,需要在项目pom.xml内为maven-surefire-plugin指定测试的.java路径,即在文件中添加如下信息:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.10</version>
<configuration>
<includes>
<include>**/hellotest.java</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
(5)利用maven直接执行项目类的main函数
将自动加载exec-maven-plugin插件:
mvn exec:java -Dexec.mainClass="xxx.xxx.xxx" [-Dexec.args="arg1 arg2 arg3"]