详解:Maven项目和其pom.xml文件

您所在的位置:网站首页 maven项目中pomxml报错 详解:Maven项目和其pom.xml文件

详解:Maven项目和其pom.xml文件

#详解:Maven项目和其pom.xml文件| 来源: 网络整理| 查看: 265

文章目录 前言1 详解Maven1.1 Maven是什么?1.2 Maven怎么用?用来做什么的?1.3 Maven和Gradle,Ant这些工具有什么区别呢?或者说Maven有何优劣?1.3.1 Ant1.3.2 Maven1.3.3 Gradle 1.4 如何搭建一个Maven项目? 2 详解pom.xml2.1 pom.xml 和Maven的关系?pom.xml是什么?2.2 pom.xml的基本结构2.3 pom.xml的各个术语1 groupId2 artifactId3 version4 packaging5 ... 小结 参考文献

前言

本文旨在对Maven项目及其pom.xml文件进行详解。

这段时间一直接触Maven项目,对Maven以及其中的pom.xml文件中很多概念都不太明白,故在此研究、记录如下。

1 详解Maven

要想了解Maven项目,需要搞懂以下几个问题

1.1 Maven是什么?

首先,Maven的读音: [ˈmeɪvn]) 在这里插入图片描述 对于Maven的定义,如 [2] http://maven.apache.org/what-is-maven.html 所言:

Maven, a Yiddish word meaning accumulator of knowledge, was originally started as an attempt to simplify the build processes in the Jakarta Turbine project. There were several projects each with their own Ant build files that were all slightly different and JARs were checked into CVS. We wanted a standard way to build the projects, a clear definition of what the project consisted of, an easy way to publish project information and a way to share JARs across several projects. The result is a tool that can now be used for building and managing any Java-based project. We hope that we have created something that will make the day-to-day work of Java developers easier and generally help with the comprehension of any Java-based project.

如上,大意是: Maven是一个Yiddish(意第绪语,属于日耳曼语族,大部分使用者是犹太人)文字,表示知识收集器。Maven最开始是用来试着简化雅加达涡轮机项目(Jakarta Turbine project)的构建过程(build process,参考 [3] )。当时,有几个项目,每一个都有各自的Ant 构建文件(这些文件相互之间都有些轻微的差别),并且JARs(即Jar包)都被检入到了CVS版本控制系统中(CVS概念,参考 [4] )。我们想要一个标准的(统一的)方式来构件这些项目,一个项目如何构成的清楚明确的定义,一个发型项目信息的简单方法以及一个跨项目间共享JAR包的方法。

所以呢,Maven就在这种需求下应运而生了。Maven现在可以被用来构建、管理任何基于Java的项目。我们希望Maven能让Java开发者日复一日的工作更加轻松,且能够广泛帮助对任何基于Java的项目的理解。

1.2 Maven怎么用?用来做什么的?

从 1.1 中可以看到,Maven主要是用来构件、管理java项目的。

具体的用法呢,参考 [6] http://maven.apache.org/guides/getting-started/maven-in-five-minutes.html 。 里面有个大概的5分钟Maven运行教程。 这里简单写一下吧(具体还是以 [6] 为准):

mvn --version #显示maven版本 mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.4 -DinteractiveMode=false # 上面这个命令是一个maven goal(即maven目标),其中,这个 generate 目标表示产生一个java项目,包括pom.xml文件 mvn package # 构建这个maven项目,这里是打成jar包。这不是一个maven goal,而是一个 maven phase(时期,阶段)。一个phase是build lifecycle 即构建生命周期(可以理解成一个阶段序列,包含了很多个phase)的一个阶段(步骤)。 java -cp target/my-app-1.0-SNAPSHOT.jar com.mycompany.app.App # 测试这个打好的Jar包 1.3 Maven和Gradle,Ant这些工具有什么区别呢?或者说Maven有何优劣?

这里主要参考文献 [7] 和 [8] 来作答,[7]和[8]都挺专业的,也很详细。

Ant,Maven,Gradle,这三个Java构建自动化工具,可以说是dominated the JVM ecosystem 即 占据了JVM生态系统。

