Maven工具的详细配置和搭建

    xiaoxiao2026-10-01  5

    Maven是一个项目管理工具,它包含了一个项目对象模型 (Project Object Model),     一组标准插件(生命周期各阶段是绑定插件中的目标来完成项目构建工作)     一个项目生命周期(Project Lifecycle),     一个依赖管理系统(Dependency Management System). 一、Maven私服搭建(管理jar包依赖的JavaEE Web系统):     nexus下载地址:http://www.sonatype.org/nexus/     1. 安装私服。        a. 解压nexus-2.8.0-05-bundle.zip。           nexus-2.8.0-05:核心目录。           -- bin/jsw:存放了操作Nexus应用的命令。           -- conf :nexus.properties 配置文件可更改port、host、nexus-work、runtime。       -- sonatype-work:工作目录。          sonatype-work\nexus\storage:所有仓库存储目录。        b. 配置启动nexus应用           nexus-2.8.0-05/bin/jsw/ windows-x86-32:       - console-nexus.bat :控制台运行nexus       - start-nexus.bat:启动nexus服务       - stop-nexus.bat:停止nexus服务       - install-nexus.bat :安装nexus到window服务中       - uninstall-nexus.bat:从window服务中删除nexus       cmd启动nexus:sc start nexus-webapp       cmd停止nexus: sc stop nexus-webapp     2. nexus应用使用。       a. 启动进入Nexus主界面:          - http://localhost:8081/nexus/index.html       b. 登录Nexus:admin/admin123       c. 点击左边菜单Repositories查看所有仓库:          - Public Repositories:仓库组。          - 3rd party:第三方的仓库。          - Maven Central:Maven中央仓库。          - Releases:发行版存放仓库。          - Snapshots:快照版存放仓库。          - fkjava Central: 自定义的仓库。      说明:四种类型的仓库:group(组)、proxy(代理)、hosted(本地)、virtual(虚拟)        d. 创建自己的仓库。           -- Add按钮后在configuration选项中进行配置.           maven中央库地址: http://repo1.maven.org/maven2/      二、Maven下载与安装:    a. maven 官网地址:http://maven.apache.org/    b. 安装maven:       -- 设置M2_HOME 变量  E:\apache-maven-3.2.1       -- 设置系统环境变量path.  E:\apache-maven-3.2.1\bin       -- 在E:\apache-maven-3.2.1/conf/settings.xml文件中更改本地仓库路径:          <localRepository>E:/apache-maven-3.2.1/repo</localRepository>    c. 配置私服(镜像)。         在conf/settings.xml文件中给定仓库下载的镜像:     -- id : 镜像唯一标识符,用来区分不同的mirror         -- mirrorOf : 镜像哪些仓库 *:代表所有的仓库 central: 代表仓库id为central的仓库.     -- name : 镜像名称         -- url : 镜像的URL     <mirror>        <id>public</id>        <mirrorOf>*</mirrorOf>        <name>public Mirror</name>        <url>http://localhost:8081/nexus/content/groups/public</url>     </mirror> 三、Eclipse中Maven插件安装: m2e插件.      eclipse中maven3在线安装地址: http://download.eclipse.org/technology/m2e/releases      Window --> Preferences --> Maven --> User setting 配置关联自已的Maven. 四、 Maven的核心概念     1. Maven 插件和目标  (Plugins and Goals)     -- Maven构建项目由一组标准插件组成。     -- 一个maven插件是由一个或者多个目标组成的。     -- 一个目标是一个明确的任务,它可以作为单独的目标运行,     或者作为一个大的构建的一部分和其它目标一起运行。     一个目标是Maven中的一个“工作单元”。     目标的例子包括Compiler插件中的compile目标,它用来编译项目中的所有源文件,     或者Surefire插件中的test目标,用来运行单元测试。     2. Maven 生命周期 (Lifecycle)       -- Clean Lifecycle 在构建项目之前进行一些清理工作。         pre-clean     clean     post-clean     -- Default Lifecycle 构建的核心部分,初始化、编译、测试、打包、验证、部署和站点生成等。        validate         验证项目是否正确,以及所有为了完整构建必要的信息是否可用        generate-sources     生成所有需要包含在编译过程中的源代码        process-sources         处理源代码,比如过滤一些值        generate-resources   生成所有需要包含在打包过程中的资源文件        process-resources    复制并处理资源文件至目标目录,准备打包        compile                 编译项目的源代码        process-classes         后处理编译生成的文件,例如对Java类进行字节码增强(bytecode enhancement)        generate-test-sources     生成所有包含在测试编译过程中的测试源码        process-test-sources     处理测试源码,比如过滤一些值        generate-test-resources     生成测试需要的资源文件        process-test-resources     复制并处理测试资源文件至测试目标目录        test-compile             编译测试源码至测试目标目录        test                     使用合适的单元测试框架运行测试。这些测试应该不需要代码被打包或发布        prepare-package             在真正的打包之前,执行一些准备打包必要的操作。这通常会产生一个包的展开的处理过的版本(将会在Maven 2.1+中实现)        package                     将编译好的代码打包成可分发的格式,如JAR,WAR,或者EAR        pre-integration-test     执行一些在集成测试运行之前需要的动作。如建立集成测试需要的环境        integration-test     如果有必要的话,处理包并发布至集成测试可以运行的环境        post-integration-test     执行一些在集成测试运行之后需要的动作。如清理集成测试环境。        verify                     执行所有检查,验证包是有效的,符合质量规范        install                     安装包至本地仓库,以备本地的其它项目作为依赖使用        deploy                     复制最终的包至远程仓库,共享给其它开发人员和项目(通常和一次正式的发布相关)     -- Site Lifecycle 生成项目报告,站点,发布站点。        pre-site        site        post-site        site-deploy          说明:生命周期各阶段Phase由一个或多个插件的Goal目标集合而成。              用mvn命令直接执行生命周期中的指定阶段(phase)              mvn phase(阶段名称)              3. Maven 坐标 (Coordinates)       坐标解释(groupId、artifactId、version):       groupId:    创建这个项目的组织名称(一般为组织的域名反写)       artifactId: 表示一个单独项目的唯一标识符(项目名或jar包)       version:    项目的版本号       注意:groupId、artifactId、version它们标识了一个唯一的项目。     4. Maven 资源库(Repositories)           什么是Maven资源库? 资源存储的地方,即所有项目的jar库,插件库。     Maven库中有三种类型         -- local : 本地库          <localRepository>C:/MyLocalRepository</localRepository>         -- remote  : 远程仓库(代理仓库也就是自已的私服)     -- central : 中央存储库(http://repo1.maven.org/maven2/)     Maven的依赖jar包搜索序列:     第一种:本地库 ---> 中央库     第二种:本地库 ---> 远程库 ---> 中央库     5. Maven 依赖管理 (Dependency Management)       a、Transitive Dependency (传递依赖)           你的项目依赖于A,A又依赖于B。你的项目是否要声明你依赖于B?       Maven的回答是它帮你自动管理这种依赖的传递,       你不需要声明你依赖于B,由Maven来做。       b、Dependency Scope (依赖范围)          举例来说,你开发时需要做测试,你需要依赖于junit的jar,但是部署应用时并不需要它,      因为单元测试不会在生产环境上跑,也就是说最终打包的jar或者war不包含junit的jar。      又如你开发web程序,你的servlet/jsp进行编译需要依赖于servlet-jsp的标准api(J2EE的jar),      但是部署时也是不需要它的,因为你的应用服务器肯定有这些东西。          因此,Maven考虑了6中可能的scope供选择:          - compile: 默认的scope。编译、测试、打包全都需要。compile参与依赖传递,就是说,         你的项目A依赖于B(依赖scope是compile),项目C依赖于你的项目A,那么C也就依赖于B。          - provided: 表示JDK或者容器会在Runtime时提供这些(jar),         如上面说到的servlet api。provided的东西在编译和测试时会用到,不参与传递依赖。          - runtime: 表示编译时不需要,但测试和运行时需要,最终打包时会包含进去。          - test: 只用于测试阶段(测试的编译和测试的运行),典型的就是junit的jar。          - system: 和provided类似,但要求jar是你的系统里已有的,             不会在repository里找,如rt.jar,tools.jar这些。          - import: 简单的说,你的项目的pom可以继承另一个项目的pom,            从而继承了父项目的依赖关系,但是因为之后single inheritance的限制,            所以创造了import,使得你可以“导入”或者说“继承”任何一到多个项目的依赖关系。       c. Dependency Classier (依赖jdk版本)          在jdk1.4下编译的还是jdk1.5       d. 解决依赖冲突(排除指定的依赖)          <exclusions>          <exclusion>         <artifactId>servlet-api</artifactId>         <groupId>javax.servlet</groupId>          </exclusion>         </exclusions>     6. 站点生成和报告  (Site Generation and Reporting)        -- mvn site 命令           运行site生命周期阶段 五、Maven标准插件说明(-D指定属性):     注意:标准插件的位置(第一次使用时它会去Maven中央库下载):           Maven本地仓库\org\apache\maven\plugins\      mvn 插件:goal -D属性名=属性值 ……    1. 通过原型创建项目: mvn archetype:create.       -DgroupId=org.fkjava    : 设置项目组织名(一般为公司域名反写)       -DartifactId=hrm        : 设置项目名       -DpackageName           : 设置包名       -DarchetypeArtifactId   : 设置生成项目所使用的原型(默认为:maven-archetype-quickstart)    2. 编译源代码: mvn compiler:compile.    3. 清除项目:mvn clean:clean (只留下源文件).    4. 测试命令:       -- mvn surefire:test : 运行测试代码    5. 安装:mvn install:install    6. 安装指定文件到本地仓库:mvn install:install-file         -DgroupId=<groupId>       : 设置项目代码的包名(一般用组织名)     -DartifactId=<artifactId> : 设置项目名或模块名     -Dversion=1.0.0           : 版本号     -Dpackaging=jar           : 什么类型的文件(jar包)     -Dfile=<myfile.jar>       : 指定jar文件路径与文件名(同目录只需文件名)                 mvn install:install-file -DgroupId=org.springframework -DartifactId=spring-core -Dversion=4.0.2.RELEASE -Dpackaging=jar -Dfile=spring-core-4.0.2.RELEASE.jar     mvn install:install-file -DgroupId=org.directwebremoting -DartifactId=dwr -Dversion=3.0.M1 -Dpackaging=jar -Dfile=dwr.jar           7. 只打jar包: mvn jar:jar    8. 只打war包: mvn war:war    9. 部署最终的包至远程仓库: mvn deploy:deploy      10. 生成eclipse项目文件:mvn eclipse:eclipse    11. 清除eclipse的一些系统设置: mvn eclipse:clean    12. 生成站点目录:mvn site:site       说明:Maven标准插件使用地址: http://maven.apache.org/plugins/index.html 六、Maven创建项目:     第一种方式:通过mvn命令.       -- 创建java项目(通过原型创建项目):          mvn archetype:create -DgroupId=org.fkjava -DartifactId=hrm -DpackageName=org.fkjava.hrm -DarchetypeArtifactId=maven-archetype-quickstart          mvn 插件名:目标 -D参数      -DgroupId : 组织名      -DartifactId: 项目名      -DpackageName : 包名          -DarchetypeArtifactId : 创建项目的原型       -- 创建web项目(通过原型创建项目):          mvn archetype:create -DgroupId=org.fkjava -DartifactId=hrm -DpackageName=org.fkjava.hrm -DarchetypeArtifactId=maven-archetype-webapp              -- 选择原型目录创建项目:          mvn archetype:generate -DarchetypeCatalog=internal (58种原型目录来创建项目)      说明:      -- archetypeCatalog属性:internal(内部), remote(远程)      -- 16:代表java项目、19: 代表web项目     第二种方式:通过eclipse选择原型创建.       maven-archetype-quickstart: java项目              maven-archetype-webapp: 普通的web项目     1. 创建出来的普通的java项目结构:        src/main/java       java源文件        src/main/resources  java资源文件        src/test/java       test源文件        src/test/resources  test资源文件 七、创建java项目(maven-archetype-quickstart):     1. mvn compile 编译java项目.     2. mvn test 运行测试用例.     3. mvn package 编译java项目并打成jar包.     4. mvn clean  清理java项目.     5. mvn install 安装jar包至本地仓库.     6. mvn deploy 部署最终jar包至远程仓库.        a. 部署到releases与snapshots仓库:           第一步:在pom.xml文件中配置部署元素           <distributionManagement>           <!-- 发行版本仓库 -->           <repository>             <id>releases</id>             <name>Nexus Release Repository</name>             <url>http://localhost:8081/nexus/content/repositories/releases/</url>         </repository>         <!-- 快照版本仓库 -->         <snapshotRepository>             <id>snapshots</id>             <name>Nexus Snapshot Repository</name>             <url>http://localhost:8081/nexus/content/repositories/snapshots/</url>         </snapshotRepository>          </distributionManagement>            第二步:在conf/settings.xml文件中配置server权限元素            <server>         <!-- 这是server的id,该id与distributionManagement                 中repository元素的id相匹配 -->         <id>releases</id>         <!-- 登录名 -->         <username>admin</username>         <!-- 密码 -->         <password>admin123</password>         </server>         <server>         <id>snapshots</id>         <username>admin</username>         <password>admin123</password>         </server>       b. 部署到自已的仓库:           第一步:在pom.xml文件中配置部署元素           <distributionManagement>          <!-- 发行版本仓库 -->            <repository>             <id>lxh</id>             <name>Nexus lxh Repository</name>             <url>http://localhost:8081/nexus/content/repositories/lxh/</url>          </repository>       </distributionManagement>           第二步:在conf/settings.xml文件中配置server权限元素       <server>         <id>lxh</id>         <username>admin</username>         <password>admin123</password>       </server>       d. 设置jdk版本:          1. 配置编译插件          <plugin>             <groupId>org.apache.maven.plugins</groupId>             <artifactId>maven-compiler-plugin</artifactId>             <version>3.1</version>             <configuration>                 <source>1.8</source>                 <target>1.8</target>             <encoding>utf-8</encoding>             </configuration>         </plugin>         2. 更新Maven项目 八、创建Web项目(maven-archetype-webapp):     1. mvn compile 编译java项目.     2. mvn test 运行测试用例.     3. mvn package 编译java项目并打成war包.     4. mvn clean  清理java项目.     a. 配置web项目依赖的jar包        <dependency>         <groupId>org.apache.tomcat</groupId>         <artifactId>tomcat-jsp-api</artifactId>         <version>8.0.14</version>         <scope>provided</scope>        </dependency>        <dependency>         <groupId>jstl</groupId>         <artifactId>jstl</artifactId>         <version>1.2</version>         <scope>runtime</scope>        </dependency>     b. 配置web项目运行web容器插件        <plugin>             <groupId>org.apache.tomcat.maven</groupId>             <artifactId>tomcat7-maven-plugin</artifactId>             <version>2.2</version>             <configuration>                 <port>8080</port>             </configuration>         </plugin>            c. 设置jdk版本:          1. 配置编译插件          <plugin>             <groupId>org.apache.maven.plugins</groupId>             <artifactId>maven-compiler-plugin</artifactId>             <version>3.1</version>             <configuration>                 <source>1.8</source>                 <target>1.8</target>             <encoding>utf-8</encoding>             </configuration>          </plugin>          2. 更新Maven项目          九、POM(Project Object Model)项目对象模型(pom.xml)配置:     a、基础元素配置:         <!-- 指定POM对象模型的版本号 -->       <modelVersion>4.0.0</modelVersion>       <!-- 指定组织ID(公司域名反写) -->       <groupId>org.fkjava</groupId>       <!-- 指定项目名 -->       <artifactId>Struts2-MyBatis3</artifactId>       <!-- 指定项目的版本 -->       <version>1.0.0-RELEASE</version>       <!-- 指定项目打包的产物(jar、ear、war、pom) -->       <packaging>pom</packaging>     b、全局属性配置:       <properties>           <currentVersion>${project.version}</currentVersion>         <struts2.version>2.3.16.3</struts2.version>         <mybatis.version>3.2.7</mybatis.version>         <mysql.version>5.1.27</mysql.version>         <tomcat.version>7.0.47</tomcat.version>       </properties>       c、依赖关系配置:       <dependencies>           <dependency>               <groupId>org.fkjava.hrm</groupId>               <artifactId>hrm-server</artifactId>               <version>1.0.0-SNAPSHOT</version>             <type>jar</type>                 <classifier>jdk15</classifier>           </dependency>       </dependencies>     d、构建元素配置:         <!-- 构建项目元素 -->       <build>           <finalName>hrm</finalName>           <!-- 定义默认的目标 -->           <defaultGoal>package</defaultGoal>           <!-- 源码目录 -->           <sourceDirectory>${basedir}/src/main/java</sourceDirectory>           <!-- 测试代码目录 -->           <testSourceDirectory>${basedir}/src/test/java</testSourceDirectory>           <!-- 资源文件 -->           <resources>              <resource>                  <!-- 源目录 -->                  <directory>${basedir}/src/main/resources</directory>              </resource>          </resources>       </build>    e、常用插件配置:       1. 编译插件:         <plugin>         <groupId>org.apache.maven.plugins</groupId>         <artifactId>maven-compiler-plugin</artifactId>         <inherited>true</inherited>         <version>3.1</version>         <configuration>             <source>1.7</source>             <target>1.7</target>             <encoding>utf-8</encoding>         </configuration>         </plugin>       2. 定义打jar包插件      <plugin>         <groupId>org.apache.maven.plugins</groupId>         <artifactId>maven-jar-plugin</artifactId>         <inherited>true</inherited>         <version>2.4</version>         <!-- 指定插件的配置信息 -->         <configuration>             <!-- 指定JDK版本 -->             <classifier>jdk17</classifier>         </configuration>      </plugin>       3. 生成API文档插件      <plugin>         <groupId>org.apache.maven.plugins</groupId>                 <artifactId>maven-javadoc-plugin</artifactId>         <version>2.9.1</version>         <!-- 指定插件需要的配置信息 -->         <configuration>             <!-- 源文件编码 -->             <encoding>UTF-8</encoding>             <!-- 生成时的编码 -->             <charset>UTF-8</charset>             <!-- 文档的编码 -->             <docencoding>UTF-8</docencoding>             <!-- 文档标题 -->             <doctitle>疯狂java</doctitle>             <!-- 窗口标题 -->             <windowtitle>OA办公管理系统</windowtitle>         </configuration>         <executions>             <execution>                 <!-- 指定目标 -->                 <goals><goal>javadoc</goal></goals>                 <!-- 生命周期哪个阶段时,执行上面javadoc目标 -->                 <phase>compile</phase>             </execution>         </executions>       </plugin>        4. tomcat插件:       <plugin>         <groupId>org.apache.tomcat.maven</groupId>         <artifactId>tomcat7-maven-plugin</artifactId>         <version>2.2</version>         <!-- 指定插件需要的配置信息 -->         <configuration>             <!-- 设置端口 -->             <port>8080</port>         </configuration>           <executions>             <execution>                 <!-- 指定目标 -->                 <goals><goal>run</goal></goals>                 <!-- 生命周期哪个阶段时,执行上面run目标 -->                 <phase>package</phase>             </execution>         </executions>       </plugin>        5. jetty插件:       <plugin>         <groupId>org.mortbay.jetty</groupId>         <artifactId>jetty-maven-plugin</artifactId>         <version>8.1.16.v20140903</version>         <configuration>            <!-- 设置web项目配置信息 -->            <webApp>               <contextPath>/${project.artifactId}</contextPath>            </webApp>            <!-- 设置支持热部署 -->            <scanIntervalSeconds>1</scanIntervalSeconds>                    <connectors>                      <connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector">                          <port>9090</port>                      </connector>                    </connectors>                    </configuration>         <executions>             <execution>                 <!-- 指定目标 -->                 <goals><goal>run</goal></goals>                 <!-- 生命周期哪个阶段时,执行上面run目标 -->                 <phase>package</phase>             </execution>         </executions>       </plugin>         f、部署元素配置:       <distributionManagement>           <!-- 发行版本仓库 -->           <repository>             <id>releases</id>             <name>Nexus Release Repository</name>             <url>http://localhost:8081/nexus/content/repositories/releases/</url>         </repository>         <!-- 快照版本仓库 -->         <snapshotRepository>             <id>snapshots</id>             <name>Nexus Snapshot Repository</name>             <url>http://localhost:8081/nexus/content/repositories/snapshots/</url>         </snapshotRepository>       </distributionManagement>    g、站点元素配置:                <!-- 指定项目名称,一般用于文档生成 -->     <name>sm</name>     <!-- 指定公司网站地址 -->     <url>http://maven.apache.org</url>     <!-- 指定项目描述 -->     <description>疯狂java用Struts2、MyBatis3架构项目</description>     <!-- 指定项目的开始时间 -->     <inceptionYear>2014</inceptionYear>
    转载请注明原文地址: https://ju.6miu.com/read-1312497.html
    最新回复(0)