wordpress api下载,谷歌seo快速排名软件首页,公司注册地址和经营地址不一致,企业网站 flashMaven是apache的一个开源项目。是一个用来把源代码构建成可发布的构件的工具。 Maven的功能非常强大#xff0c;可以认为是一个项目管理工具#xff0c;不仅仅是一个构建工具。 Maven本身的核心很小#xff0c;但是可以在上面扩展出很多的插件。Mven采用的是插件的思想…Maven是apache的一个开源项目。是一个用来把源代码构建成可发布的构件的工具。 Maven的功能非常强大可以认为是一个项目管理工具不仅仅是一个构建工具。 Maven本身的核心很小但是可以在上面扩展出很多的插件。Mven采用的是插件的思想通过插件的功能扩展出很多的功能。同时Maven采用约定大于配置的思想在项目中采用了很多约定规则来减少配置。不想ant这样的构建工具需要很多的配置。作为一个项目构建工具最重要的是管理项目的库和项目之间的依赖关系。 本文将以以下面的例子来作为学习maven的一个过程。 假设要构建一个名为Content-Search的项目该项目包含了三个子项目:contentSearch-dal,contentSearch-biz,contentSearch-web。项目采用Struts2、hibernate3、spring2.5的框架结合。使用mysql数据库。 contentSearch-web:Web层的代码, struts2spring2.5结合的框架。 contentSearch-biz:业务逻辑层代码。 contentSearch-dal:数据持久层代码。 三个项目之间的依赖和对外部库的依赖关系大概如下 作为从一个项目构建工具角度出发maven主要要实现的就是项目的依赖的二方、三方库管理项目之间的依赖管理。以及项目的整个生命周期的管理包括编译(compile)、测试(test)、打包(packaging)、部署(install)等。 以以上为背景先介绍下maven的基本概念 依赖管理对每个项目对外部的依赖的管理。 远程仓库maven外部引用仓库的管理。 项目LifeCycle抽象和管理项目生命周期编译、测试和打包等过程的抽象和管理。 1. Maven安装http://maven.apache.org/download.html在上面根据不同的操作系统选择不同的安装版本。 安装后的目录结构如下: bin/:maven脚本 boot/ conf/:全局的settings.xml。 如果需要自己定制可以覆盖.m2文件夹里的settings.xml文件。 lib/:有包含maven核心的jar文件 设置环境变量: M2_HOMEmaven安装路径 Path: $M2_HOME/bin 2. Maven核心概念groupId , artifactId, packaging, version:—— 以上4个是 Maven 的 坐 标(coordinates),它们唯一标识了一个项目。 groupId: 团体,公司,小组,组织,项目,或者其它团体。如contentSearch-web,contentSearch-dal,contentSearch-biz同属一个groupId。 artifactId在 groupId 下的表示一个单独项目的唯一标识符。项目名称ID 。 packaging: 标识项目的类型如jarwar等。 Version版本号。 以文章开头的例子为例contentSearch-web的坐标可以定义如下: groupId: com.companyName.contentSearch artifactIdcontentSearch-web packaging: war Version1.0。 而contentSearch-dal的坐标如下: groupId: com.companyName.contentSearch artifactIdcontentSearch-dal packaging: jar Version1.0。 在一个 Maven 仓库中,所有的东西存储在一个与 Maven 项目坐标十分匹配的目录结构中。 /groupId/artifactId/version/artifactId-version.packaging. POM:项目抽象模型 这是maven非常核心的概念。Maven通过它来管理项目。每一个项目都有一个pom.xml文件该文件定义了改项目的基本信息依赖关系等maven对项目的生命周期管理也是基于此文件。以contentSearch-dal为例该项目的pom.xml定义如下(关于pom.xml里面各项的含义本文不做过多介绍相信看了基本都能清楚。): project xmlnshttp://maven.apache.org/POM/4.0.0 xmlns:xsihttp://www.w3.org/2001/XMLSchema-instance xsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd modelVersion4.0.0/modelVersion parent groupId com.companyName.contentSearch/groupId artifactIdcontentSearch/artifactId version1.0/version /parent artifactIdcontentSearch-dal/artifactId packagingjar/packaging version1.0/version namecontentSearch-dal/name urlhttp://maven.apache.org/url dependencies dependency groupIdjunit/groupId artifactIdjunit/artifactId version3.8.1/version scopetest/scope /dependency dependency groupIdgeneralorm/groupId artifactIdhibernate/artifactId version3.0/version /dependency dependency groupIdmysql/groupId artifactIdmysql-connector-java/artifactId version5.1.6/version /dependency dependency groupIdlog4j/groupId artifactIdlog4j/artifactId version1.2.14/version /dependency dependency groupIddom4j/groupId artifactIddom4j/artifactId version1.6.1/version /dependency dependency groupIdslf4j/groupId artifactIdslf4j-api/artifactId version1.5.8/version /dependency dependency groupIdslf4j/groupId artifactIdslf4j-nop/artifactId version1.5.2/version /dependency dependency groupIdantlr/groupId artifactIdantlr/artifactId version2.7.6/version /dependency dependency groupIdjta/groupId artifactIdjta/artifactId version1.1/version /dependency dependency groupIdjavassist/groupId artifactIdjavassist/artifactId version3.9.0.GA/version /dependency dependency groupIdcommons-collections/groupId artifactIdcommons-collections/artifactId version3.1/version /dependency /dependencies /project Repository 仓库二方库三方库的概念。每当安装完成maven之后就会有一个默认的本地仓库和远程仓库。本地仓库在用户工作目录下的.m2文件夹如在linux下为/home/yblin/.m2。远程repository默认在settings.xml里面配置如果要修改该文件必须在.m2文件夹下面替换一下即可。当进行编译的时候maven会先查找本地Repository如果本地Repository没有会去取远程repository 。 3. Maven命令创建一个项目: mvn archetype:create -DgroupIdcom.xxx.simple - DartifactId simple archetype:create是一个goal-DgroupIdorg.sonatype.mavenbook.ch03代表要传到目标goal任务的参数以-D开头。 插件和目标:maven archetype:create. Archetype是插件标识create是目标标识。 一个 Maven 插件是一个单个或者多个目标的集合。Maven 插件的例子有一些简单但核心的插件,像 Jar 插件,它包含了一组创建JAR 文件的目标,Compiler插件,它包含了一组编译源代码和测试代码的目标,或者 Surefire 插件,它包含一组运行单元测试和生成测试报告的目标。而其它的,更有专门的插件包括:Hibernate3 插件,用来集成流行的持久化框架 Hibernate,JRuby 插件,它让你能够让运行 ruby 称为 Maven 构建的一部分或者用 Ruby 来编写 Maven 插件。Maven也提供了自定义插件的能力。一个定制的插件可以用 Java 编写,或者用一些其它的语言如 Ant,Groovy,beanshell 和之前提到的 Ruby。 生命周期命令:maven install等 Mvn install命令没有进行任何插件配置或者定制,所以这个例子绑定了一组标准插件的目标到默认的生命周期。当 Maven 经过以 package 为结尾的默认生命周期的时候,下面的目标按顺序被执行: resources:resources Resources 插件的 resources 目标绑定到了 resources 阶段。这个目标复 制 src/main/resources 下的所有资源和其它任何配置的资源目录,到输 出目录。 compiler:compile Compiler 插件的 compile 目标绑定到了 compile 阶段。这个目标编译 src/main/java 下的所有源代码和其他任何配置的资源目录,到输出目 录。 resources:testResources Resources 插件的 testResources 目标绑定到了 test-resources 阶段。 这个目标复制 src/test/resources 下的所有资源和其它任何的配置的测 试资源目录,到测试输出目录。 compiler:testCompile Compiler 插件的 testCompile 目标绑定到了 test-compile 阶段。这个目 标编译 src/test/java 下的测试用例和其它任何的配置的测试资源目录, 到测试输出目录。 surefire:test Surefire 插件的 test 目标绑定到了 test 阶段。这个目标运行所有的测试 并且创建那些捕捉详细测试结果的输出文件。默认情况下,如果有测试失 败,这个目标会终止。 jar:jar Jar 插件的 jar 目标绑定到了 package 阶段。这个目标把输出目录打包成 JAR 文件。 当 Maven 运行一个目标的时候,每个目标都会访问定义在项目 POM 里的信息。 其他命令: mvn install:install-file -DgeneratePomtrue -DgroupIdjep -DartifactIdjep -Dversion3.3.0 -Dpackagingjar -DfileE:/lib/LIB_COMMON/jep-3.3.0-trial.jar添加一个包到依赖库。 mvn dependency:resolve mvn dependency:tree浏览项目依赖。 mvn archetype:create 创建 Maven 项目 mvn compile 编译源代码 mvn test-compile 编译测试代码 mvn test 运行应用程序中的单元测试 mvn site 生成项目相关信息的网站 mvn clean 清除目标目录中的生成结果 mvn package 依据项目生成 jar 文件 mvn install 在本地 Repository 中安装 jar mvn eclipse:eclipse 生成 Eclipse 项目文件 4. Maven实践 以文章开头例子为例,做为实践。 1 下载依赖包并添加到本地repository.如果配置了远程依赖库里面已经有了这些依赖包那无需下载在编译时会自动下载到本地库。 下载struts2,hibernate2,spring2.5,mysql-jdbc等相关的包到本地通过命令安装到本地repository. mvn install:install-file -DgrouptIdframework -DartifactIdspring-beans -Dversion2.5.6 -Dfilespring-beans-2.5.6.jar 该命令将spring-beans-2.5.6.jar安装到本地repository. 2 创建总控项目 mvn archetype:create命令创建总控项目修改pom.xml文件如下: project xmlnshttp://maven.apache.org/POM/4.0.0 xmlns:xsihttp://www.w3.org/2001/XMLSchema-instance xsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd modelVersion4.0.0/modelVersion groupId com.companyName.contentSearch /groupId artifactIdcontentSearch/artifactId packagingpom/packaging version1.0/version nameChapter 6 Simple Parent Project/name modules modulecontentSearch-dal/module modulecontentSearch-biz/module modulecontentSearch-web/module /modules build pluginManagement plugins plugin groupIdorg.apache.maven.plugins/groupId artifactIdmaven-compiler-plugin/artifactId configuration source1.5/source target1.5/target /configuration /plugin /plugins /pluginManagement /build dependencies dependency groupIdjunit/groupId artifactIdjunit/artifactId version3.8.1/version scopetest/scope /dependency /dependencies /project 3 创建子项目 总控项目包括了三个子项目在总控项目文件夹下面创建3个子项目如下: contentSearch-dal: project xmlnshttp://maven.apache.org/POM/4.0.0 xmlns:xsihttp://www.w3.org/2001/XMLSchema-instance xsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd modelVersion4.0.0/modelVersion parent groupId com.companyName.contentSearch /groupId artifactIdcontentSearch/artifactId version1.0/version /parent artifactIdcontentSearch-dal/artifactId packagingjar/packaging version1.0/version namecontentSearch-dal/name urlhttp://maven.apache.org/url dependencies dependency groupIdjunit/groupId artifactIdjunit/artifactId version3.8.1/version scopetest/scope /dependency dependency groupIdgeneralorm/groupId artifactIdhibernate/artifactId version3.0/version /dependency dependency groupIdmysql/groupId artifactIdmysql-connector-java/artifactId version5.1.6/version /dependency dependency groupIdlog4j/groupId artifactIdlog4j/artifactId version1.2.14/version /dependency dependency groupIddom4j/groupId artifactIddom4j/artifactId version1.6.1/version /dependency dependency groupIdslf4j/groupId artifactIdslf4j-api/artifactId version1.5.8/version /dependency dependency groupIdslf4j/groupId artifactIdslf4j-nop/artifactId version1.5.2/version /dependency dependency groupIdantlr/groupId artifactIdantlr/artifactId version2.7.6/version /dependency dependency groupIdjta/groupId artifactIdjta/artifactId version1.1/version /dependency dependency groupIdjavassist/groupId artifactIdjavassist/artifactId version3.9.0.GA/version /dependency dependency groupIdcommons-collections/groupId artifactIdcommons-collections/artifactId version3.1/version /dependency /dependencies /project contentSearch-biz: project xmlnshttp://maven.apache.org/POM/4.0.0 xmlns:xsihttp://www.w3.org/2001/XMLSchema-instance xsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd modelVersion4.0.0/modelVersion parent groupId com.companyName.contentSearch/groupId artifactIdcontentSearch/artifactId version1.0/version /parent artifactIdcontentSearch-biz/artifactId packagingjar/packaging version1.0/version namecontentSearch-biz/name urlhttp://maven.apache.org/url dependencies dependency groupIdjunit/groupId artifactIdjunit/artifactId version3.8.1/version scopetest/scope /dependency dependency groupId com.companyName.contentSearch /groupId artifactIdcontentSearch-dal/artifactId version1.0/version scope/scope /dependency dependency groupIdcom.sun/groupId artifactIdmail/artifactId version1.4.3/version scope/scope /dependency dependency groupIdcom.sun/groupId artifactIdactivation/artifactId version1.1.1/version scope/scope /dependency /dependencies /project contentSearch-web: project xmlnshttp://maven.apache.org/POM/4.0.0 xmlns:xsihttp://www.w3.org/2001/XMLSchema-instance xsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd modelVersion4.0.0/modelVersion parent groupId com.companyName.contentSearch /groupId artifactIdcontentSearch/artifactId version1.0/version /parent artifactIdcontentSearch-web/artifactId packagingwar/packaging version1.0-SNAPSHOT/version namecontentSearch-web Maven Webapp/name urlhttp://maven.apache.org/url dependencies dependency groupIdapache.tomcat/groupId artifactIdservlet-api/artifactId version2.5/version /dependency dependency groupIdapache.commons/groupId artifactIdcommons-fileupload/artifactId version1.2.1/version /dependency dependency groupIdcommons-logging/groupId artifactIdcommons-logging/artifactId version1.0.4/version /dependency dependency groupIdapache.tomcat/groupId artifactIdjsp-api/artifactId version1.0/version /dependency dependency groupIdjunit/groupId artifactIdjunit/artifactId version3.8.1/version scopetest/scope /dependency dependency groupIdframework/groupId artifactIdstruts2-core/artifactId version2.1.8/version /dependency dependency groupIdframework/groupId artifactIdxwork-core/artifactId version2.1.6/version /dependency dependency groupIdframework/groupId artifactIdspring-core/artifactId version2.5.6/version /dependency dependency groupIdframework/groupId artifactIdspring-web/artifactId version2.5.6/version /dependency dependency groupIdframework/groupId artifactIdspring-context/artifactId version2.5.6/version /dependency dependency groupIdframework/groupId artifactIdspring-beans/artifactId version2.5.6/version /dependency dependency groupIdframework/groupId artifactIdfreemarker/artifactId version2.3.15/version /dependency dependency groupIdframework/groupId artifactIdstruts2-spring-plugin/artifactId version2.1.8/version /dependency dependency groupId com.companyName.contentSearch /groupId artifactIdcontentSearch-biz/artifactId version1.0/version /dependency dependency groupIdframework/groupId artifactIdstruts2-json-plugin/artifactId version2.1.8/version /dependency /dependencies build finalNamecontentSearch-web/finalName plugins plugin groupIdorg.mortbay.jetty/groupId artifactIdmaven-jetty-plugin/artifactId version6.1.9/version /plugin /plugins /build /project 4 编写代码打包运行。 mvn clean install mvn jetty:run 以上只是从构建的角度介绍maven的使用还有很多其他的特性待以后学习后再做介绍。作为一个优秀的构建工具maven还是值得推荐的。 http://maven.apache.org/guides/ 附上官网上的指南地址。转载于:https://www.cnblogs.com/langren1992/p/5221993.html