1.3.1 Ant

最开始的时候,make是唯一的自动构建工具。大概1976年的时候,make被用在构建java项目。但是呢,make中有很多C的传统风格并不适用于Java项目,所以的Ant可以说是应运而生了。

Apache Ant (“Another Neat Tool”),即其全称为:Another Neat Tool,其定义&介绍如下:

Apache Ant (“Another Neat Tool”) is a Java library used for automating build processes for Java applications. Additionally, Ant can be used for building non-Java applications. It was initially part of Apache Tomcat codebase and was released as a standalone project in 2000. In many aspects, Ant is very similar to Make, and it’s simple enough so anyone can start using it without any particular prerequisites. Ant build files are written in XML, and by convention, they’re called build.xml. Different phases of a build process are called “targets”.

大意是: Ant,即Another Neat Tool的缩写,是一个用来自动构建Java应用的Java库。此外,Ant还能用于构建非Java程序。Ant最早是Apache Tomcat 代码库中的一部分,在2000年的时候被作为一个独立的项目发布。

在许多方面,Ant和Make非常相似,并且使用非常简单,不需要任何特别的先决条件。Ant 构建文件是用XML写的,且按照惯例,他们叫build.xml。 Ant 中的不同phase(阶段)叫做targets(目标)。

Ant的优缺点介绍如下:

The main benefit of Ant is its flexibility. Ant doesn’t impose any coding conventions or project structures. Consequently, this means that Ant requires developers to write all the commands by themselves, which sometimes leads to huge XML build files which are hard to maintain. Since there are no conventions, just knowing Ant does not mean we’ll quickly understand any Ant build file. It’ll likely take some time to get accustomed with an unfamiliar Ant file, which is a disadvantage compared the other, newer tools. At first, Ant had no built-in support for dependency management. However, as dependency management became a must in the later years, Apache Ivy was developed as a sub-project of the Apache Ant project. It’s integrated with Apache Ant, and it follows the same design principles. However, the initial Ant limitations due to not having built-in support for dependency management and frustrations when working with unmanagable XML build files led to the creation of Maven.

大意是: Ant的优点:灵活,即没有强加任何代码习惯或者项目结构。 缺点:正因如此,阅读非常困难;而且因为对依赖管理(dependency management)没有built-in支持,且其XML文件难以管理,导致Maven的诞生。

1.3.2 Maven

Maven的介绍如下:

Apache Maven is a dependency management and a build automation tool, primarily used for Java applications. Maven continues to use XML files just like Ant but in a much more manageable way. The name of the game here is convention over configuration. While Ant gives the flexibility and requires everything to be written from scratch, Maven relies on conventions and provides predefined commands (goals). Simply put, Maven allows us to focus on what our build should do, and gives us the framework to do it. Another positive aspect of Maven was that it provided built-in support for dependency management. Maven’s configuration file, containing build and dependency management instructions, is by convention called pom.xml. Additionally, Maven also prescribes strict project structure, while Ant provides flexibility there as well.

大意是: Maven是一个依赖管理和构建自动化工具。Maven沿用了和Ant相似的XML文件,但是是更可管理的方式。Maven依赖习惯(conventions)并且提供预定义的命令(goals)。 简单来说,Maven让我们专注于我们的构建应该做什么,并且给我们一个框架去做。其他的好处就是:Maven提供了built-in(内嵌的)依赖管理(其实Ant也可以,带上lvy就行)。

Maven的优缺点如下:

Maven became very popular since build files were now standardized and it took significantly less time to maintain build files, comparing to Ant. However, though more standardized than Ant files, Maven configuration files still tend to get big and cumbersome. Maven’s strict conventions come with a price of being a lot less flexible than Ant. Goal customization is very hard, so writing custom build scripts is a lot harder to do, compared with Ant. Although Maven has made some serious improvements regarding making application’s build processes easier and more standardized, it still comes with a price due to being a lot less flexible than Ant. This lead to the creation of Gradle which combines best of both worlds – Ant’s flexibility and Maven’s features.

