Maven: POM, Setting, Repository

来源:互联网 发布:华美淘宝客 编辑:程序博客网 时间:2024/06/05 05:27

概念

POM

POM代表面向项目的模型“project Object Model”。每一个Maven项目中都维护这一个pom.xml,Maven项目包括配置文件,依赖关系,代码位置,等等。在Maven里面中维护好这个pom.xml文件就可以管理好项目的所有元素了。
- 具体POM XML中元素的解释参考:http://maven.apache.org/pom.html
- POM XML对应的xml schema: http://maven.apache.org/xsd/maven-4.0.0.xsd

Setting

在settings.xml文件中的settings元素包含了Maven执行时的参数,就像pom.xml。settings包括了local repository, remote repository,mirror和验证等信息。
settings.xml存在如下两个地方:
- Maven的安装目录:$M2_HOME/conf/settings.xml(全局配置)
- 用户目录: ${user.home}/.m2/settings.xml(用户级别的配置))
具体的Setting字段的配置:https://maven.apache.org/settings.html

Repository

Repository Manager是一个专门的服务器,用来管理两进制组件。
在Mavan中,Repository Manager可以用来如下目的:
- 用来作为公共的Maven Repositories.
- 用来depoly我们的Maven项目到Repository中,进行统一管理。
具体说明: https://maven.apache.org/repository-management.html

实例

选择Nexus SSO作为Repository Manager,然后depoly自己的一个plugin项目到此Repository Manager中。

步骤

1.下载Nexus SSO: http://www.sonatype.org/nexus/go/. 选择Windows版本ZIP版本,解压后运行。

D:\webServers\nexus-2.11.3-01-bundle\nexus-2.11.3-01\bin>nexus.bat startwrapper  | The nexus-webapp service is not installed - The specified service does not exist as an installed service. (0x424)Press any key to continue . . .D:\webServers\nexus-2.11.3-01-bundle\nexus-2.11.3-01\bin>nexus.bat installwrapper  | nexus installed.D:\webServers\nexus-2.11.3-01-bundle\nexus-2.11.3-01\bin>nexus.bat startwrapper  | Starting the nexus service...wrapper  | Waiting to start...wrapper  | Waiting to start...wrapper  | Waiting to start...wrapper  | Waiting to start...wrapper  | nexus started.

nexus需要install后,再start. 启动成功后,变可以访问Nexus服务器了。
2.访问Nexus http://localhost:8081/nexus 。使用admin/admin123登录后,进行管理和查看。
Nexus里面自带的Repository可以基本满足需求。我会用到如下的两个Repository:
- Snapshots: http://localhost:8081/nexus/content/repositories/releases/
- Releases: http://localhost:8081/nexus/content/repositories/snapshots/
Snapshots用于存放临时不稳定版本,Releases用于存放发布的稳定版本。
3.配置 pom.xml文件,用于depoly时artifact的分发。

<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">   <groupId>com.lewi</groupId>  <artifactId>plugin</artifactId>  <version>1.2-SNAPSHOT</version>  <packaging>maven-plugin</packaging> ... <distributionManagement>     <repository>          <id>nexus</id>          <name>Nexus Release Repository</name>          <url>http://localhost:8081/nexus/content/repositories/releases/</url>      </repository>     <snapshotRepository>          <id>nexus</id>          <name>Nexus Snapshot Repository</name>          <url>http://localhost:8081/nexus/content/repositories/snapshots/</url>      </snapshotRepository> </distributionManagement>...</project>

4.配置settings.xml中的server,用于pom.xml中server的认证。

<servers>    <server>      <id>nexus</id>      <username>deployment</username>      <password>deployment123</password>    </server></servers>

5.运行配置好的pom.xml对应项目,mvn deploy. 可以查看到build成功。然后再到Repository Manager中的Snapshots下,可以看到到编辑后的artifact存在了。如下图。
Nexus截图

总结

本文首先说明了Maven中主要元素和配置的概念,然后用一个具体的Repository Manager实例,演示了Maven如何和具体的Repository结合。

Reference:
http://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html

0 0