[Java代码] 使用Maven管理Java项目

一、Maven入门 1.下载maven

Maven是基于项目对象模型(Project Object Model),可以通过一小段描述信息来管理项目的构建、报告和文档的项目管理工具,提供了一个仓库的概念,统一管理项目所依赖的第三方jar包,最大可能避免了由于环境变量的不同在不同电脑之间无法运行的问题,Struts2、Hibernate都是采用maven部署的项目。它是Apache软件基金会的一个开源项目,下载地址是:Apache Maven 注意:Maven3.3要求jdk版本为1.7及以上。将压缩包解压到任意目录:可以看到如下的目录结构:

2.配置Maven环境

新建环境变量M2_HOME,其值为maven的解压目录,并在path环境变量中引用M2_HOME下的bin目录:

  1. shellM2_HOME:D:\Program Files (x86)\apache-maven-3.3.3
  2. http://www.nvzi91.cn/niaodaoyan/29938.html
  3. Path:...;%M2_HOME%\bin
复制代码

接下来在命令行输入mvn -v命令可以查看版本:

3.使用maven创建helloworld项目

首先创建如下的目录结构

  1. src
  2. |----main
  3. |----java
  4. |----package
  5. |----test
  6. |----java
  7. |----package
  8. |----resources
  9. pom.xml
  10. http://www.nvzi91.cn/yindaoyan/29939.html
复制代码

在main/java目录下创建我们的java源代码:

  1. javapackage org.gpf.maventest01.model;
  2. public class HelloWorld {
  3. public String sayHelloWorld(){
  4. return "Hello World!";
  5. }http://www.nvzi91.cn/yindaoyan/29940.html
  6. }
复制代码

在test/java目录下创建我们的测试代码(使用了Junit4测试框架):

  1. javapackage org.gpf.maventest01.model;
  2. http://www.nvzi91.cn/luanchaonanzhong/29941.html
  3. import org.junit.*;
  4. import static org.junit.Assert.*;
  5. public class HelloWorldTest {
  6. @Test
  7. public void testSayHelloWorld(){
  8. assertEquals("Hello World!",new HelloWorld().sayHelloWorld());
  9. }
  10. }
复制代码

接下来编写pom.xml(可以参见Struts2,如图所示)

将其拷贝到项目的根目录,并修改为如下:

  1. xml<?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  3. <modelVersion>4.0.0</modelVersion> <!-- maven版本,此值固定 -->
  4. <!-- 项目 -->
  5. <groupId>org.gpf.maventest01</groupId><!--项目包名-->
  6. <artifactId>maventest01</artifactId> <!-- 模块名,建议使用项目名 -->
  7. <version>0.0.1SNAPSHOT</version> <!--版本,此处为快照版本-->
  8. http://www.nvzi91.cn/zigongjiliu/29942.html
  9. <!-- 依赖 -->
  10. <dependencies>
  11. <dependency>
  12. <groupId>junit</groupId>
  13. <artifactId>junit</artifactId>
  14. <version>4.10</version>
  15. </dependency>
  16. </dependencies>
  17. </project>
复制代码

接下来在命令行进入我们的项目输入mvn compile命令(首次运行会下载第三方插件和maven依赖的jar,请耐心等待),对该项目进行编译。

最后使用mvn test命令运行我们的测试用例:

此时发现项目的根目录下多了一个target目录,其中class目录就是生成的类文件,reports目录就是一系列测试报告。

运行mvn package命令可以打包我们的项目,会生成maventest-0.0.1SNAPSHOT.jar。

二、Maven核心知识 1.常用构建命令

mvn -v 查看maven版本
compile 编译
test 测试http://www.kmrlyy.com/fujianyan/33454.html
package 打包
clean 删除target(字节码和测试报告)
install 安葬jar包到本地仓库

如果一个项目需要利用到其他项目的jar包,一种方式是拷贝jar包并拷贝到classpath中,maven提供了一个更简单的策略,直接在本地仓库中查找即可。例如我们一个新的项目maven02项目需要使用到之前的maven01项目的HelloWorld类中的sayHelloWorld方法,使用maven可以这样:

