简介
Apache Maven,是一个软件(特别是Java软件)项目管理及自动构建工具,由Apache软件基金会所提供。
基于项目对象模型(缩写:POM)概念,Maven利用一个中央信息片断能管理一个项目的构建、报告和文档等步骤。
Maven也可被用于构建和管理各种项目,例如C#,Ruby,Scala和其他语言编写的项目。Maven曾是Jakarta项目的子项目,现为由Apache软件基金会主持的独立Apache项目。
Maven的超级POM文件(pom-4.0.0.xml)可以在maven-model-builder-3.5.0.jar
中找到。
使用指令构建Maven项目(我使用的Maven version: 3.5.0)
指令名称:mvn archetype:generate
方式一:在交互中指定groupId,artifactId,version,package相关信息
D:\code\IDEA_WORKSPACE>mvn archetype:generate
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] >>> maven-archetype-plugin:3.0.1:generate (default-cli) > generate-sources @ standalone-pom >>>
[INFO]
[INFO] <<< maven-archetype-plugin:3.0.1:generate (default-cli) < generate-sources @ standalone-pom <<<
[INFO]
[INFO]
[INFO] --- maven-archetype-plugin:3.0.1:generate (default-cli) @ standalone-pom ---
[INFO] Generating project in Interactive mode
[WARNING] No archetype found in remote catalog. Defaulting to internal catalog
[INFO] No archetype defined. Using maven-archetype-quickstart (org.apache.maven.archetypes:maven-archetype-quickstart:1.0)
Choose archetype:
1: internal -> org.apache.maven.archetypes:maven-archetype-archetype (An archetype which contains a sample archetype.)
2: internal -> org.apache.maven.archetypes:maven-archetype-j2ee-simple (An archetype which contains a simplifed sample J2EE application.)
3: internal -> org.apache.maven.archetypes:maven-archetype-plugin (An archetype which contains a sample Maven plugin.)
4: internal -> org.apache.maven.archetypes:maven-archetype-plugin-site (An archetype which contains a sample Maven plugin site.
This archetype can be layered upon an existing Maven plugin project.)
5: internal -> org.apache.maven.archetypes:maven-archetype-portlet (An archetype which contains a sample JSR-268 Portlet.)
6: internal -> org.apache.maven.archetypes:maven-archetype-profiles ()
7: internal -> org.apache.maven.archetypes:maven-archetype-quickstart (An archetype which contains a sample Maven project.)
8: internal -> org.apache.maven.archetypes:maven-archetype-site (An archetype which contains a sample Maven site which demonstrates
some of the supported document types like APT, XDoc, and FML and demonstrates how
to i18n your site. This archetype can be layered upon an existing Maven project.)
9: internal -> org.apache.maven.archetypes:maven-archetype-site-simple (An archetype which contains a sample Maven site.)
10: internal -> org.apache.maven.archetypes:maven-archetype-webapp (An archetype which contains a sample Maven Webapp project.)
Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): 7: 7
Define value for property 'groupId': com.alpha
Define value for property 'artifactId': crypto
Define value for property 'version' 1.0-SNAPSHOT: : 1.0-SNAPSHOT
Define value for property 'package' com.alpha: : com.alpha.crypto
Confirm properties configuration:
groupId: com.alpha
artifactId: crypto
version: 1.0-SNAPSHOT
package: com.alpha.crypto
Y: : Y
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Old (1.x) Archetype: maven-archetype-quickstart:1.1
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: basedir, Value: D:\code\IDEA_WORKSPACE
[INFO] Parameter: package, Value: com.alpha.crypto
[INFO] Parameter: groupId, Value: com.alpha
[INFO] Parameter: artifactId, Value: crypto
[INFO] Parameter: packageName, Value: com.alpha.crypto
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] project created from Old (1.x) Archetype in dir: D:\code\IDEA_WORKSPACE\crypto
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 01:56 min
[INFO] Finished at: 2017-09-02T19:34:28+08:00
[INFO] Final Memory: 15M/204M
[INFO] ------------------------------------------------------------------------
方式二:明确指定上述信息
D:\code\IDEA_WORKSPACE>mvn archetype:generate -DgroupId=com.alpha -DartifactId=decode-demo -Dversion=1.0-SNAPSHOT -Dpackage=com.alpha.decode -DarchetypeArtifactId=maven-archetype-webapp
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] >>> maven-archetype-plugin:3.0.1:generate (default-cli) > generate-sources @ standalone-pom >>>
[INFO]
[INFO] <<< maven-archetype-plugin:3.0.1:generate (default-cli) < generate-sources @ standalone-pom <<<
[INFO]
[INFO]
[INFO] --- maven-archetype-plugin:3.0.1:generate (default-cli) @ standalone-pom ---
[INFO] Generating project in Interactive mode
[WARNING] No archetype found in remote catalog. Defaulting to internal catalog
[INFO] Using property: groupId = com.alpha
[INFO] Using property: artifactId = decode-demo
[INFO] Using property: version = 1.0-SNAPSHOT
[INFO] Using property: package = com.alpha.decode
Confirm properties configuration:
groupId: com.alpha
artifactId: decode-demo
version: 1.0-SNAPSHOT
package: com.alpha.decode
Y: : y
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Old (1.x) Archetype: maven-archetype-webapp:1.0
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: basedir, Value: D:\code\IDEA_WORKSPACE
[INFO] Parameter: package, Value: com.alpha.decode
[INFO] Parameter: groupId, Value: com.alpha
[INFO] Parameter: artifactId, Value: decode-demo
[INFO] Parameter: packageName, Value: com.alpha.decode
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] project created from Old (1.x) Archetype in dir: D:\code\IDEA_WORKSPACE\decode-demo
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 11.370 s
[INFO] Finished at: 2017-09-02T19:45:32+08:00
[INFO] Final Memory: 15M/208M
[INFO] ------------------------------------------------------------------------
补充:如果缺少相关参数,会在交互过程中要求选择、补全。如果没有指定archetypeArtifactId,会要求选择。
D:\code\IDEA_WORKSPACE>mvn archetype:generate -DgroupId=com.alpha -DartifactId=encode-demo
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] >>> maven-archetype-plugin:3.0.1:generate (default-cli) > generate-sources @ standalone-pom >>>
[INFO]
[INFO] <<< maven-archetype-plugin:3.0.1:generate (default-cli) < generate-sources @ standalone-pom <<<
[INFO]
[INFO]
[INFO] --- maven-archetype-plugin:3.0.1:generate (default-cli) @ standalone-pom ---
[INFO] Generating project in Interactive mode
[WARNING] No archetype found in remote catalog. Defaulting to internal catalog
[INFO] No archetype defined. Using maven-archetype-quickstart (org.apache.maven.archetypes:maven-archetype-quickstart:1.0)
Choose archetype:
1: internal -> org.apache.maven.archetypes:maven-archetype-archetype (An archetype which contains a sample archetype.)
2: internal -> org.apache.maven.archetypes:maven-archetype-j2ee-simple (An archetype which contains a simplifed sample J2EE application.)
3: internal -> org.apache.maven.archetypes:maven-archetype-plugin (An archetype which contains a sample Maven plugin.)
4: internal -> org.apache.maven.archetypes:maven-archetype-plugin-site (An archetype which contains a sample Maven plugin site.
This archetype can be layered upon an existing Maven plugin project.)
5: internal -> org.apache.maven.archetypes:maven-archetype-portlet (An archetype which contains a sample JSR-268 Portlet.)
6: internal -> org.apache.maven.archetypes:maven-archetype-profiles ()
7: internal -> org.apache.maven.archetypes:maven-archetype-quickstart (An archetype which contains a sample Maven project.)
8: internal -> org.apache.maven.archetypes:maven-archetype-site (An archetype which contains a sample Maven site which demonstrates
some of the supported document types like APT, XDoc, and FML and demonstrates how
to i18n your site. This archetype can be layered upon an existing Maven project.)
9: internal -> org.apache.maven.archetypes:maven-archetype-site-simple (An archetype which contains a sample Maven site.)
10: internal -> org.apache.maven.archetypes:maven-archetype-webapp (An archetype which contains a sample Maven Webapp project.)
Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): 7: 7
[INFO] Using property: groupId = com.alpha
[INFO] Using property: artifactId = encode-demo
Define value for property 'version' 1.0-SNAPSHOT: :
[INFO] Using property: package = com.alpha
Confirm properties configuration:
groupId: com.alpha
artifactId: encode-demo
version: 1.0-SNAPSHOT
package: com.alpha
Y: :
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Old (1.x) Archetype: maven-archetype-quickstart:1.1
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: basedir, Value: D:\code\IDEA_WORKSPACE
[INFO] Parameter: package, Value: com.alpha
[INFO] Parameter: groupId, Value: com.alpha
[INFO] Parameter: artifactId, Value: encode-demo
[INFO] Parameter: packageName, Value: com.alpha
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] project created from Old (1.x) Archetype in dir: D:\code\IDEA_WORKSPACE\encode-demo
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 34.825 s
[INFO] Finished at: 2017-09-02T19:51:55+08:00
[INFO] Final Memory: 15M/202M
[INFO] ------------------------------------------------------------------------
常用指令
compile
执行default-resources
default-compile
,生成classes目录、maven-status目录
test
执行default-resources
default-compile
default-testResources
default-testCompile
default-test
,生成test-class、surefire-reports目录
clean
删除target目录
package
执行default-resources
default-compile
default-testResources
default-testCompile
default-test
default-jar
,生成maven-archiver目录、artifactId-version.jar文件
install
安装到本地仓库
小结
Maven不仅仅是构建工具,还是一个依赖管理工具和项目信息管理工具。