首页 > 代码库 > Maven入门2-pom.xml文件与settings.xml文件
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文件