在maven01项目中运行mvn install命令安装jar包到本地仓库。 创建maven02项目,在其中导入maven01项目的类。

src/main

  1. javapackage org.gpf.maventest02.util;
  2. import org.gpf.maventest01.model.HelloWorld;
  3. http://www.kmrlyy.com/fujianyan/33455.html
  4. public class Speak {
  5. public String sayHi(){
  6. return new HelloWorld().sayHelloWorld();
  7. }
  8. }
复制代码

src/test

  1. javapackage org.gpf.maventest02.util;
  2. import org.junit.*;
  3. import static org.junit.Assert.*;
  4. public class SpeakTest {
  5. http://www.kmrlyy.com/gongjingmilan/33456.html
  6. @Test
  7. public void testSayHi(){
  8. assertEquals("Hello World!",new Speak().sayHi());
  9. }
  10. }
复制代码

3.在pom.xml添加依赖。

  1. xml<?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  3. <modelVersion>4.0.0</modelVersion>
  4. <groupId>org.gpf.maventest02</groupId>
  5. <artifactId>maventest02</artifactId>
  6. <version>0.0.1SNAPSHOT</version>
  7. http://www.kmrlyy.com/penqiangyan/33457.html
  8. <dependencies>
  9. <dependency>
  10. <groupId>junit</groupId>
  11. <artifactId>junit</artifactId>
  12. <version>4.10</version>
  13. </dependency>
  14. <!-- 添加maven01的jar的依赖,可以从maven01项目的pom.xml中拷贝 -->
  15. <dependency>
  16. <groupId>org.gpf.maventest01</groupId>
  17. <artifactId>maventest01</artifactId>
  18. <version>0.0.1SNAPSHOT</version>
  19. </dependency>
  20. </dependencies>
  21. </project>
  22. http://www.kmrlyy.com/niaodaoyan/33458.html
复制代码
使用mvn compile命令编译maven02项目。 2.使用archetype插件自动创建符合maven的目录骨架 方式一:使用mvn archetype:generate根据提示一步步操作; 方式二:使用mvn archetype:generate -D命令一次性生成全部。 3.maven中的坐标和仓库

maven中任何一个依赖、插件都可以被称为构件。所有构件通过坐标作为其唯一标识。

仓库可以管理项目的依赖。仓库可以分为本地仓库和远程仓库,如果本地仓库中找不到就会在远程仓库中查找并下载,如果远程仓库找不到就会报错。maven远程仓库的地址可以在maven安装目录的lib目录的maven-model-builder-3.3.3.jar中找到,如图所示:

将pom.xml打开。

镜像仓库

由于maven的中央仓库位于国外,速度慢,也有可能其他原因无法访问,我们可以使用国内的镜像仓库。配置镜像仓库需要修改conf/settings.xml,打开该文件修改mirror标签如下:

  1. xml <mirrors>
  2. <!-- mirror
  3. | Specifies a repository mirror site to use instead of a given repository. The repository that
  4. | this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used
  5. | for inheritance and direct lookup purposes, and must be unique across the set of mirrors.
  6. |http://m.nvzi91.cn/penqiangyan/29351.html
  7. <mirror>
  8. <id>mirrorId</id>
  9. <mirrorOf>repositoryId</mirrorOf>
  10. <name>Human Readable Name for this Mirror.</name>
  11. <url>http://my.repository.com/repo/path</url>
  12. </mirror>
  13. -->
  14. <mirror>
  15. <id>maven.net.cn</id>
  16. <mirrorOf>central</mirrorOf>
  17. <name>central mirror in china</name>
  18. <url>http://maven.net.cn/content/groups/public</url>
  19. </mirror>
  20. </mirrors>
复制代码

maven仓库默认是放在用户目录的.m2隐藏目录下的,Windows系统就是C:\Users\用户名\.m2,我们需要将仓库迁移到其他目录,修改conf/settings.xml,如图所示:

将settings.xml复制到E:/tmp/mavenproject/repo,这样更新maven就不需要重新配置settings.xml了。

4.在MyEclipse中使用maven

MyEclipse默认安装了maven插件,极大简化了操作。在使用MyEclipse创建项目之前需要进行如下的配置:

