关于maven项目的pom.xml结构分析

一个基本项目的pom.xml文件,通常至少有三个部分

第一部分,项目坐标,信息描述等

    <modelVersion>4.0.0</modelVersion>
    <groupId>com.company.project</groupId>
    <artifactId>module</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>test Maven Webapp</name>
    <url>http://maven.apache.org</url>


modelVersion:pom文件的模型版本

关于group id和artifact id,为了便于多人多模块协同开发管理

group id:com.公司名.项目名

artifact id:功能模块名

packaging:项目打包的后缀,war是web项目发布用的,默认为jar

version:     artifact模块的版本

name和url:相当于项目描述,可删除

group id + artifact id +version :项目在仓库中的坐标

第二部分,引入jar包

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<spring.version>4.1.4.RELEASE</spring.version>
		<hibernate.version>4.3.8.Final</hibernate.version>
		<jackson.version>2.5.0</jackson.version>
	</properties>
	<dependencies>
		<!-- 这是创建项目时自动生成的,将junit-3.8.1.jar引入到项目中 -->
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</dependency>

		<!-- spring -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>${spring.version}</version>
		</dependency>
	</dependencies>

properties下声明相应的版本信息,然后在dependency下引用的时候用${spring-version}就可以引入该版本jar包了

dependency:引入资源jar包到本地仓库,要引入更多资源就在<dependencies>中继续增加<dependency>

group id+artifact id+version:资源jar包在仓库中的坐标

scope:作用范围,test指该jar包仅在maven测试时使用,发布时会忽略这个包。需要发布的jar包可以忽略这一配置

刚开始本地仓库是空的,maven会从远程仓库自动下载这个jar到本地仓库,下载完后,就可以在项目中使用这个jar了

如果将<dependency>的内容删除,junit-3.8.1.jar也会自动消失,无法使用

第三部分,构建项目

    <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>


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:设置插件的参数值

转自与:https://blog.csdn.net/wangb_java/article/details/54170143#commentBox

猜你喜欢

转载自blog.csdn.net/whz199511/article/details/86003105