Maven:pom文件

Maven教程:https://www.yiibai.com/maven/

本文为maven中基本单位pom文件的学习,目前主要解决问题:pom更新,生成jar包到指定文件夹

2018年5月7日

POM代表项目对象模型,是MAVEN中工作的基本单位,是一个XML文件,始终保存在项目基本目录pom.xml文件中。目的是用来包含各种配置信息、目标和插件。在这行任务或目标时,maven会使用当前目录中的POM,读取所需要的配置信息,然后执行目标。

每个项目只有一个POM文件。

  • 所有POM文件项目元素必须有三个必填字段:groupIdartifactIdversion,这些属性在项目仓库是唯一标识的。
  • 在库中的项目符号:groupId:artifactId:version
  • pom.xml的根元素是project

pom更新

  1. 如果使用Eclipse,可以右键该项目选择refresh
  2. 也可以从终端进入该项目文件目录,输入命令mvn eclipse:eclipse,Maven将从指定地址下载资料保存到本地仓库。

将引用的jar包放到lib文件夹中

Maven执行项目时需要很多jar包,通常做法是把jar包统一放在lib目录中

首先需要在pom文件中build节点内添加一个plugin,内容如下:

 <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <executions>
                <execution>
                    <id>copy-dependencies</id>
                    <phase>prepare-package</phase>
                    <goals>
                        <goal>copy-dependencies</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${project.build.directory}/lib</outputDirectory>
                        <overWriteReleases>false</overWriteReleases>
                        <overWriteSnapshots>false</overWriteSnapshots>
                        <overWriteIfNewer>true</overWriteIfNewer>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <classpathPrefix>lib/</classpathPrefix>
                        <mainClass>theMainClass</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>

上面配置我们使用了maven-dependency-plugin插件,还有执行了节点copy-dependencies操作。我们指定的输出目录是${project.build.directory}/lib,这里的${project.build.directory}就是我们通常看到的target目录,也就是要把jar复制到target目录下的lib目录下。