Maven 初学(一)基本概念

Pom中有三个主要元素

Groupid,artifactid,version

goupid 是一个组织唯一的标识  例如 com.ibm.www

artifactid  是一个工程呢ID        ibm-project

version   代表一个版本  例如    com.ibm.www.ibm-project.1.0

maven执行一个目标(goal)有以下几个步骤

prepare-resources    资源的拷贝

compile   源代码拷贝阶段

package  创建jar/war包阶段

install      安装包到本地或者远程库中

mvn clean dependency:copy-dependencies package
 
clean 首先被执行,
然后dependency:copy-dependencies被执行
然后是package被执行
clean 的 Lifecycle
pre-clean
clean 
post-clean 
 
默认 build Lifecycle
validate    验证工程是否正确,所有必要的信息是否有效
Lifecycle PhaseDescription
validateValidates whether project is correct and all necessary information is available to complete the build process.
initializeInitializes build state, for example set properties
generate-sourcesGenerate any source code to be included in compilation phase.
process-sourcesProcess the source code, for example, filter any value.
generate-resourcesGenerate resources to be included in the package.
process-resourcesCopy and process the resources into the destination directory, ready for packaging phase.
compileCompile the source code of the project.
process-classesPost-process the generated files from compilation, for example to do bytecode enhancement/optimization on Java classes.
generate-test-sourcesGenerate any test source code to be included in compilation phase.
process-test-sourcesProcess the test source code, for example, filter any values.
test-compileCompile the test source code into the test destination directory.
process-test-classesProcess the generated files from test code file compilation.
testRun tests using a suitable unit testing framework(Junit is one).
prepare-packagePerform any operations necessary to prepare a package before the actual packaging.
packageTake the compiled code and package it in its distributable format, such as a JAR, WAR, or EAR file.
pre-integration-testPerform actions required before integration tests are executed. For example, setting up the required environment.
integration-testProcess and deploy the package if necessary into an environment where integration tests can be run.
post-integration-testPerform actions required after integration tests have been executed. For example, cleaning up the environment.
verifyRun any check-ups to verify the package is valid and meets quality criterias.
installInstall the package into the local repository, which can be used as a dependency in other projects locally.
deployCopies the final package to the remote repository for sharing with other developers and projects.

local Repository 本地库

可以在settings文件中设置本地库位置

<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 
   http://maven.apache.org/xsd/settings-1.0.0.xsd">
      <localRepository>C:/MyLocalRepository</localRepository>
</settings>


Central Repository  中心库

maven在本地库中不能找到依赖的时候,就回去中心库中寻找

http://repo1.maven.org/maven2/

Remote Repository远程库

<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>com.companyname.projectgroup</groupId>
   <artifactId>project</artifactId>
   <version>1.0</version>
   <dependencies>
      <dependency>
         <groupId>com.companyname.common-lib</groupId>
         <artifactId>common-lib</artifactId>
         <version>1.0.0</version>
      </dependency>
   <dependencies>
   <repositories>
      <repository>
         <id>companyname.lib1</id>
         <url>http://download.companyname.org/maven2/lib1</url>
      </repository>
      <repository>
         <id>companyname.lib2</id>
         <url>http://download.companyname.org/maven2/lib2</url>
      </repository>
   </repositories>
</project>


使用archetype插件创建工程

C:MVN>mvn archetype:generate
-DgroupId=com.companyname.bank 
-DartifactId=consumerBanking 
-DarchetypeArtifactId=maven-archetype-quickstart 
-DinteractiveMode=false


 参考自:http://www.tutorialspoint.com/maven/maven_quick_guide.htm

原文地址:https://www.cnblogs.com/mengjianzhou/p/5986830.html