maven学习笔记(基本的命令和概念)

mvn创建新项目:

mvn archetype:generate -DgroupId=org.sonatype.mavenbook.ch03 -DartifactId=simple -DpackageName=org.sonatype.mavenbook -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

编译测试打包等等。。。  

mvn install

install是声明周期

还有另外一种运行方式,运行插件和目标,例如:

mvn resources:resources 
compiler:compile 
resources:testResources 
compiler:testCompile 
surefire:test 
jar:jar

当Maven经过以package为结尾的默认生命周期的时候,下面的目标按顺序被执行
resources:resources
Resources插件的resources目标绑定到了resources阶段。这个目标复制src/main/resources下的所有资源和其它任何配置的资源目录,到输出目录。
compiler:compile
Compiler插件的compile目标绑定到了compile阶段。这个目标编译src/main/java下的所有源代码和其他任何配置的资源目录,到输出目录。
resources:testResources
Resources插件的testResources目标绑定到了test-resources阶段。这个目标复制src/test/resources下的所有资源和其它任何的配置的测试资源目录,到测试输出目录。
compiler:testCompile
Compiler插件的testCompile目标绑定到了test-compile阶段。这个目标编译src/test/java下的测试用例和其它任何的配置的测试资源目录,到测试输出目录。
surefire:test
Surefire插件的test目标绑定到了test阶段。这个目标运行所有的测试并且创建那些捕捉详细测试结果的输出文件。默认情况下,如果有测试失败,这个目标会终止。

jar:jar
Jar插件的jar目标绑定到了package阶段。这个目标把输出目录打包成JAR文件。

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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>cn.net.comsys.ut4</groupId>
  <artifactId>simpleDubbo</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>simpleDubbo</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

packaging项目的类型
默认是jar,描述了项目打包后的输出。类型为jar的项目产生一个JAR文件,类型为war的项目产生一个web应用

maven依赖关系中Scope的作用 

Dependency Scope 

在POM中,<dependency>中还引入了<scope>,它主要管理依赖的部署。目前<scope>可以使用5个值: 

    * compile,缺省值,适用于所有阶段,会随着项目一起发布。 
    * provided,类似compile,期望JDK、容器或使用者会提供这个依赖。如servlet.jar。 
    * runtime,只在运行时使用,如JDBC驱动,适用运行和测试阶段。 
    * test,只在测试时使用,用于编译和运行测试代码。不会随项目发布。 
    * system,类似provided,需要显式提供包含依赖的jar,Maven不会在Repository中查找它。 

当一个依赖的范围是test的时候,说明它在Compiler插件运行compile目标的时候是不可用的。它只有在运行compiler:testCompile和surefire:test目标的时候才会被加入到classpath中

站点生成和报告

mvn site

  

原文地址:https://www.cnblogs.com/jifeng/p/4785296.html