配置maven安装目录(自带的可能有错)

给jre添加一个运行时参数(错误-Dmaven.multiModuleProjectDirectory system propery is not set. Check $M2_HOME environment variable and mvn script match.的解决方案)

使用MyEclipse创建Maven项目的步骤(注意选择合适的Junit版本,默认是3.8.1,我们需要该变成4.10)如下:



接下来在MyEclipse中运行maven项目,右键项目名---Run As会出现如下菜单:

在弹出的的对话框的Goals中输入maven命令(compile、test、package等)即可。

5.maven的生命周期和插件

maven有3个独立的阶段。

clean(清理) pre-clean clean post-clean default(构建、最核心) compile test package install site(生成项目站点) pre-site site(生成项目的站点文档) post-site site-deploy(发布站点到服务器)

Maven是基于插件的,我们可以在http://maven.apache.org/plugins/index.html找到合适的插件,例如源代码打包插件source插件。

编辑pom.xml,配置source插件(可以参见source插件文档)

  1. xml<project>
  2. ...
  3. <build>
  4. <plugins>
  5. <plugin>http://m.nvzi91.cn/zigongai/29352.html
  6. <!-- 配置插件 -->
  7. <groupId>org.apache.maven.plugins</groupId>
  8. <artifactId>maven-source-plugin</artifactId>
  9. <version>2.4</version>
  10. <!-- 绑定插件到package阶段,如果运行package命令将会打包源代码 -->
  11. <executions>
  12. <execution>
  13. <phase>package</phase>
  14. <goals>
  15. <goal>jar-no-fork</goal>
  16. </goals>
  17. </execution>
  18. </executions>
  19. </plugin>
  20. </plugins>
  21. </build>
  22. ...http://m.nvzi91.cn/jiankang/29353.html
  23. <project>
复制代码

使用clean package命令,运行结果:

6.pom.xml文件解析
  1. xml<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  3. <!-- 指定当前pom的版本 -->
  4. <modelVersion>4.0.0</modelVersion>
  5. <!-- 主项目坐标 -->
  6. <groupId>org.gpf.maventest</groupId> <!-- 域名倒置+项目名,也就是包名 -->
  7. <artifactId>maventest-demo</artifactId><!-- 项目名+模块名 -->
  8. <version>0.0.1-SNAPSHOT</version><!-- 版本号(3个数字) 大版本.分支版本.小版本snapshot(快照)|alpha(内测)|beta(公测)|Release(稳定)|GA(正式)-->
  9. <packaging>jar</packaging><!-- 打包方式,默认是jar,还可以是war、zip、pom -->
  10. <!-- 项目信息 -->
  11. <name>maventest-demo</name><!-- 项目描述名 -->
  12. <url>http://maven.apache.org</url><!-- 项目地址 -->
  13. <description></description><!-- 项目描述 -->
  14. <developers></developers><!-- 开发人员列表 -->
  15. <licenses></licenses><!-- 许可证信息 -->
  16. <organization></organization><!-- 组织信息 -->
  17. <properties>
  18. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  19. </properties>
  20. <!-- 依赖列表 -->
  21. <dependencies>
  22. <dependency>
  23. <groupId>junit</groupId>
  24. <artifactId>junit</artifactId>
  25. <version>4.10</version>
  26. <type></type>
  27. <scope>test</scope><!-- 依赖范围,只能在test -->
  28. <optional></optional><!-- 依赖是否可选,默认是false -->
  29. <exclusions><!-- 排除依赖传递传递列表 -->
  30. <exclusion></exclusion>
  31. </exclusions>
  32. </dependency>
  33. </dependencies>
  34. <!-- 依赖管理,定义在父模块中供子模块继承使用 -->
  35. <dependencyManagement>
  36. <dependencies>
  37. <dependency></dependency>
  38. </dependencies>
  39. </dependencyManagement>
  40. <!-- 对构建提供支持 -->
  41. <build>
  42. <!-- 插件列表 -->
  43. <plugins>
  44. <plugin>
  45. <groupId>org.apache.maven.plugins</groupId>
  46. <artifactId>maven-source-plugin</artifactId>
  47. <version>2.4</version>
  48. <executions>
  49. <execution>
  50. <phase>package</phase>
  51. <goals>
  52. <goal>jar-no-fork</goal>
  53. </goals>
  54. </execution>
  55. </executions>
  56. </plugin>
  57. </plugins>
  58. </build>
  59. www.nvzi91.cn
  60. <!-- 指定父模块 -->
  61. <parent></parent>
  62. <!-- 多模块编译 -->
  63. <modules>
  64. <module></module>
  65. </modules>
  66. </project>