大意是: Maven的配置文件比Ant文件更加标准化,且只需更少的时间来维护构建文件。但是呢,尽管更加标准化,但是Maven的配置文件还是往往会变得庞大而且笨重。 而且,Maven的严格规范使其没有Ant这么灵活。Goal(目标)的定值非常困难。 这种不灵活性就导致了Gradle的产生。

1.3.3 Gradle

Gradle的介绍如下:

Gradle is a dependency management and a build automation tool which was built upon the concepts of Ant and Maven. One of the first things we can note about Gradle is that it’s not using XML files, unlike Ant or Maven. Over time, developers became more and more interested in having and working with a domain specific language – which, simply put, would allow them to solve problems in a specific domain using a language tailored for that particular domain. This was adopted by Gradle, which is using a DSL based on Groovy. This led to smaller configuration files with less clutter since the language was specifically designed to solve specific domain problems. Gradle’s configuration file is by convention called build.gradle.

大意是: Gradle是一个依赖管理和构建自动化工具,其可以说是建立咋Ant和Maven的概念之上的集大成者。 Gradle不再适用XML文件作为配置文件,而是build.gradle. 随着时间变迁,开发者对使用领域专用语言(Domain Specific Language,简称 DSL ,可参考[9])越来越感兴趣,因为DSL有助于解决特定领域的语言。 这个想法被Gradle接受,Gradle使用了基于Groovy的DSL语言,这样就使得配置文件规模更小,且少了很多杂乱的东西(clutter)。

Gradle和Maven,Ant的对比:

At its core, Gradle intentionally provides very little functionality. Plugins add all useful features. In our example, we were using java plugin which allows us to compile Java code and other valuable features. Gradle gave its build steps name “tasks”, as opposed to Ant’s “targets” or Maven’s “phases”. With Maven, we used Apache Maven Dependency Plugin, and it’s specific goal to copy dependencies to a specified directory. With Gradle, we can do the same by using tasks:gradle copyDependencies

大意是: 从本质上看,Gradle有意提供非常少的功能。但是插件(plugins)可以添加所有有用的特性(features)。比如,使用Java插件,就可以编译Java代码以及其他一系列给力的特性。 Gradle中的构建步骤叫做tasks,和Ant的targets,Maven的phases相对应。

1.4 如何搭建一个Maven项目?

参考1.2小节,这里不多讲咯。时间宝贵,珍贵,金贵丫。

2 详解pom.xml

以下主要参考:[10] POM Reference http://maven.apache.org/pom.html#What_is_the_POM 。

2.1 pom.xml 和Maven的关系?pom.xml是什么?

pom.xml的介绍如下:

POM stands for “Project Object Model”. It is an XML representation of a Maven project held in a file named pom.xml. When in the presence of Maven folks, speaking of a project is speaking in the philosophical sense, beyond a mere collection of files containing code. A project contains configuration files, as well as the developers involved and the roles they play, the defect tracking system, the organization and licenses, the URL of where the project lives, the project’s dependencies, and all of the other little pieces that come into play to give code life. It is a one-stop-shop for all things concerning the project. In fact, in the Maven world, a project need not contain any code at all, merely a pom.xml.

大意是:POM代表Project Object Model,即项目对象模型。pom是一个Maven项目的XML表示,被放在pom.xml文件中。pom非常给力,提供一站式支持(一步到位),即包括了:配置文件,缺陷跟踪系统(defect tracking system),组织和许可证(licenses),项目所在的URL地址,项目依赖,以及其他所有的和代码生命周期相关的方面。 实际上呢,要做到这些,在Maven世界里,只需要一个pom.xml文件。实在是神奇!

2.2 pom.xml的基本结构 4.0.0 ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... 2.3 pom.xml的各个术语 1 groupId

群组id,这在一个组织或者项目中一般是独一无二的。groupid,如:org.codehaus.mojo,在打包后会出现在$M2_REPO/org/codehaus/mojo这个对应的文件夹下面。

例:org.codehaus.mojo

2 artifactId

工件id,可以理解成是项目的id,比如my-project,在打包后,会出现对应的文件夹$M2_REPO/org/codehaus/mojo/my-project。

例:my-project

