创建一个新项目:
mvn archetype:generate -DgroupId=cn.net.comsys.ut4.simpleweather -DartifactId=simple-weather -DpackageName=cn.net.comsys.ut4.simpleweather -Dversion=1.0
generate目标文档:Generate project in batch mode
为pom.xml添加一些项目信息如下:
<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>cn.net.comsys.ut4.simpleweather</groupId> <artifactId>simple-weather</artifactId> <version>1.0</version> <packaging>jar</packaging> <name>simple-weather</name> <url>http://WWW.COMSYS.NET.CN</url> <licenses> <license> <name>Apache 2</name> <url>http://www.apache.org/licenses/LICENSE-2.0.txt</url> <distribution>repo</distribution> <comments>A business-friendly OSS license</comments> </license> </licenses> <organization> <name>COMSYS</name> <url>http://WWW.COMSYS.NET.CN</url> </organization> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> </project>
如licenses、organization等
Maven Exec 插件
install成jar后可以通过exec执行main方法
mvn exec:java -Dexec.mainClass=org.sonatype.mavenbook.weather.Main
Exec 插件让我们能够在不往 classpath 载入适当的依赖的情况下,运行这个程序
查看exec插件目标以及参数帮助:
mvn help:describe -Dplugin=exec -Dfull
查看项目依赖:
mvn dependency:resolve
mvn dependency:tree
执行单元测试:
mvn test
忽略测试失败:<testFailureIgnore>true</testFailureIgnore>
<project> [...] <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <configuration> <testFailureIgnore>true</testFailureIgnore> </configuration> </plugin> </plugins> </build> [...] </project>
也可以通过命令行参数设置
mvn test -Dmaven.test.failure.ignore=true
跳过单元测试:
mvn install -Dmaven.test.skip=true
构建一个打包好的命令行应用程序
想pom.xml增加一下内容
<project> [...] <build> <plugins> <plugin> <artifactId>maven-assembly-plugin</artifactId> <configuration> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> </configuration> </plugin> </plugins> </build> [...] </project>
运行以下命令进行装配:
mvn install assembly:assembly