maven(四):一个基本maven项目的pom.xml配置

继续之前创建的test项目,一个基本项目的pom.xml文件,通常至少有三个部分

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

  1. <modelVersion>4.0.0 </modelVersion>
  2. <groupId>com.company.project </groupId>
  3. <artifactId>module </artifactId>
  4. <packaging>war </packaging>
  5. <version>0.0.1-SNAPSHOT </version>
  6. <name>test Maven Webapp </name>
  7. <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包

  1. <dependencies>
  2. <dependency>
  3. <groupId>junit </groupId>
  4. <artifactId>junit </artifactId>
  5. <version>3.8.1 </version>
  6. <scope>test </scope>
  7. </dependency>
  8. </dependencies>

这是创建项目时自动生成的,将junit-3.8.1.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也会自动消失,无法使用

 

第三部分,构建项目

  1. <build>
  2. <finalName>helloworld </finalName>
  3. <plugins>
  4. <plugin>
  5. <groupId>org.apache.maven.plugins </groupId>
  6. <artifactId>maven-compiler-plugin </artifactId>
  7. <version>3.5.1 </version>
  8. <configuration>
  9. <source>1.7 </source>
  10. <target>1.7 </target>
  11. </configuration>
  12. </plugin>
  13. <plugin>
  14. <groupId>org.apache.maven.plugins </groupId>
  15. <artifactId>maven-resources-plugin </artifactId>
  16. <version>3.0.1 </version>
  17. <configuration>
  18. <encoding>UTF-8 </encoding>
  19. </configuration>
  20. </plugin>
  21. </plugins>
  22. </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:设置插件的参数

猜你喜欢

转载自blog.csdn.net/zxs9999/article/details/80966829