1 dependencyManagement 和dependencies区别

1、dependencies即使在子项目中不写该依赖项,那么子项目仍然会从父项目中继承该依赖项(全部继承)


2、dependencyManagement里只是声明依赖,并不实现引入,因此子项目需要显示的声明需要用的依赖。
如果不在子项目中声明依赖,是不会从父项目中继承下来的;只有在子项目中写了该依赖项,
并且没有指定具体版本,才会从父项目中继承该项,
并且version和scope都读取自父pom;另外如果子项目中指定了版本号,那么会使用子项目中指定的jar版本。

2 在POM中配置远程仓库(repositories标签和pluginRepositories标签)

前面我们看到超级POM配置了ID为central的远程仓库,
我们可以在POM中配置其它的远程仓库。

这样做的原因有很多,比如你有一个局域网的远程仓库,使用该仓库能大大提高***,继而提高构建速度,也有可能你依赖的一个jar在central中找不到,它只存在于某个特定的公共仓库,这样你也不得不添加那个远程仓库的配置。

这里我配置一个远程仓库指向中央仓库的中国镜像:

<project>
...
  <repositories>
    <repository>
      <id>maven-net-cn</id>
      <name>Maven China Mirror</name>
      <url>http://maven.net.cn/content/groups/public/</url>
      <releases>
        <enabled>true</enabled>
      </releases>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
    </repository>
  </repositories>
  <pluginRepositories>
    <pluginRepository>
      <id>maven-net-cn</id>
      <name>Maven China Mirror</name>
      <url>http://maven.net.cn/content/groups/public/</url>
      <releases>
        <enabled>true</enabled>
      </releases>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>    
    </pluginRepository>
  </pluginRepositories>
...
</project>

解释里面的标签的意思

releases标签 和snapshots标签

一个仓库一般分为public(Release)仓和SNAPSHOT仓,前者存放正式版本,后者存放快照版本

我们先看一下<repositories>的配置,你可以在它下面添加多个<repository> ,
每个<repository>都有它唯一的ID,一个描述性的name,以及最重要的,远程仓库的url。
此外,<releases><enabled>true</enabled></releases>
告诉Maven可以从这个仓库下载releases版本的构件,
而<snapshots><enabled>false</enabled></snapshots>告诉Maven不要从这个仓库下载snapshot版本的构件。
禁止从公共仓库下载snapshot构件是推荐的做法,因为这些构件不稳定,且不受你控制,你应该避免使用
。当然,如果你想使用局域网内组织内部的仓库,你可以激活snapshot的支持。

总结

我们可以在我们项目里面的pom文件里面配置 远程仓库。配置的时候在<repositories>标签和 
 <pluginRepositories> 标签里面配置。里面可以配置多个地址。

至于<pluginRepositories>,这是配置Maven从什么地方下载插件构件
(Maven的所有实际行为都由其插件完成)。该元素的内部配置和<repository>完全一样,不再解释。

3 build标签里面可以写什么

打包出来的jar包,名字是什么,我们可以在这个里面设置

finalName标签

   build目标文件的名称,默认情况为${artifactId}-${version}   ,可以自己随便写个
<build>
        <finalName>${
   artifactId}-${
   version}</finalName>
</build>

基本标签

<build>
        <defaultGoal>install</defaultGoal>
        <directory>${basedir}/target</directory>
        <finalName>${artifactId}-${version}</finalName>
        <filters>
                <filter>filters/filter1.properties</filter>
        </filters>
         ...
</build>

1)defaultGoal
执行build任务时,如果没有指定目标,将使用的默认值。

如上配置:在命令行中执行mvn,则相当于执行mvn install

2)directory

 build目标文件的存放目录,默认在${basedir}/target目录

3)finalName

build目标文件的名称,默认情况为${artifactId}-${version}

4)filter

定义*.properties文件,包含一个properties列表,该列表会应用到支持filter的resources中。

也就是说,定义在filter的文件中的name=value键值对,会在build时代替${name}值应用到resources中。

                 maven的默认filter文件夹为${basedir}/src/main/filters

Resources标签里面的配置

里面配置的东西就是,在打包为jar包的时候,哪些资源文件就可以打包,哪些资源不打包,就是在这个标签里面进行配置的。

<resources>
			<!-- 打包包含xml文件-->
			<resource>
				<directory>src/main/java</directory>
				<includes>
					<include>**/*.xml</include>
				</includes>
				<filtering>false</filtering>
			</resource>
			<resource>
				<directory>src/main/resources</directory>
				<filtering>true</filtering>
				<excludes>
					<exclude>static/**</exclude>
				</excludes>
			</resource>
			<resource>
				<directory>src/main/resources</directory>
				<filtering>false</filtering>
				<includes>
					<include>static/**</include>
				</includes>
			</resource>
			<resource>
				<directory>src/main/webapp</directory>
				<filtering>false</filtering>
			</resource>
		</resources>

1)resources
一个resources元素的列表。每一个都描述与项目关联的文件是什么和在哪里

2)targetPath

                指定build后的resource存放的文件夹,默认是basedir。

                通常被打包在jar中的resources的目标路径是META-INF

3)filtering

                true/false,表示为这个resource,filter是否激活

4)directory

                定义resource文件所在的文件夹,默认为${basedir}/src/main/resources

5)includes

                指定哪些文件将被匹配,以*作为通配符

6)excludes

               指定哪些文件将被忽略

7)testResources

               定义和resource类似,只不过在test时使用

plugins标签里面的配置

我们要在项目里面使用插件,就可以在这个里面配置我们要使用的插件

<build>
    ...
	<plugins>
		<plugin>
根据这3个属性标签定义使用哪个插件
			<groupId>org.apache.maven.plugins</groupId>
			<artifactId>maven-jar-plugin</artifactId>
			<version>2.0</version>

设置是否加载这个插件的  扩展,默认不扩展
			<extensions>false</extensions>
是否让子pom继承,ture or false 默认为true.
			<inherited>true</inherited>
  配置该plugin期望得到的properties
			<configuration>
				<classifier>test</classifier>
			</configuration>

与pom基础的dependencies的结构和功能都相同,只是plugin的dependencies用于plugin,
而pom的denpendencies用于项目本身。在plugin的dependencies主要用于改变plugin
原来的dependencies,例如排除一些用不到的dependency或者修改dependency的版本等


			<dependencies>...</dependencies>

plugin也有很多个目标,每个目标具有不同的配置,executions就是设定plugin的目标,
			<executions>...</executions>
		</plugin>
    </plugins>
</build>

pluginManagement配置

pluginManagement的作用类似于denpendencyManagement,只是denpendencyManagement是用于管理项目jar包依赖,pluginManagement是用于管理plugin。与pom build里的plugins区别是,这里的plugin是列出来,然后让子pom来决定是否引用。