生成第一个maven项目
mvn -B archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.4
<?xml version="1.0" encoding="UTF-8"?> <!-- xml: xml标识 -->
<!-- project: pom文件根元素 -->
<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><!-- modelVersion: 模型版本,根据版本不同模型内容也不同,这里学习4.0.0版本 -->
<groupId>com.mycompany.app</groupId><!-- 表明创作者身份或组织 -->
<artifactId>my-app</artifactId><!-- 对外发布的软件名称(唯一) -->
<version>1.0-SNAPSHOT</version><!-- 软件版本号 -->
<name>my-app</name><!-- 用于在生成的文档显示该软件的名字 -->
<url>http://www.example.com</url><!-- 用于在生成的文档显示该软件的主页 -->
<properties><!-- 相当于声明一些变量,在pom任何地方使用,一般用于声明版本 -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><!-- 单个变量 -->
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies><!-- 声明软件中使用的依赖的集合 -->
<dependency><!-- 单个依赖 -->
<groupId>junit</groupId><!-- 依赖包组织名 -->
<artifactId>junit</artifactId><!-- 依赖包标识 -->
<version>4.11</version><!-- 依赖包版本 -->
<scope>test</scope><!-- 依赖包作用域 -->
</dependency>
</dependencies>
<build><!-- 声明项目的目录结构和管理插件 -->
<pluginManagement><!-- 为了避免使用默认版本,锁定下面插件的版本 (可以移动到父pom) -->
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
-
尝试编译
mvn compile
maven-resources-plugin maven-compiler-plugin
-
测试
mvn test
maven-resources-plugin:resources maven-compiler-plugin:compile maven-resources-plugin:testResources maven-compiler-plugin:testCompile maven-surefire-plugin:test
-
打包
mvn package
maven-resources-plugin:resources maven-compiler-plugin:compile maven-resources-plugin:testResources maven-compiler-plugin:testCompile maven-surefire-plugin:test maven-jar-plugin:jar
-
清理
mvn clean
maven-clean-plugin:clean
Phase
可以在尝试中发现在每一个生命周期中包含多个Phase,每个Phase执行一个目标。
Clean Lifecycle
| Phase | Description |
|---|---|
pre-clean | execute processes needed prior to the actual project cleaning |
clean | remove all files generated by the previous build |
post-clean | execute processes needed to finalize the project cleaning |
Default Lifecycle
| Phase | Description |
|---|---|
validate | validate the project is correct and all necessary information is available. |
initialize | initialize build state, e.g. set properties or create directories. |
generate-sources | generate any source code for inclusion in compilation. |
process-sources | process the source code, for example to filter any values. |
generate-resources | generate resources for inclusion in the package. |
process-resources | copy and process the resources into the destination directory, ready for packaging. |
compile | compile the source code of the project. |
process-classes | post-process the generated files from compilation, for example to do bytecode enhancement on Java classes. |
generate-test-sources | generate any test source code for inclusion in compilation. |
process-test-sources | process the test source code, for example to filter any values. |
generate-test-resources | create resources for testing. |
process-test-resources | copy and process the resources into the test destination directory. |
test-compile | compile the test source code into the test destination directory |
process-test-classes | post-process the generated files from test compilation, for example to do bytecode enhancement on Java classes. |
test | run tests using a suitable unit testing framework. These tests should not require the code be packaged or deployed. |
prepare-package | perform any operations necessary to prepare a package before the actual packaging. This often results in an unpacked, processed version of the package. |
package | take the compiled code and package it in its distributable format, such as a JAR. |
pre-integration-test | perform actions required before integration tests are executed. This may involve things such as setting up the required environment. |
integration-test | process and deploy the package if necessary into an environment where integration tests can be run. |
post-integration-test | perform actions required after integration tests have been executed. This may including cleaning up the environment. |
verify | run any checks to verify the package is valid and meets quality criteria. |
install | install the package into the local repository, for use as a dependency in other projects locally. |
deploy | done in an integration or release environment, copies the final package to the remote repository for sharing with other developers and projects. |
Site Lifecycle
| Phase | Description |
|---|---|
pre-site | execute processes needed prior to the actual project site generation |
site | generate the project's site documentation |
post-site | execute processes needed to finalize the site generation, and to prepare for site deployment |
site-deploy | deploy the generated site documentation to the specified web server |
Phase与goal
Clean Lifecycle Bindings
| Phase | plugin:goal |
|---|---|
clean | clean:clean |
Default Lifecycle Bindings - Packaging ejb / ejb3 / jar / par / rar / war
| Phase | plugin:goal |
|---|---|
process-resources | resources:resources |
compile | compiler:compile |
process-test-resources | resources:testResources |
test-compile | compiler:testCompile |
test | surefire:test |
package | ejb:ejb or ejb3:ejb3 or jar:jar or par:par or rar:rar or war:war |
install | install:install |
deploy | deploy:deploy |
Default Lifecycle Bindings - Packaging ear
| Phase | plugin:goal |
|---|---|
generate-resources | ear:generate-application-xml |
process-resources | resources:resources |
package | ear:ear |
install | install:install |
deploy | deploy:deploy |
Default Lifecycle Bindings - Packaging maven-plugin
| Phase | plugin:goal |
|---|---|
generate-resources | plugin:descriptor |
process-resources | resources:resources |
compile | compiler:compile |
process-test-resources | resources:testResources |
test-compile | compiler:testCompile |
test | surefire:test |
package | jar:jar and plugin:addPluginArtifactMetadata |
install | install:install |
deploy | deploy:deploy |
Default Lifecycle Bindings - Packaging pom
| Phase | plugin:goal |
|---|---|
package | |
install | install:install |
deploy | deploy:deploy |
Site Lifecycle Bindings
| Phase | plugin:goal |
|---|---|
site | site:site |
site-deploy | site:deploy |