1、 maven
1.新建一个maven项目,项目后期的打包,编译,文档均可以经过maven进行维护html
2.能够管理项目依赖的jar包
项目团队中:解决jar文件不统一,为了维护全公司全部jar环境
---- pom.xml ----> struts 2.3java
Maven是一个项目管理工具,它包含了一个项目对象模型 (Project Object Model ---> pom.xml),
一组标准插件(生命周期各阶段是绑定插件中的目标来完成项目构建工做) 编译,打包,测试
一个项目生命周期(Project Lifecycle),
一个依赖管理系统(Dependency Management System). mysql
2、Maven下载与安装:
a. maven 官网地址:http://maven.apache.org/程序员
b. 安装maven:
-- 设置M2_HOME 变量 C:\maven\apache-maven-3.2.5
-- 设置系统环境变量path. C:\maven\apache-maven-3.2.5\bin
建立一个本地仓库:放下载的jar文件等相关的项目插件。
-- 在C:\maven\apache-maven-3.2.5\conf\settings.xml文件中更改本地仓库路径:
<localRepository>C:/maven/apache-maven-3.2.5/repo</localRepository>web
-- 在eclipse中关联你的maven安装目录(相似于关联tomcat同样)spring
maven中央库地址: https://repo1.maven.org/maven2/sql
搜寻jar包网址: http://search.maven.orgapache
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>windows
3、Eclipse中Maven插件安装: m2e插件.api
eclipse中maven3在线安装地址: http://download.eclipse.org/technology/m2e/releases
Window --> Preferences --> Maven --> User setting 配置关联自已的Maven.
4、Maven私服搭建(管理jar包依赖的JavaEE Web系统):
企业会以公司为单位,新建一个maven的私服,这里面放本公司全部的jar文件
全部的程序员都经过maven链接公司的私服进行项目的管理
-- eclipse(maven) ----> 公司的私服下载jar包 ----> 去maven中心库下载
nexus下载地址:http://www.sonatype.org/nexus/
1. 安装私服(公司的maven服务器 ,放全公司的jar包,以及项目生命周期中要使用到的各类插件)。
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.10.0-02\bin\jsw\windows-x86-64:
- 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选项中进行配置.
5、 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 jar包, A又依赖B ,你申明了A ,B就会自动拉取下来。
你的项目依赖于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生命周期阶段
6、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
6、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
src/main/resources java资源文件 配置文件 好比Struts.xml文件
src/test/java test源文件
src/test/resources test资源文件
7、建立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项目
8、建立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项目
9、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用Struts二、MyBatis3架构项目</description> <!-- 指定项目的开始时间 --> <inceptionYear>2014</inceptionYear>