pom作為項目對象模型。通過xml表示maven項目,使用pom.xml來實現。
主要描述了項目:包括的配置文件;開發者需要遵循的規則,缺陷管理系統,組織和licenses,項目的url,項目的依賴性,以及其他所有的項目相關因素。
一個基本項目的pom.xml文件,通常至少有三個部分:
第一部分:項目坐標,信息描述等
<modelVersion>4.0.0</modelVersion>
<groupId>com.company.project</groupId> //com.公司名.項目名
<artifactId>module</artifactId> //功能模塊名
<packaging>war</packaging> //項目打包的后綴,war是web項目發布用的,默認為jar
<version>0.0.1-SNAPSHOT</version> //artifact模塊的版本
<name>test Maven Webapp</name> //相當于項目描述,可刪除
<url>http://maven.apache.org</url> ////相當于項目描述,可刪除
//父項目的坐標
//group id + artifact id +version :項目在倉庫中的坐標
<parent>
<groupId>com.xxx.xxx</groupId>
<artifactId>xxxx</artifactId>
<version>1.0.0</version>
</parent>
//項目開發者屬性,如即時消息如何處理等
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<poi.version>3.16</poi.version>
<docker.image.prefix>pms</docker.image.prefix>
<spring-cloud.version>Dalston.SR4</spring-cloud.version>
</properties>
第二部分:引入jar包
//dependency:引入資源jar包到本地倉庫,要引入更多資源就在<dependencies>中繼續增加<dependency>
//group id+artifact id+version:資源jar包在倉庫中的坐標
//scope:作用范圍,test指該jar包僅在maven測試時使用,發布時會忽略這個包。需要發布的jar包可以忽略這一配置
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
需要引入什么包可以來這個地址搜索http://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-eureka/1.4.5.RELEASE
這個網頁有如下代碼,可以直接復制粘貼。
image
第三部分,構建項目
//build:項目構建時的配置
//finalName:在瀏覽器中的訪問路徑,如果將它改成helloworld,再執行maven--update,這時運行項目的訪問路徑是 http://localhost:8080/helloworld/ 而不是項目名的 http://localhost:8080/test
//plugins:插件,之前篇章已經說過,第一個插件是用來設置java版本為1.7,第二個插件是我剛加的,用來設置編碼為utf-8
//group id+artifact id+version:插件在倉庫中的坐標
//configuration:設置插件的參數值
<build>
<finalName>helloworld</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.1</version>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>