复制代码
7.依赖范围

maven提供了3种classpath(编译、测试、运行),例如上面的pom.xml的Junit框架的scope属性为test。http://maven.apache.org/guides/introduction/introduction-to-dependency...上提供了6种依赖范围:

compile:缺省值,编译、测试都有效 provided:编译,测试都有效,但是在运行时并不会加入。例如Servlet API,因为Web容器本身有,如果加入就会出现冲突。 runtime:测试、运行有效。 test:仅测试时有效。 system:与本机相关,可移植性差。 import:导入的依赖范围,仅适用在dependencyManager中,表示从其他pom导入dependency配置。 8.依赖传递

创建3个maven项目,grandfather、father、son,其中father依赖grandfather,son又依赖father,他们之间构成依赖。

例如在Father项目的pom.xml中添加对GradFather的依赖,只需要在dependency标签中指定GrandFather的坐标即可,就像下面这样:

  1. xml<dependencies>
  2. <dependency>
  3. <groupId>junit</groupId>
  4. <artifactId>junit</artifactId>
  5. <version>3.8.1</version>
  6. <scope>test</scope>
  7. </dependency>
  8. <dependency>
  9. <groupId>com.maventest</groupId>
  10. <artifactId>GrandFather</artifactId>
  11. <version>0.0.1-SNAPSHOT</version>
  12. </dependency>
  13. </dependencies>
  14. www.kmrlyy.com
复制代码

类似的在Son项目中添加对Father的依赖。现在构建Son需要先构建GrandFather和Father。对GrandFather和Father进行clean install命令,再对Son项目进行compile命令即可。构建完成后3个项目的目录结构如图:

此时发现Son依赖了2个项目GrandFather和Father,如果仅仅希望Son依赖Father可以将GrandFather的坐标进行依赖排除

  1. xml<dependencies>
  2. <dependency>
  3. <groupId>com.maventest</groupId>
  4. <artifactId>Father</artifactId>
  5. <version>0.0.1-SNAPSHOT</version>
  6. <exclusions>
  7. <exclusion>
  8. <groupId>com.maventest</groupId>
  9. <artifactId>GrandFather</artifactId>
  10. </exclusion>
  11. </exclusions>
  12. </dependency>
  13. </dependencies>
  14. m.nvzi91.cn
复制代码
9.依赖冲突

依赖冲突有2条原则:

短路优先(优先解析路径短的版本),例如有以下2个依赖:A-->B-->C-->x.jar,A-->D--x.jar(优先) 路径相同,先声明先优先。 10.聚合与继承

如果我们的项目要使用到以上的GrandFather、Father、Son3个模块,就可以使用聚合。新建一个relationship项目,修改其pom.xml如下:

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  3. <modelVersion>4.0.0</modelVersion>
  4. <groupId>com.maventest</groupId>
  5. <artifactId>relationship</artifactId>
  6. <version>0.0.1-SNAPSHOT</version>
  7. <!-- 注意package的值为pom -->
  8. <packaging>pom</packaging>
  9. <name>relationship</name>
  10. <url>http://maven.apache.org</url>
  11. <properties>
  12. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  13. </properties>
  14. <dependencies>
  15. <dependency>
  16. <groupId>junit</groupId>
  17. <artifactId>junit</artifactId>
  18. <version>3.8.1</version>
  19. <scope>test</scope>
  20. </dependency>
  21. </dependencies>
  22. <!-- 导入模块 -->
  23. <modules>
  24. <module>../GrandFather</module>
  25. <module>../Father</module>
  26. <module>../Son</module>
  27. </modules>
  28. </project>```
  29. 在relationship项目运行`clean install`命令将会依次生成3个jar并加入本地仓库。
  30. 在以上的项目中项目中每次都要配置Junit的依赖,我们可以向java编程那样将公共的模块抽取出来,这种方式叫做继承。新建一个maven项目,命名为common,其`pom.xml`配置如下:
  31. ```xml
  32. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  33. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  34. <modelVersion>4.0.0</modelVersion>
  35. <groupId>com.maventest.common</groupId>
  36. <artifactId>common</artifactId>
  37. <version>0.0.1-SNAPSHOT</version>
  38. <packaging>pom</packaging>
  39. <name>common</name>
  40. <url>http://maven.apache.org</url>
  41. <properties>
  42. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  43. <junit-version>3.8.1</junit-version>
  44. </properties>
  45. <dependencyManagement>
  46. <dependencies>
  47. <dependency>
  48. <groupId>junit</groupId>
  49. <artifactId>junit</artifactId>
  50. <version>${junit-version}</version>
  51. <scope>test</scope>
  52. </dependency>
  53. </dependencies>
  54. </dependencyManagement>
  55. </project>
