pom.xml和conf/settings.xml

来源:互联网 发布:开淘宝的经验分享 编辑:程序博客网 时间:2024/06/06 03:37

一、pom.xml

 1 <project xmlns="http://maven.apache.org/POM/4.0.0" 2          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 4             http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5     <modelVersion>4.0.0</modelVersion> 6      7     <!-- 基本设置 --> 8     <groupId>...</groupId> 9     <artifactId>...</artifactId>10     <version>...</version>11     <packaging>...</packaging>12     <dependencies>...</dependencies>13     <parent>...</parent>14     <dependencyManagement>...</dependencyManagement>15     <modules>...</modules>16     <properties>...</properties>17     18     <!-- 构建过程的设置 -->19     <build>...</build>20     <reporting>...</reporting>21     22     <!-- 项目信息设置 -->23     <name>...</name>24     <description>...</description>25     <url>...</url>26     <inceptionYear>...</inceptionYear>27     <licenses>...</licenses>28     <organization>...</organization>29     <developers>...</developers>30     <contributors>...</contributors>31     32     <!-- 环境设置 -->33     <issueManagement>...</issueManagement>34     <ciManagement>...</ciManagement>35     <mailingLists>...</mailingLists>36     <scm>...</scm>37     <prerequisites>...</prerequisites>38     <repositories>...</repositories>39     <pluginRepositories>...</pluginRepositories>40     <distributionManagement>...</distributionManagement>41     <profiles>...</profiles>42 </project>
  1. 添加远程仓库
<project ...><repositories>    <!-- java.net远程仓库 -->    <repository>      <id>java.net</id>      <url>https://maven.java.net/content/repositories/public/</url>    </repository>    <!-- JBoss远程仓库 -->    <repository>    <id>JBoss repository</id>    <url>http://repository.jboss.org/nexus/content/groups/public/</url>      </repository> </repositories></project>
  1. <dependency>: 依赖关系
<dependencies>     <dependency>     <!-- maven坐标:groupId,artifactId,version -->     <groupId>junit</groupId>       <artifactId>junit</artifactId>       <version>4.0</version>       <!-- scope:compile(default),provided,runtime,test,system -->     <scope>test</scope>     </dependency></dependencies>
  1. <parent>继承关系
    [...]      <parent>        <groupId>com.parent</groupId>        <artifactId>parent</artifactId>        <version>1.0-SNAPSHOT</version>      </parent>      [...]  
  1. <module>: 聚合关系
  <groupId>com.parent</groupId>    <artifactId>parent</artifactId>    <version>1.0-SNAPSHOT</version>    <modules>      <module>my-project<module>    </modules> 
  1. <build>: 构建设置
//两种<build>标签  <!– (1):"Project Build" contains more elements than just the BaseBuild set –>    <build></build>    <profiles>      <profile>        <!– (2):"Profile Build" contains a subset of "Project Build"s elements –>        <build></build>      </profile>    </profiles> 

build中的主要标签:Resources和Plugins。
Resources:用于排除或包含某些资源文件

<resources>    <resource>      <targetPath>META-INF/plexus</targetPath>      <filtering>false</filtering>      <directory>${basedir}/src/main/plexus</directory>      <includes>        <include>configuration.xml</include>      </includes>      <excludes>        <exclude>**/*.properties</exclude>      </excludes>    </resource>  </resources> 

Plugins:设置构建的插件

    <build><plugins>           <plugin>             <groupId>org.apache.maven.plugins</groupId>             <artifactId>maven-jar-plugin</artifactId>             <version>2.0</version>             <extensions>false</extensions>             <inherited>true</inherited>             <configuration>               <classifier>test</classifier>             </configuration>             <dependencies></dependencies>             <executions></executions>           </plugin>  
  1. 快照版本和正式版本
maven2会根据模块的版本号(pom文件中的version)中是否带有-SNAPSHOT来判断是快照版本还是正式版本。1)如果是快照版本,那么在mvn deploy时会自动发布到快照版本库中,而使用快照版本的模块,在不更改版本号的情况下,直接编译打包时,maven会自动从镜像服务器上下载最新的快照版本。2)如果是正式发布版本,那么在mvn deploy时会自动发布到正式版本库中,而使用正式版本的模块,在不更改版本号的情况下,编译打包时如果本地已经存在该版本的模块则不会主动去镜像服务器上下载。所以,我们在开发阶段,可以将公用库的版本设置为快照版本,而被依赖组件则引用快照版本进行开发,在公用库的快照版本更新后,我们也不需要修改pom文件提示版本号来下载新的版本,直接mvn执行相关编译、打包命令即可重新下载最新的快照库了,从而也方便了我们进行开发。

二、conf/settings.xml
1. <profile>: 当profile定义在settings.xml中时意味着该profile是全局的,它会对所有项目或者某一用户的所有项目都产生作用。因为它是全局的,所以在settings.xml中只能定义一些相对而言范围宽泛一点的配置信息,比如远程仓库等。而一些比较细致一点的需要根据项目的不同来定义的就需要定义在项目的pom.xml中。具体而言,能够定义在settings.xml中的信息有”repositories”、”pluginRepositories”和”properties”。定义在”properties”里面的键值对可以在pom.xml中使用。
参考: http://elim.iteye.com/blog/1900568
2. maven repository(远程仓库配置)参考: http://blog.csdn.net/joewolf/article/details/4876604
3. 顶层 pom 中的 dependencies 与 dependencyManagement 中的 dependencies 元素有一个重要的区别:
<dependencyManagement> 中的 <dependencies>只表明依赖项版本的优先选择,并不影响项目的依赖项;而 <dependencies> 元素则影响项目的依赖项。
只有当 外层的dependencies 元素中没有指明版本信息时, dependencyManagement 中的 dependencies 元素才起作用。
参考: http://mushiqianmeng.blog.51cto.com/3970029/720470/
4.

0 0
原创粉丝点击