MAVEN简介:
Apache Maven是一个软件项目管理和综合工具。Maven简化和标准化项目建设过程。处理编译,分配,文档,团队协作和其他任务的无缝连接。
Maven让开发人员的工作更轻松,同时创建报表,检查,构建和测试自动化设置。基于项目对象模型(POM)的概念,
Maven可以从一个中心资料片管理项目构建,报告和文件。
MAVEN优势:
- 最强大的功能就是能够自动下载项目依赖库,不需要自行查找项目依赖jar包。
- Maven项目的结构和内容在一个XML文件中声明,pom.xml 项目对象模型(POM),这是整个Maven系统的基本单元。
MAVEN下载地址:http://maven.apache.org/download.html
http://archive.apache.org/dist/maven/maven-3/
JAVA常用jar包下载地址: http://www.java2s.com/Code/Jar
MAVEN教程地址:http://www.yiibai.com/maven/
MAVEN中央仓库: http://search.maven.org
http://maven.aliyun.com/nexus/content/groups/public/
http://mvnrepository.com/
http://repo1.maven.org/maven2/
MAVEN中jar pom 查找:
http://maven.aliyun.com/nexus
http://192.168.1.91:8081/nexus(自己搭建的Nexus)
MAVEN仓库搜索服务地址:
Sonatype Nexus:https://repository.sonatype.org/
MVNrepository:http://mvnrepository.com/
MAVEN常见错误: http://blog.csdn.net/nthack5730/article/details/46633287
MAVEN安装:
1.将下载的MAVEN解压要自己指定的文件位置。
2.配置环境变量:
MAVEN_HOME: D:\softInstal\apache-maven-3.2.3
class变量: ;%MAVEN_HOME%\bin
命令行输入:mvn -v 检查环境变量是否配置OK
3.修改本地仓库默认位置
当建立一个Maven项目,所有相关文件会自动从中央仓库下载文件,并存储在Maven本地仓库。
Maven的本地资源库默认为 .m2 目录文件夹:
- Unix/Mac OS X – ~/.m2
- Windows – C:\Documents and Settings\{your-username}\.m2
修改默认仓库地址:
打开: [MAVEN_HOME]/config/setting.xml
修改:<localRepository>内容,指向自己设定的仓库文件存储位置。
设置默认的远端仓库和jdk版本:
在setting中设置jdk版本,设置后在eclipse中的MAVEN需要关联此setting。也可在具体项目的pom.xml中设置。
<profile> <id>jdk-1.8</id> <activation> <activeByDefault>true</activeByDefault> <jdk>1.8</jdk> </activation> <properties> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion> </properties>
设置默认的仓库
MAVEN 常用命令:
- mvn archetype:generate 创建Maven项目
- mvn compile 编译源代码
- mvn deploy 发布项目
- mvn test-compile 编译测试源代码
- mvn test 运行应用程序中的单元测试
- mvn site 生成项目相关信息的网站
- mvn clean 清除项目目录中的生成结果
- mvn package 根据项目生成的jar
- mvn install 在本地Repository中安装jar
- mvn eclipse:eclipse 生成eclipse项目文件
- mvnjetty:run 启动jetty服务
- mvntomcat:run 启动tomcat服务
- mvn clean package -Dmaven.test.skip=true:清除以前的包后重新打包,跳过测试类
MAVEN POM文件:
POM代表项目对象模型。它是 Maven 中工作的基本单位,这是一个 XML 文件。它始终保存在该项目基本目录中的 pom.xml
文件。
POM 包含的项目是使用 Maven 来构建的,它用来包含各种配置信息。
POM 也包含了目标和插件。在执行任务或目标时,Maven 会使用当前目录中的 POM。它读取POM得到所需要的配置信息,然后执行目标。
POM 文件要项目元素必须有三个必填字段: groupId
,artifactId
,version
groupId:公司的项目组名
artifactId:项目名
version:版本
在库中的项目符号是:groupId:artifactId:version。
根节点为project
以上内容在Maven库中必须唯一。
超级Pom:
在eclipse中打开pom.xml 会看到有个 effective pom,这个里面就是MAVEN工程的结构配置,是所有pom.xml的顶级父类,可通过继承的形式修改。
在eclipse中可查看,也可通过以下命令行查看。
也可通过以下命令将结果导入到本地查看
MAVEN 构建生命周期:
一个典型的 Maven 构建生命周期是由下列顺序的阶段:
Maven有以下三种标准的生命周期:clean-》build-》site
三个周期之前相互独立,但是各个周期内部阶段phrase又是顺序进行。
clean又分为三个步骤:
- pre-clean
- clean
- post-clean
调用clean会删除target中的所有生成文件(配置文件除外)。
也可直接调用mvn clean:post-clean 会自动依次从pre-clean开始执行,到post-clean.
build分23个步骤:
如果我们执行package,会依次从最上面第一个开始至到package结束。
一般我们执行到package或者install即可,如果要
不同的 Maven 目标绑定到 Maven生命周期的不同阶段这是这取决于包类型(JAR/WAR/EAR)。
在pom.xml中安装maven-antrun-plugin插件可回显各个过程。
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-antrun-plugin</artifactId> <version>1.1</version> <executions> <execution> <id>id.pre-clean</id> <phase>pre-clean</phase> <goals> <goal>run</goal> </goals> <configuration> <tasks> <echo>pre-clean phase</echo> </tasks> </configuration> </execution> <execution> <id>id.clean</id> <phase>clean</phase> <goals> <goal>run</goal> </goals> <configuration> <tasks> <echo>clean phase</echo> </tasks> </configuration> </execution> <execution> <id>id.post-clean</id> <phase>post-clean</phase> <goals> <goal>run</goal> </goals> <configuration> <tasks> <echo>post-clean phase</echo> </tasks> </configuration> </execution> </executions> </plugin> </plugins> </build>
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-antrun-plugin</artifactId> <version>1.1</version> <executions> <execution> <id>id.validate</id> <phase>validate</phase> <goals> <goal>run</goal> </goals> <configuration> <tasks> <echo>validate phase</echo> </tasks> </configuration> </execution> <execution> <id>id.compile</id> <phase>compile</phase> <goals> <goal>run</goal> </goals> <configuration> <tasks> <echo>compile phase</echo> </tasks> </configuration> </execution> <execution> <id>id.test</id> <phase>test</phase> <goals> <goal>run</goal> </goals> <configuration> <tasks> <echo>test phase</echo> </tasks> </configuration> </execution> <execution> <id>id.package</id> <phase>package</phase> <goals> <goal>run</goal> </goals> <configuration> <tasks> <echo>package phase</echo> </tasks> </configuration> </execution> <execution> <id>id.deploy</id> <phase>deploy</phase> <goals> <goal>run</goal> </goals> <configuration> <tasks> <echo>deploy phase</echo> </tasks> </configuration> </execution> </executions> </plugin> </plugins> </build>
<plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-antrun-plugin</artifactId> <version>1.1</version> <executions> <execution> <id>id.pre-site</id> <phase>pre-site</phase> <goals> <goal>run</goal> </goals> <configuration> <tasks> <echo>pre-site phase</echo> </tasks> </configuration> </execution> <execution> <id>id.site</id> <phase>site</phase> <goals> <goal>run</goal> </goals> <configuration><tasks> <echo>site phase</echo> </tasks> </configuration> </execution> <execution> <id>id.post-site</id> <phase>post-site</phase> <goals> <goal>run</goal> </goals> <configuration> <tasks> <echo>post-site phase</echo> </tasks> </configuration> </execution> <execution> <id>id.site-deploy</id> <phase>site-deploy</phase> <goals> <goal>run</goal> </goals> <configuration> <tasks> <echo>site-deploy phase</echo> </tasks> </configuration> </execution> </executions> </plugin> </plugins>
site分4个步骤:
- pre-site
- site
- post-site
- site-deploy
Maven的网站插件通常用于创建新的文档,创建报告,部署网站。
一般项目开发到build阶段的install就基本结束了,如果要deploy则需要发布到远端仓库中,这时候就需要在pom中创建发布管理信息。
以下错误:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-site-plugin:3.3:de ploy (default-deploy) on project MavenWeb: Missing distribution management in pr oject MavenWeb Maven Webapp (com.web.mt:MavenWeb:1.0-SNAPSHOT) -> [Help 1]
解决:
pom.xml中添加:
<distributionManagement> <repository> <id>releases</id> <name>public</name> <url>http://maven.aliyun.com/nexus/content/repositories/releases</url> </repository> <snapshotRepository> <id>snapshots</id> <name>snapshots</name> <url>http://maven.aliyun.com/nexus/content/repositories/snapshots</url> </snapshotRepository> </distributionManagement>
>mvn clean
>mvn deploy
以下错误:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-deploy-plugin:2.7: deploy (default-deploy) on project MavenWeb: Failed to deploy artifacts: Could n ot transfer artifact com.web.mt:MavenWeb:war:1.0.0 from/to releases (http://mave n.aliyun.com/nexus/content/repositories/releases): Access denied to: http://mave n.aliyun.com/nexus/content/repositories/releases/com/web/mt/MavenWeb/1.0.0/Maven Web-1.0.0.war, ReasonPhrase: Forbidden. -> [Help 1]
缺少通行证,settings.xml添加
<server> <id>releases</id> <username>*****</username> <password>*****</password> </server>
MAVEN 远端仓库:
参考:
远程仓库搭建: https://my.oschina.net/u/1455528/blog/637063
(转自)Maven远程仓库的各种配置 http://www.cnblogs.com/AlanLee/p/6198413.html
1.远程仓库的配置
在平时的开发中,我们往往不会使用默认的中央仓库,默认的中央仓库访问的速度比较慢,访问的人或许很多,有时候也无法满足我们项目的需求,可能项目需要的某些构件中央仓库中是没有的,而在其他远程仓库中有,如JBoss Maven仓库。这时,可以在pom.xml中配置该仓库,代码如下:
1 <!-- 配置远程仓库 --> 2 <repositories> 3 <repository> 4 <id>jboss</id> 5 <name>JBoss Repository</name> 6 <url>http://repository.jboss.com/maven2/</url> 7 <releases> 8 <enabled>true</enabled> 9 <updatePolicy>daily</updatePolicy> 10 </releases> 11 <snapshots> 12 <enabled>false</enabled> 13 <checksumPolicy>warn</checksumPolicy> 14 </snapshots> 15 <layout>default</layout> 16 </repository> 17 </repositories>
repository:在repositories元素下,可以使用repository子元素声明一个或者多个远程仓库。
id:仓库声明的唯一id,尤其需要注意的是,Maven自带的中央仓库使用的id为central,如果其他仓库声明也使用该id,就会覆盖中央仓库的配置。
name:仓库的名称,让我们直观方便的知道仓库是哪个,暂时没发现其他太大的含义。
url:指向了仓库的地址,一般来说,该地址都基于http协议,Maven用户都可以在浏览器中打开仓库地址浏览构件。
releases和snapshots:用来控制Maven对于发布版构件和快照版构件的下载权限。需要注意的是enabled子元素,该例中releases的enabled值为true,表示开启JBoss仓库的发布版本下载支持,而snapshots的enabled值为false,表示关闭JBoss仓库的快照版本的下载支持。根据该配置,Maven只会从JBoss仓库下载发布版的构件,而不会下载快照版的构件。
layout:元素值default表示仓库的布局是Maven2及Maven3的默认布局,而不是Maven1的布局。基本不会用到Maven1的布局。
其他:对于releases和snapshots来说,除了enabled,它们还包含另外两个子元素updatePolicy和checksumPolicy。
元素updatePolicy用来配置Maven从远处仓库检查更新的频率,默认值是daily,表示Maven每天检查一次。其他可用的值包括:never-从不检查更新;always-每次构建都检查更新;interval:X-每隔X分钟检查一次更新(X为任意整数)。
元素checksumPolicy用来配置Maven检查校验和文件的策略。当构建被部署到Maven仓库中时,会同时部署对应的检验和文件。在下载构件的时候,Maven会验证校验和文件,如果校验和验证失败,当checksumPolicy的值为默认的warn时,Maven会在执行构建时输出警告信息,其他可用的值包括:fail-Maven遇到校验和错误就让构建失败;ignore-使Maven完全忽略校验和错误。
同样还有个插件仓库,即在项目中用到的一些插件:
<pluginRepositories> <pluginRepository> // 写法和Repositories中的一样 </pluginRepository> </pluginRepositories>
2.远程仓库的认证
大部分公共的远程仓库无须认证就可以直接访问,但我们在平时的开发中往往会架设自己的Maven远程仓库,出于安全方面的考虑,我们需要提供认证信息才能访问这样的远程仓库。配置认证信息和配置远程仓库不同,远程仓库可以直接在pom.xml中配置,但是认证信息必须配置在settings.xml文件中。这是因为pom往往是被提交到代码仓库中供所有成员访问的,而settings.xml一般只存在于本机。因此,在settings.xml中配置认证信息更为安全。
1 <settings> 2 2 ... 3 3 <!--配置远程仓库认证信息--> 4 4 <servers> 5 5 <server> 6 6 <id>releases</id> 7 7 <username>admin</username> 8 8 <password>admin123</password> 9 9 </server> 10 10 </servers> 11 11 ... 12 12 </settings>
上面代码我们配置了一个id为releases的远程仓库认证信息。Maven使用settings.xml文件中的servers元素及其子元素server配置仓库认证信息。认证用户名为admin,认证密码为admin123。这里的关键是id元素,settings.xml中server元素的id必须与pom.xml中需要认证的repository元素的id完全一致。正是这个id将认证信息与仓库配置联系在了一起。
3.部署构件至远程仓库
我们使用自己的远程仓库的目的就是在远程仓库中部署我们自己项目的构件以及一些无法从外部仓库直接获取的构件。这样才能在开发时,供其他对团队成员使用。
Maven除了能对项目进行编译、测试、打包之外,还能将项目生成的构件部署到远程仓库中。首先,需要编辑项目的pom.xml文件。配置distributionManagement元素,代码如下:
1 <distributionManagement> 2 <repository> 3 <id>releases</id> 4 <name>public</name> 5 <url>http://59.50.95.66:8081/nexus/content/repositories/releases</url> 6 </repository> 7 <snapshotRepository> 8 <id>snapshots</id> 9 <name>Snapshots</name> 10 <url>http://59.50.95.66:8081/nexus/content/repositories/snapshots</url> 11 </snapshotRepository> 12 </distributionManagement>
distributionManagement包含repository和snapshotRepository子元素,前者表示发布版本(稳定版本)构件的仓库,后者表示快照版本(开发测试版本)的仓库。这两个元素都需要配置id、name和url,id为远程仓库的唯一标识,name是为了方便人阅读,关键的url表示该仓库的地址。
往远程仓库部署构件的时候,往往需要认证,配置认证的方式同上。
配置正确后,运行命令mvn clean deploy,Maven就会将项目构建输出的构件部署到配置对应的远程仓库,如果项目当前的版本是快照版本,则部署到快照版本的仓库地址,否则就部署到发布版本的仓库地址。
私有远端仓库建设:
目前网上流行的仓库管理软件有Apache的Achiva,JFrog的Artifactory和Sonatype的Nexus。由于当前Nexus是最流行的仓库管理软件,因此,我们这里采用Nexus。
Nexus下载地址:http://nexus.sonatype.org/downloads
自带Web容器(Jetty)的Bundle包,另外一种是war包。
参见笔记【Nexus搭建入门】
MAVEN搭建JAVA项目:
前记:可先创建一般java项目,然后通过项目-》右键-》converter maven project实现
1.首先需要了解以下参数:
mvn -h 可查看mvn下当前可输入的参数及意义
mvn -D表示 Define a system property,在创建项目时需要
2. 命令行创建java项目
说明:mvn archetype:generate --创建项目
-DgroupId=com.denny.mt --创建基础包路径,公司或集体名称
-DartifactId=MavenTest --创建项目名
-DarchetypeArtifactId=maven-archetype-quickstart --使用快速创建方式(依托quickstart快速构建,否则会创建的Maven很大,许多不是我们要的)
-DinteractiveMode --设置内部活动模式为false
命令执行到此,build Success后则创建项目成功。此时eclipse还无法识别。
进入项目执行命令:
mvn eclipse:eclipse,则会生成.classpath和.project文件。
此时一个java项目就产生了,可以导入到eclipse中。
pom.xml文件更新:
1.java编译器更新
①直接在pom.xml中加入此内容
1 <build> 2 <finalName>MavenWeb</finalName> 3 <plugins> 4 <plugin> 5 <groupId>org.apache.maven.plugins</groupId> 6 <artifactId>maven-compiler-plugin</artifactId> 7 <version>2.3.2</version> 8 <configuration> 9 <source>1.8</source> 10 <target>1.8</target> 11 </configuration> 12 </plugin> 13 </plugins> 14 </build>
②MAVEN项目右键Maven-》plugin-》add plugin 添加以上内容,生成后,在<configuration>中加入source和target即可。
2.jar包更新
直接打开pom.xml选择dependency,选择需要更新的文件版本号修改。
以上1,2修改后 Maven-》update project,才可以起作用,亦或命令行:mvn eclipse:eclipse
MAVEN eclipse 插件安装:
1. 自动安装:(速度较慢)
eclipse->help->install new soft-> http://m2eclipse.sonatype.org/sites/m2e
2.手动下载,eclipse-maven3-plugin.7z 解压。粗暴方式是直接拷贝放置到eclipse的feature和plugin中。
还可以通过建立link的形式,eclipse 安装的根目录下创建两个文件夹:links,mavenPlugins,将解压的
features和plugins放入其中。在links文件夹下创建maven.link文件,内容为: path=[mavenPlugins]的绝对文件地址(/或者\\)。
重新打开eclipse会在preference下看到有Maven。
然后eclipse->window->preference->Maven->user settings 设置本地Maven的setting文件和本地仓库位置做关联。
->installation 设置maven的本地安装文件。
MAVEN 标准目录结构:
MAVEN Resources文件:
在开发maven项目时,一般都会把properties配置文件放到src/main/resources目录下,针对这个目录,maven的resources对其进行单独的配置。
以上创建的maven项目中,并没有resources文件夹,通过以下方式构造resourses文件夹,切忌不可直接建立folder,否则maven无法将其自动打包,
且文件位置,不在项目识别中。
正确的做法:
[MavenTest]项目 右键 - 》properties -》java build path -》source -》 add folder-》选择 main 点击 【create new folder】-》输入 resources -》 OK。
如果resource下文件夹变为包的形式,处理方式参照(http://www.cnblogs.com/tjudzj/p/7457108.html)
MAVEN 搭建jar包
maven通过pom.xml文件自动关联统一管理这些架包,并使多个java工程共享这些jar包,我们只需要配置pom.xml文件即可,剩下的一切就交个maven自动配置了。
1.自动下载
中央库地址(http://search.maven.org/)查找自己需要的jar包,可在项目上右键-》Maven-》add dependency -》 输入中央库中的文件信息(artifact、group、version)。
因MAVEN中央库为国外网站速度较慢,因此可修改镜像转为国内MAVEN,目前多用阿里的Maven中央仓库。
可修改Maven的setting.xml文件或者Maven repository的setting.xml 文件,找到mirrors进行修改,也可在pom.xml中添加远程仓库的形式添加。
<mirrors> <mirror> <id>alimaven</id> <mirrorOf>central</mirrorOf> <name>aliyun maven</name> <url>http://maven.aliyun.com/nexus/content/groups/public/</url> </mirror> </mirrors>
<mirror> 2 <id>repo2</id> 3 <mirrorOf>central</mirrorOf> 4 <name>Human Readable Name for this Mirror.</name> 5 <url>http://repo2.maven.org/maven2/</url> 6 </mirror> 7 8 <mirror> 9 <id>ui</id> 10 <mirrorOf>central</mirrorOf> 11 <name>Human Readable Name for this Mirror.</name> 12 <url>http://uk.maven.org/maven2/</url> 13 </mirror> 14 15 16 <mirror> 17 <id>ibiblio</id> 18 <mirrorOf>central</mirrorOf> 19 <name>Human Readable Name for this Mirror.</name> 20 <url>http://mirrors.ibiblio.org/pub/mirrors/maven2/</url> 21 </mirror> 22 23 <mirror> 24 <id>jboss-public-repository-group</id> 25 <mirrorOf>central</mirrorOf> 26 <name>JBoss Public Repository Group</name> 27 <url>http://repository.jboss.org/nexus/content/groups/public</url> 28 </mirror> 29 30 <mirror> 31 <id>JBossJBPM</id> 32 <mirrorOf>central</mirrorOf> 33 <name>JBossJBPM Repository</name> 34 <url>https://repository.jboss.org/nexus/content/repositories/releases/</url> 35 </mirror>
如果有自己的私服地址,则可直接写在settings.xml中。
<profiles>
<profile>
<id>dev</id>
<repositories> <repository> <id>local-nexus</id> <name>local-nexus</name> <url>http://192.168.68.135:8081/nexus/content/groups/public/</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>true</enabled> </snapshots> <layout>default</layout> </repository> </repositories> </profile>
</profiles>
<activeProfiles>
<activeProfile>dev</activeProfile>
</activeProfiles>
2.手动下载(定制库到本地资源库)
如果有的文件不能在中央库下载,或者因中央库网络问题无法连接,可采用此种,如:jdbc文件就无法在maven的中央库下载。
Missing artifact ojdbc:ojdbc:jar:14
在中央库手动download所需要的jar文件,或者加载外来的jar文件,如果文件的版本号和文件在一起需要修改为maven文件格式要求,比如:ojdbc14.jar -》 ojdbc-14.jar
手动下载jar包地址,好多jar包在此方便下载(http://www.java2s.com/Code/Jar)
首先,先将本地jar存放到本地资源库:
mvn install:install-file -Dfile=c:\kaptcha-{version}.jar -DgroupId=com.google.code -DartifactId=kaptcha -Dversion={version} -Dpackaging=jar
然后再add dependency即可。
3. MAVEN 部分文件虽不在MAVEN自己的中央库中,但是有些文件却在自己的远程仓库中存在。因此,可以加载远程仓库到本地的pom.xml中,即可引用jar包下载。
序号 | 类 | URL |
1 |
java.net |
https://maven.java.net/content/repositories/public/ |
2 |
jboss |
http://repository.jboss.org/nexus/content/groups/public/ |
1 <repositories> 2 <repository> 3 <id>java.net</id> 4 <url>https://maven.java.net/content/repositories/public/</url> 5 </repository> 6 </repositories>
添加之后,查找文件在中央库找不到时,将在远程库中查找。
MAVEN 打包:
maven打包通过在cmd中输入
即可,文件名称和version在pom.xml中已写好,只有Test测试成功,build才会success。
在 target文件夹下,可看到生成的jar包。
也可以通过
》mvn install
跳过测试打包: mvn install -DskipTests
MAVEN搭建WEB项目:
1. 命令行创建
d:\workSpace>mvn archetype:generate -DgroupId=com.web.mt -DartifactId=MavenWeb -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false
*注意:生成的artifact为 webapp模板
2.转化为可导入eclipse的web项目
其中: -Dwtpversion=2.0表示此为web项目,默认为java项目
3. 导入Maven项目,eclise空白处右键-》Import-》Maven -》 Existing Maven Project。
修改java版本, 参照MAVEN搭建 Java项目中的 java编译器更新
<!-- For Maven Tomcat Plugin -->
1 <plugin> 2 <groupId>org.apache.tomcat.maven</groupId> 3 <artifactId>tomcat7-maven-plugin</artifactId> 4 <version>2.2</version> 5 <configuration> 6 <path>/MavenWeb</path> 7 </configuration> 8 </plugin>
<!-- Eclipse project -->使用这个之后,就不用再用 -Dwtpversion=2.0了,eclipse:eclipse直接就是web项目。
1 <plugin> 2 <groupId>org.apache.maven.plugins</groupId> 3 <artifactId>maven-eclipse-plugin</artifactId> 4 <version>2.9</version> 5 <configuration> 6 <!-- Always download and attach dependencies source code --> 7 <downloadSources>true</downloadSources> 8 <downloadJavadocs>false</downloadJavadocs> 9 <!-- Avoid type mvn eclipse:eclipse -Dwtpversion=2.0 --> 10 <wtpversion>2.0</wtpversion> 11 </configuration> 12 </plugin>
4.tomcat服务器的载入,项目右键-》Server-》tomcat服务器, -》target Server-》tomcat服务器。
5.修改web.xml 因maven创建的工程web版本太低,因此可参照tomcat的web.xml版本来升级。
6. version管理
因多个文件升级时,查找jar不太好找,因此可以将jar的版本号集中管理,文件处仅做引用,方便后期jar包的升级。
<properties> <junit-version>3.8.1</junit-version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties> .......... <version>${junit-version}</version>
添加完需要的jar包后重新更新项目。mvn eclipse:eclipse.
首次构建web项目后,index.jsp文件会报出,因找不到servlet jar文件,可通过target Server添加,亦可在pom.xml中添加 javax.servlet。scope设置为provided.
7. springMVC 的配置(详细见:springMVC配置)
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app version="3.1" 3 xmlns="http://xmlns.jcp.org/xml/ns/javaee" 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5 xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 6 http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"> 7 8 <display-name>MavenWeb</display-name> 9 <welcome-file-list> 10 <welcome-file>index.jsp</welcome-file> 11 </welcome-file-list> 12 13 14 15 16 <servlet> 17 <servlet-name>mvc-dispatcher</servlet-name> 18 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 19 <init-param> 20 <param-name>contextConfigLocation</param-name> 21 <param-value>classpath:/dispatcher-servlet.xml</param-value> 22 </init-param> 23 <load-on-startup>1</load-on-startup> 24 </servlet> 25 <servlet-mapping> 26 <servlet-name>default</servlet-name> 27 <url-pattern>*.css</url-pattern> 28 <url-pattern>*.js</url-pattern> 29 <url-pattern>*.jpg;*.png;*.ico</url-pattern> 30 </servlet-mapping> 31 <servlet-mapping> 32 <servlet-name>mvc-dispatcher</servlet-name> 33 <url-pattern>/</url-pattern> 34 </servlet-mapping> 35 36 <context-param> 37 <param-name>contextConfigLocation</param-name> 38 <param-value>classpath:/applicationContext.xml</param-value> 39 </context-param> 40 41 <listener> 42 <display-name>contextLoaderListener</display-name> 43 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 44 </listener> 45 46 47 48 <session-config> 49 <session-timeout>30</session-timeout> 50 </session-config> 51 52 53 54 </web-app>
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:context="http://www.springframework.org/schema/context" 4 xmlns:mvc="http://www.springframework.org/schema/mvc" 5 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 6 xsi:schemaLocation=" 7 http://www.springframework.org/schema/beans 8 http://www.springframework.org/schema/beans/spring-beans.xsd 9 http://www.springframework.org/schema/context 10 http://www.springframework.org/schema/context/spring-context.xsd 11 http://www.springframework.org/schema/mvc 12 http://www.springframework.org/schema/mvc/spring-mvc.xsd"> 13 <context:component-scan base-package="com.web.mt.home" use-default-filters="false"> 14 <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" /> 15 <context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice" /> 16 </context:component-scan> 17 </beans>
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:context="http://www.springframework.org/schema/context" 4 xmlns:mvc="http://www.springframework.org/schema/mvc" 5 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 6 xsi:schemaLocation=" 7 http://www.springframework.org/schema/beans 8 http://www.springframework.org/schema/beans/spring-beans.xsd 9 http://www.springframework.org/schema/context 10 http://www.springframework.org/schema/context/spring-context.xsd 11 http://www.springframework.org/schema/mvc 12 http://www.springframework.org/schema/mvc/spring-mvc.xsd"> 13 <context:component-scan base-package="com.web.mt.home" use-default-filters="false"> 14 <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" /> 15 <context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice" /> 16 </context:component-scan> 17 <mvc:annotation-driven></mvc:annotation-driven> 18 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 19 <property name="prefix"> 20 <value>/WEB-INF/pages/</value> 21 </property> 22 <property name="suffix"> 23 <value>.jsp</value> 24 </property> 25 </bean> 26 27 <bean id="stringConverter" 28 class="org.springframework.http.converter.StringHttpMessageConverter"> 29 <property name="supportedMediaTypes"> 30 <list> 31 <value>text/plain;charset=UTF-8</value> 32 </list> 33 </property> 34 </bean> 35 36 <!-- 输出对象转JSON支持 --> 37 <bean id="jsonConverter" 38 class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"></bean> 39 <bean 40 class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> 41 <property name="messageConverters"> 42 <list> 43 <ref bean="stringConverter" /> 44 <ref bean="jsonConverter" /> 45 </list> 46 </property> 47 </bean> 48 </beans>
1 <?xml version="1.0" encoding="UTF-8"?> 2 3 <configuration> 4 5 <!-- ch.qos.logback.core.ConsoleAppender 控制台输出 --> 6 <appender name="console" class="ch.qos.logback.core.ConsoleAppender"> 7 <encoder> 8 <pattern>[%-5level] %d{HH:mm:ss.SSS} [%thread] %logger{36} - %msg%n 9 </pattern> 10 </encoder> 11 </appender> 12 13 <!-- ch.qos.logback.core.rolling.RollingFileAppender 文件日志输出 --> 14 <appender name="file" 15 class="ch.qos.logback.core.rolling.RollingFileAppender"> 16 <Encoding>UTF-8</Encoding> 17 <File>d:/log/mavenweb/mavenweb.log</File> 18 <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> 19 <FileNamePattern>d:/log/mavenweb/mavenweb-%d{yyyy-MM-dd}.log 20 </FileNamePattern> 21 <MaxHistory>10</MaxHistory> 22 <TimeBasedFileNamingAndTriggeringPolicy 23 class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"> 24 <MaxFileSize>5MB</MaxFileSize> 25 </TimeBasedFileNamingAndTriggeringPolicy> 26 </rollingPolicy> 27 <layout class="ch.qos.logback.classic.PatternLayout"> 28 <pattern>[%-5level] %d{HH:mm:ss.SSS} [%thread] %logger{36} - %msg%n 29 </pattern> 30 </layout> 31 </appender> 32 33 <!-- 日志级别 --> 34 <root> 35 <!-- 定义了ERROR和INFO级别的日志,分别在FILE文件和控制台输出 --> 36 <level value="error" /> 37 <level value="info" /> 38 <appender-ref ref="file" /> 39 <appender-ref ref="console" /> 40 </root> 41 42 43 </configuration>
1 package com.web.mt.home.controller; 2 3 import org.springframework.stereotype.Controller; 4 import org.springframework.ui.Model; 5 import org.springframework.web.bind.annotation.PathVariable; 6 import org.springframework.web.bind.annotation.RequestMapping; 7 8 @Controller 9 @RequestMapping("/home") 10 public class MavenBaseController { 11 12 /** 13 * 调整页面 14 * @param model 15 * @return 16 */ 17 @RequestMapping("/{page}") 18 public String jumpPage(@PathVariable String page, Model model){ 19 return page; 20 } 21 }
运行:mvn package
最好在打包前执行下clean
mvn clean package
如果在服务启动时,出现ClassNotFound.... ContextLoaderListener.则说明Maven的Dependency lib没有被打包。
项目右键-》properties-》Deployment Assembly -》Add Maven Dependency lib即可。