复制代码

这是项目可能有红色的小叉,由于父模块不需要测试,我们可以删除src/test重新更新maven项目(右键项目-->Maven ForMyEclipse-->Update Project Configuration),发现红色的叉消失。在子模块中我们可以这样使用:

  1. xml<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  3. <modelVersion>4.0.0</modelVersion>
  4. <groupId>com.maventest</groupId>
  5. <artifactId>GrandFather</artifactId>
  6. <version>0.0.2-SNAPSHOT</version>
  7. <packaging>jar</packaging>
  8. <name>GrandFather</name>
  9. <url>http://maven.apache.org</url>
  10. <properties>
  11. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  12. </properties>
  13. <!-- 引入父模块坐标 -->
  14. <parent>
  15. <groupId>com.maventest.common</groupId>
  16. <artifactId>common</artifactId>
  17. <version>0.0.1-SNAPSHOT</version>
  18. </parent>
  19. <dependencies>
  20. <dependency>
  21. <groupId>junit</groupId>
  22. <artifactId>junit</artifactId>
  23. </dependency>
  24. </dependencies>
  25. </project>
复制代码
三、使用Maven建立Web项目

新建一个maven项目,ArcheType选择webapp,如图所示:


在pom.xml中配置tomcat插件(可参见Tomcat官方文档)。

  1. xml<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  3. <modelVersion>4.0.0</modelVersion>
  4. <groupId>com.maventest.webdemo</groupId>
  5. <artifactId>webdemo</artifactId>
  6. <packaging>war</packaging>
  7. <version>0.0.1-SNAPSHOT</version>
  8. <name>webdemo Maven Webapp</name>
  9. <url>http://maven.apache.org</url>
  10. <dependencies>
  11. <dependency>
  12. <groupId>junit</groupId>
  13. <artifactId>junit</artifactId>
  14. <version>4.10</version>
  15. <scope>test</scope>
  16. </dependency>
  17. </dependencies>
  18. <build>
  19. <finalName>webdemo</finalName>
  20. <plugins>
  21. <plugin>
  22. <groupId>org.apache.tomcat.maven</groupId>
  23. <artifactId>tomcat7-maven-plugin</artifactId>
  24. <version>2.2</version>
  25. </plugin>
  26. </plugins>
  27. </build>
  28. </project>
复制代码

clean-->package-->部署项目到Tomcat即可。

四、常见问题和解决方案 1.jdk版本不符合的问题

Eclipse中的jdk版本显示为1.5,但是编译使用的版本是1.7,可以参考以下的conf/settings.xml的配置:

  1. xml<profile>
  2. <id>jdk-1.7</id>
  3. <activation>
  4. <activeByDefault>true</activeByDefault>
  5. <jdk>1.7</jdk>
  6. </activation>
  7. <properties>
  8. <maven.compiler.source>1.7</maven.compiler.source>
  9. <maven.compiler.target>1.7</maven.compiler.target>
  10. <maven.compiler.compilerVersion>1.7</maven.compiler.compilerVersion>
  11. </properties>
  12. </profile>
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值