3 version

版本,可以理解成项目的版本号。如:1.0,在打包后,会出现对应文件夹$M2_REPO/org/codehaus/mojo/my-project/1.0。

例:1.0

4 packaging

打包的格式,如:jar或者war,默认是jar。

例:war

5 … 小结

太多了,实在解释不完。大概看了初期这么些知识,感觉该懂的也大概差不多了,所以就先到此为止吧。

更多pom的术语解释参考: [10] POM Reference http://maven.apache.org/pom.html#What_is_the_POM [14] maven http://maven.apache.org/ref/3.6.0/maven-model/maven.html

有机会有时间的话,会在以后继续补充。

参考文献

[1] pom.xml详解 https://www.cnblogs.com/wkrbky/p/6353285.html 这篇文章和 http://maven.apache.org/pom.html#Introduction 相对应,类似英文转中文的翻译版。 还是很有参考价值的。

[2] What is maven? http://maven.apache.org/what-is-maven.html

[3] 在做java开发时,build和compile有什么联系和区别?谢谢。 https://zhidao.baidu.com/question/1817957803472211668.html

java中build的意思,如下: 在这里插入图片描述

[4] CVS–Concurrent Versions System http://www.thathost.com/wincvs-howto/cvsdoc/cvs_toc.html#TOC2 这里给出了CVS,全称:Concurrent Versions System 的详细介绍。

如下,大意是: CVS是一个版本控制系统。通过CVS,你可以记录你的源码文件的变更历史。比如,通过CVS,你能很轻松的回复旧版本,从而找到bug。还有很多功能,在此不赘述。

CVS is a version control system. Using it, you can record the history of your source files. For example, bugs sometimes creep in when software is modified, and you might not detect the bug until a long time after you make the modification. With CVS, you can easily retrieve old versions to see exactly which change caused the bug. This can sometimes be a big help.

[5] Welcome to Apache Maven http://maven.apache.org/index.html Maven首页。

[6] Maven in 5 Minutes http://maven.apache.org/guides/getting-started/maven-in-five-minutes.html

[7] Ant vs Maven vs Gradle https://www.baeldung.com/ant-maven-gradle

[8] Java Build Tools: Ant vs Maven vs Gradle https://technologyconversations.com/2014/06/18/build-tools/

[9] DSL(domain specific language ) https://blog.csdn.net/HQ354974212/article/details/72240447 稍微介绍了一下DSL。

[10] POM Reference http://maven.apache.org/pom.html#What_is_the_POM

[11] xmlns https://baike.baidu.com/item/xmlns/5866334?fr=aladdin 解释xmlns的意思:

xmlns(XML Namespaces的缩写)是一个属性,是XML(标准通用标记语言的子集)命名空间。作用是赋予命名空间一个唯一的名称。

[12] xml中的xmlns,xmlns:xsi,xsi:schemaLocation有什么作用,如果没有会怎么样呢 https://www.cnblogs.com/ihanliu/p/4718795.html 如下:

我来给你解释一下吧,首先这个文件是一个xml文件,那么他里面的所有内容都符合xml语法规范,开头 的这最外层同样也是一个xml文件的标签,后面那一长串也就是所谓的属性,其中 xmlns表示命名空间,xmlns=“http://maven.apache.org/POM/4.0.0” 这表示默认命名空间,而下面 xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance” 这个命名空间里面的元素或者属性就必须 要以xsi:这种方式来写,比如schemaLocation就是他的一个属性,所以写成xsi:schemaLocation,而默认命名空间不带类似 xsi这种,其实xml标签名称有个专业叫法叫做QName,而如果没有前面的xsi:这种一般叫做NCName。所以你看mvn里面 的这种就是默认命名空间下面的元素,最后那一行就表示把定义这个命名空间的schema文件给引用进来,好让 eclipse这类型工具能够解析和验证你的xml文件是否符合语法规范。等同 于。

[13] maven snapshot和release版本的区别 https://zhidao.baidu.com/question/561174953188808884.html 在这里插入图片描述

[14] maven http://maven.apache.org/ref/3.6.0/maven-model/maven.html



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3