Maven入门2-pom.xml文件与settings.xml文件
Maven入门2-pom.xml文件与settings.xml文件
本文内容来源于官网文档部分章节,settings.xml文件:参考http://maven.apache.org/settings.html,pom.xml文件参考:http://maven.apache.org/guides/introduction/introduction-to-the-pom.html。
http://maven.apache.org/pom.html;一个是POM的简单介绍,一个是详细介绍。
下面针对几个主要的地方做介绍:
转载请注明出处:http://www.cnblogs.com/liun1994/
1、settings.xml文件
settings.xml文件包含多个不同的标签,用来配置maven的执行,包含两个settings.xml文件,一个在${user.home}/.m2/settings.xml目录下,一个在${maven.home}/conf/settings.xml目录下;第一个称为global settings,第二个称为user settings,如果两个都存在的话,将会合并,并以user settings为主导;settings.xml文件的内容通过配置的环境变量来解释执行。下面是settings.xml文件的一个预览:
<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
https://maven.apache.org/xsd/settings-1.0.0.xsd">
<localRepository/>
<interactiveMode/>
<usePluginRegistry/>
<offline/>
<pluginGroups/>
<servers/>
<mirrors/>
<proxies/>
<profiles/>
<activeProfiles/>
</settings>
转载请注明出处:http://www.cnblogs.com/liun1994/
1)简单值配置:
<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
https://maven.apache.org/xsd/settings-1.0.0.xsd">
<localRepository>${user.home}/.m2/repository</localRepository><!-- 系统本地仓库地址 -->
<interactiveMode>true</interactiveMode><!-- 是否允许与用户的输入进行互动,默认true -->
<usePluginRegistry>false</usePluginRegistry><!-- 是否允许使用${user.home}/.m2/plugin-registry.xml文件管理插件的版本,默认为false -->
<offline>false</offline><!-- 是否允许在离线模式运行,这在无法与远程仓库连接时很有用 -->
...
</settings>
2)插件组:
pluginGroup可以有好多个,默认包括org.apache.maven.plugins和org.codehaus.mojo。
<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
https://maven.apache.org/xsd/settings-1.0.0.xsd">
...
<pluginGroups>
<pluginGroup>org.mortbay.jetty</pluginGroup>
</pluginGroups>
...
</settings> 配置插件之后可以在命令行使用:mvn jerry:run
3)servers配置
下载和部署的仓库,由POM文件的repositories和distributionManagement标签定义,然而某些设置却不能在这个文件下设置;比如username,password等,这些信息在服务器的settings.xml文件中配置。
<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
https://maven.apache.org/xsd/settings-1.0.0.xsd">
...
<servers>
<server>
<id>server001</id><!-- 与mirror/repository的id匹配 -->
<username>my_login</username>
<password>my_password</password>
<privateKey>${user.home}/.ssh/id_dsa</privateKey>
<passphrase>some_passphrase</passphrase>
<filePermissions>664</filePermissions>
<directoryPermissions>775</directoryPermissions>
<configuration></configuration>
</server>
</servers>
...
</settings>
4)mirrors
更多关于mirrors的配置可参考此处:http://maven.apache.org/guides/mini/guide-mirror-settings.html
如果是国内的网,可能会遇到无法访问的问题,这时可换成国内的mirror。
<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
https://maven.apache.org/xsd/settings-1.0.0.xsd">
...
<mirrors>
<mirror>
<id>planetmirror.com</id> <!-- 当连接此mirror时使用server的配置 -->
<name>PlanetMirror Australia</name>
<url>http://downloads.planetmirror.com/pub/maven2</url>
<mirrorOf>central</mirrorOf>
</mirror>
</mirrors>
...
</settings>
其他的一些配置,可去官网链接查询!
2、pom.xml文件
1)简单介绍
POM文件是Project Object Model的简称,它包含了使用Maven进行项目管理的详细信息,它包含许多默认的值,比如编译路径默认为:target;源码路径src/main/java;测试路径:src/test/java。
所有的pom.xml文件都继承Super POM,Super POM是Maven默认的Pom,下面是内嵌POM的一个预览,版本为3.0.4:
默认POM配置了一些路径,插件,默认打包方式为jar。转载请注明出处:http://www.cnblogs.com/liun1994/
<project>
<modelVersion>4.0.0</modelVersion> <repositories>
<repository>
<id>central</id>
<name>Central Repository</name>
<url>http://repo.maven.apache.org/maven2</url>
<layout>default</layout>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories> <pluginRepositories>
<pluginRepository>
<id>central</id>
<name>Central Repository</name>
<url>http://repo.maven.apache.org/maven2</url>
<layout>default</layout>
<snapshots>
<enabled>false</enabled>
</snapshots>
<releases>
<updatePolicy>never</updatePolicy>
</releases>
</pluginRepository>
</pluginRepositories> <build>
<directory>${project.basedir}/target</directory>
<outputDirectory>${project.build.directory}/classes</outputDirectory>
<finalName>${project.artifactId}-${project.version}</finalName>
<testOutputDirectory>${project.build.directory}/test-classes</testOutputDirectory>
<sourceDirectory>${project.basedir}/src/main/java</sourceDirectory>
<scriptSourceDirectory>src/main/scripts</scriptSourceDirectory>
<testSourceDirectory>${project.basedir}/src/test/java</testSourceDirectory>
<resources>
<resource>
<directory>${project.basedir}/src/main/resources</directory>
</resource>
</resources>
<testResources>
<testResource>
<directory>${project.basedir}/src/test/resources</directory>
</testResource>
</testResources>
<pluginManagement>
<!-- NOTE: These plugins will be removed from future versions of the super POM -->
<!-- They are kept for the moment as they are very unlikely to conflict with lifecycle mappings (MNG-4453) -->
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.3</version>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2-beta-5</version>
</plugin>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.1</version>
</plugin>
<plugin>
<artifactId>maven-release-plugin</artifactId>
<version>2.0</version>
</plugin>
</plugins>
</pluginManagement>
</build> <reporting>
<outputDirectory>${project.build.directory}/site</outputDirectory>
</reporting> <profiles>
<!-- NOTE: The release profile will be removed from future versions of the super POM -->
<profile>
<id>release-profile</id> <activation>
<property>
<name>performRelease</name>
<value>true</value>
</property>
</activation> <build>
<plugins>
<plugin>
<inherited>true</inherited>
<artifactId>maven-source-plugin</artifactId>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<inherited>true</inherited>
<artifactId>maven-javadoc-plugin</artifactId>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<inherited>true</inherited>
<artifactId>maven-deploy-plugin</artifactId>
<configuration>
<updateReleaseInfo>true</updateReleaseInfo>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles> </project>
最小化配置:
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1</version>
</project>
如果使用最小化配置,没有指定repositories,那么将会使用Super POM的配置下载库及插件,即http://repo.maven.apache.org/maven2。
几个例子:Maven中有继承和聚合的概念,对应两个标签:parent, module。
Example1:
目录结构:
.
|-- my-module
| `-- pom.xml
`-- pom.xml
配置:
<project>
<parent>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>my-module</artifactId>
</project> Example2:
目录结构:
.
|-- my-module
| `-- pom.xml
`-- parent
`-- pom.xml
配置:
<project>
<parent>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1</version>
<relativePath>../parent/pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>my-module</artifactId>
</project>
聚合:
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1</version>
<packaging>pom</packaging>
<modules>
<module>my-module</module>
</modules>
</project>
详情参考:http://maven.apache.org/guides/introduction/introduction-to-the-pom.html
2)标签预览
共分四类,下面介绍几个比较重要的;
<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> <!-- The Basics -->
<groupId>...</groupId>
<artifactId>...</artifactId>
<version>...</version>
<packaging>...</packaging>
<dependencies>...</dependencies>
<parent>...</parent>
<dependencyManagement>...</dependencyManagement>
<modules>...</modules>
<properties>...</properties> <!-- Build Settings -->
<build>...</build>
<reporting>...</reporting> <!-- More Project Information -->
<name>...</name>
<description>...</description>
<url>...</url>
<inceptionYear>...</inceptionYear>
<licenses>...</licenses>
<organization>...</organization>
<developers>...</developers>
<contributors>...</contributors> <!-- Environment Settings -->
<issueManagement>...</issueManagement>
<ciManagement>...</ciManagement>
<mailingLists>...</mailingLists>
<scm>...</scm>
<prerequisites>...</prerequisites>
<repositories>...</repositories>
<pluginRepositories>...</pluginRepositories>
<distributionManagement>...</distributionManagement>
<profiles>...</profiles>
</project>
在POM文件中,repositories标签配置去哪下载依赖库及插件,但是如果在settings.xml配置了mirror的话,就会去mirror地址下载,相当于拦截器;
Maven仓库分两类:
repository里存放的都是各种jar包和maven插件。当向仓库请求插件或依赖的时候,会先检查local repository,如果local repository有则直接返回,否则会向remote repository请求,并缓存到local repository。
mirror相当于一个拦截器,它会拦截maven对remote repository的相关请求,把请求里的remote repository地址,重定向到mirror里配置的地址。
<mirrorOf></mirrorOf>标签里面放置的是要被镜像的Repository ID。为了满足一些复杂的需求,Maven还支持更高级的镜像配置:
3、小结
下面是我学习Maven时的步骤,同时也适用于其他技术:
1)搜索相关博客、资料,了解一下概念性的知识;
2)安装与简单配置,阅读官方文档中感兴趣的部分;
3)对于不理解的部分,搜索相关的资料,搞明白;
4)暂时告一段落,下次用到时如果出错,一方面从官方文档找配置,另一方面搜索质量高的博客借鉴。
转载请注明出处:http://www.cnblogs.com/liun1994/
Maven入门2-pom.xml文件与settings.xml文件的更多相关文章
- Maven入门-3.pom文件和settings文件
1.pom.xml文件介绍2.settings.xml文件介绍 1.pom.xml文件介绍 Maven项目的核心是pom.xml,pom(Project Object Model项目对象模型) pom ...
- maven在windows环境下加载settings.xml文件
今天发现maven在windows环境下加载的settings.xml文件是c:下的,就算修改conf下的settings.xml里的<localRepository>给他明确指向也没用.
- Maven的相关问题(一)——settings.xml配置详解
工作中第一次正式接触maven,虽然之前在学习时有遇到过,但是对于maven的认识和理解确实太浅薄,仅仅局限于机械式的操,纸上得来终觉浅,绝知此事要躬行···古人诚不欺我也~ 下面先贴一个找到的一个非 ...
- maven的几个重要配置文件pom.xml、settings.xml;Maven打包生成包含所有依赖的jar包
一个java项目通过maven自动下载依赖时,会涉级读取三个配置文件,分别是项目下的pom.xml 文件 .用户家目录下的.m2/settings.xml 与 maven 全局配置settings.x ...
- Maven中settings.xml文件各标签含义
原文地址:http://www.cnblogs.com/jingmoxukong/p/6050172.html?utm_source=gold_browser_extension settings.x ...
- maven的settings.xml文件
settings.xml是maven的配置文件.一般我们在网上下载的maven包解压以后,conf文件里面的有个setting.xml文件,通常这个settings.xml文件中会有你的本地仓库会在哪 ...
- maven全局配置文件settings.xml详解
概要 settings.xml有什么用? 如果在Eclipse中使用过Maven插件,想必会有这个经验:配置settings.xml文件的路径. settings.xml文件是干什么的,为什么要配置它 ...
- [转]maven全局配置文件settings.xml详解
概要 settings.xml有什么用? 如果在Eclipse中使用过Maven插件,想必会有这个经验:配置settings.xml文件的路径. Paste_Image.png settings.xm ...
- Apache Maven(七):settings.xml
settings.xml 文件中包含settings标签,这个标签可以配置如何去执行Maven.其中包括本地存储库位置,备用远程存储库服务器和身份验证信息等值. 有如下两个位置可能存放这setting ...
随机推荐
- mxnet:结合R与GPU加速深度学习(转)
近年来,深度学习可谓是机器学习方向的明星概念,不同的模型分别在图像处理与自然语言处理等任务中取得了前所未有的好成绩.在实际的应用中,大家除了关心模型的准确度,还常常希望能比较快速地完成模型的训练.一个 ...
- 第40篇 使用Sublime+MarkDown快速写博客
原文地址:http://blog.laofu.online/2017/06/03/how-use-sublime/ 前端的开发人员应该都知道sublime的神器,今天就说说如何使用sublime结合m ...
- Redis中的数据结构与常用命令
开发系统:Ubuntu 17.04Redis驱动:StackExchange.Redis 1.2.3Redis版本:3.2.1开发平台:.NET Core 对于Redis的介绍这里只写一句:Redis ...
- #416 Div2 C
#416 Div2 C 题意 一些人去坐车,它们已经按给定顺序排队,每个人可能去不同的目的地,去同一目的地的人一定要被分成一组(去不同目的地的也可被分到同一组),对分好的每一组所有不同的目的地序号作异 ...
- Java常用类之【日期相关类】
一.日期类 Java语言提供了2个类来处理日期 Date类 Date类以毫秒来表示特定的日期 构造方法 Date date = new Date(); System.out.println(date) ...
- 利用React/anu编写一个弹出层
本文将一步步介绍如何使用React或anu创建 一个弹出层. React时代,代码都是要经过编译的,我们很多时间都耗在babel与webpack上.因此本文也介绍如何玩webpack与babel. 我 ...
- 用JQuery写的滚动条,可以改变样式哦!
很早之前在做项目的时候要用到自定义的滚动条,可是现在的CSS2只能改改颜色什么的,对于改变形状或者更高级的用法根本不可能实现,没办法只能自己写一个了.(好像CSS3可以该形状,不过没研究过有兴趣的童鞋 ...
- Django中通过filter和simple_tag为前端实现自定义函数
Django的模板引擎提供了一般性的功能函数,通过前端可以实现多数的代码逻辑功能,这里称之为一般性,是因为它仅支持大多数常见情况下的函数功能,例如if判断,ifequal对比返回值等,但是稍微复杂一些 ...
- [BZOJ4518]征途
4518: [Sdoi2016]征途 Time Limit: 10 Sec Memory Limit: 256 MB Description Pine开始了从S地到T地的征途. 从S地到T地的路可以 ...
- Vulkan Tutorial 17 Rendering and presentation
操作系统:Windows8.1 显卡:Nivida GTX965M 开发工具:Visual Studio 2017 Setup 这一章节会把之前的所有内容进行整合.我们将会编写drawFrame函数, ...