下载maven的bin,在apache官方网站能够下载。html
下载下来以后,解压,找个路径放进去, 把bin的位置设在环境变量里,新建环境变量MAVEN_HOMEjava
在PATH里加入maven的bin的路径web
因为Maven依赖Java运行环境,所以使用Maven以前须要配置Java的运行环境。下载并安装JDK,配置JDK的环境变量JAVA_HOME,不然maven将没法使用.
apache
配置完毕后,在Windows命令提示符下,输入mvn -v测试一下,配置成功显示如图:api
找到 Maven 下的 conf 下的 settings.xml 配置文件,个人是在 D:\Server\maven\conf\settings.xmltomcat
Maven 的仓库默认是放在本地用户的临时文件夹下面的 .m2 文件夹下的 repository 下,个人是在 C:\Users\admcnm\.m2\repository 目录下,如今咱们来修改将它指定到咱们本身的路径下,我如今要将仓库指定到 D:\Repositories\Maven 目录下,只须要将上面注销的本地仓库打开,而后把相应的路径值写到里面去就好了:app
检查 eclipse 的 maven 插件是否安装成功:Window-->Preferences。Eclipse kepler或者更高的版本自带内置集成了Maven插件eclipse
1. 点击 Add 按钮,选到你本机安装 maven 的路径值webapp
2. 点击 Browse 按钮,选到你 maven 的 setting.xml 配置文件,而后点击 OK,这样就完成了 eclipse maven 插件的配置。maven
a) 本地Maven链接互联网
若是你的机器能够联网,Maven默认链接本身的仓库地址,须要其余仓库地址自行添加。
b) 本地Maven链接Nexus仓库
Nexus仓库能够链接外网,或者手动上传JAR到Nexus仓库,Maven再经过链接Nexus的方式实现下载JAR。
中间通过此步骤
cannot change version of project facet Dynamic Web Module to 3.0 该问题见文末尾总结。
执行mvn:install(或者能出发执行pom.xml里面build逻辑的命令)
<build> <finalName>SpringActivemq</finalName> <pluginManagement> <plugins> <plugin> <groupId>org.eclipse.m2e</groupId> <artifactId>lifecycle-mapping</artifactId> <version>1.0.0</version> <configuration> <lifecycleMappingMetadata> <pluginExecutions> <pluginExecution> <pluginExecutionFilter> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <versionRange>[2.0,)</versionRange> <goals> <goal>copy-dependencies</goal> </goals> </pluginExecutionFilter> <action> <ignore /> </action> </pluginExecution> </pluginExecutions> </lifecycleMappingMetadata> </configuration> </plugin> </plugins> </pluginManagement> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>1.7</source> <target>1.7</target> <encoding>UTF-8</encoding> <showWarnings>true</showWarnings> </configuration> </plugin> <!-- 同步jar到lib下 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <executions> <execution> <id>copy-dependencies</id> <phase>test</phase> <goals> <goal>copy-dependencies</goal> </goals> <configuration> <outputDirectory> src/main/webapp/WEB-INF/lib </outputDirectory> </configuration> </execution> </executions> </plugin> </plugins> </build>
1.不在须要引入tomcat(servlet-api.jar)
2.pom中配置引入servlet-api,期中注意<scope>provided</scope>(依赖servlet-api,打成war包会跟tomcat自带的包[servlet-api]冲突。因此用provided编译、测试时有用,打包发布不包含。)
<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"> <modelVersion>4.0.0</modelVersion> <groupId>com.xxx</groupId> <artifactId>xxxCommons</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>xxxCommons Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <!-- 配置servlet-api依赖 -- > <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency> </dependencies> <build> <finalName>HillCommons</finalName> <plugins> <!-- tomcat启动插件 --> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.2</version> </plugin> </plugins> </build> </project>
3.插件启动tomcat
1.在tomcat中的conf的tomcat_users.xml中建立管理用户和密码,通常选manager-script,在pom.xml中的tomcat插件中用text,不然可能出现403错误
<role rolename="manager-gui"/> <role rolename="manager-script"/> <role rolename="manager-jmx"/> <role rolename="manager-status"/> <role rolename="manager-text"/> <role rolename="admin-gui"/> <user username="admin" password="admin" roles="manager-gui,manager-script,manager-jmx,manager-status,manager-text,admin-gui"/>
2.pom.xml 配置tomcat插件
<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.2</version> <exclusions> <exclusion> <artifactId>junit</artifactId> <groupId>junit</groupId> </exclusion> </exclusions> </dependency> </dependencies> <build> <finalName>maven-web-test</finalName> <pluginManagement> <plugins> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.2</version> <configuration> <url>http://localhost:8080/manager/html</url> <username>admin</username> <password>admin</password> </configuration> </plugin> </plugins> </pluginManagement> </build>
3.配置tomcat部署命令
1)问题描述:使用Eclipse自带的Maven插件建立Web项目时报错
离线解决方式:
1.http://maven.oschina.net/content/groups/public/org/apache/maven/archetypes/maven-archetype-quickstart/ 下载最新版maven-archetype-quickstart-1.1.jar
2.命令行到下载目录下执行mvn install:install-file -DgroupId=org.apache.maven.archetypes -DartifactId=maven-archetype-quickstart -Dversion=1.1 -Dpackaging=jar -Dfile=maven-archetype-quickstart-1.1.jar
2)cannot change version of project facet Dynamic Web Module to 3.0
网上搜下:http://my.oschina.net/cloudcoder/blog/362949
3)eclipse中使用maven插件的时候,运行run as maven build的时候报错-Dmaven.multiModuleProjectDirectory system propery is not set. Check $M2_HOME environment variable and mvn script match.
能够设一个环境变量M2_HOME指向你的maven安装目录MAVEN_HOME=D:\Program Files\apache-maven-3.3.3而后在Window->Preference->Java->Installed JREs->Edit在Default VM arguments中设置- Dmaven.multiModuleProjectDirectory=$MAVEN_HOME
4)两个不一样版本开发IDE(例如两个不一样版本Eclipse都自带集成的mavn插件,但他们因为集成的Mavn插件版本不一样是的本地mavn plugins下的jar版本也不全相同,致使只有个Eclipse的mavn环境能正常使用,另外一个jar版本不匹配)