1.查看maven的版本:mvn -v
2.编译项目:mvn compile (在进入项目的根目录后,src所在文件夹即为根目录)
3.mvn exec:java -Dexec.mainClass="包名.类名":执行指定类的main函数。
4.在maven的settings.xml中设置localRepository将jar包下载到指定路径中,避免下载到c盘。
5.在maven的settings.xml中设置mirror为阿里云镜像,可以提高下载速度:
<mirrors>
<!-- mirror
| Specifies a repository mirror site to use instead of a given repository. The repository that
| this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used
| for inheritance and direct lookup purposes, and must be unique across the set of mirrors.
|
<mirror>
<id>mirrorId</id>
<mirrorOf>repositoryId</mirrorOf>
<name>Human Readable Name for this Mirror.</name>
<url>http://my.repository.com/repo/path</url>
</mirror>
-->
<!-- 更换阿里镜像,加快下载速度 -->
<mirror>
<id>nexus-aliyun</id>
<mirrorOf>central</mirrorOf>
<name>Nexus aliyun</name>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
</mirror>
</mirrors>
6.在maven命令后加上-Dmaven.test.skip=true表示在执行对应的maven操作时跳过单元测试,比 如
mvn deploy -Dmaven.test.skip=true
表示部署项目时跳过单元测试。
7.在idea中用maven创建java项目时在新建项目中选择maven,然后选择javastarter;创建web项目时选择maven后再选择webapp。
8.运行web项目时需要用到jetty或tomcat本地服务器,要在pom.xml的<build>中加入相应的插件:
<!--插件集合-->
<plugins>
<!--jetty插件,一个轻量级服务器-->
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
<version>6.1.25</version>
<configuration>
<!--热部署,每10s扫描一次-->
<scanIntervalSeconds>10</scanIntervalSeconds>
<!--可指定当前项目的站点名-->
<contextPath>/mavenweb_study</contextPath>
<connectors>
<connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">
<!--设置端口号-->
<port>9090</port>
</connector>
</connectors>
</configuration>
</plugin>
<!--tomcat插件-->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.1</version>
<configuration>
<port>8081</port>
<!--项目的站点名,即对外访问路径-->
<path>/mavenweb_study</path>
<uriEncoding>UTF-8</uriEncoding>
<!--服务器名称-->
<server>tomcat7</server>
</configuration>
</plugin>
</plugins>
9.在maven项目中,需要的依赖包可以进入maven的远程仓库,然后输入需要的依赖包,找到需要的版本后就能看到pom.xml中的格式,直接复制到pom.xml中的dependencies里即可。
10.idea中重写快捷键Ctrl+O(字母)。
11.在使用MVC框架时可以先创建普通的maven项目maven_parent,然后对该父项目创建三个子模块maven_dao、maven_service以及maven_controller,其中dao和service均用javastarter创建,controller则用webapp创建,因为controller才是直接处理前端请求的;由于controller是采用webapp创建的,所以web页面自然就写在controller模块中,那么服务器的依赖就要添加在controller的pom.xml中。
12.启动服务器时要先在右上角配置命令parent_install、service_install、dao_install、controller_install,即分别对maven_parent、maven_service、maven_dao、maven_controller进行install,防止最后启动服务器时报错找不到对应的依赖包。配置命令时选择install,并选择对应的目标文件,第一次先执行parent_install,再执行其他的install命令;然后再配置启动服务器命令,选择tomcat:run,设置目标文件为maven_controller,再执行该命令即可成功启动服务器。
其中install的作用是将项目本身编译并打包到本地仓库,这样其他项目在引用本项目的jar包时直接在本地仓库就能找到,在一个项目需要用到另一个项目的东西时就需要先install另一个文件才能找到;package只是将项目打包并放到target文件中,相当于压缩。
13.resources目录用来放置配置资源文件,如xml文件、properties文件等,可以在其中建立不同的目录用来存放不同的测试环境。
14. 在打包时,如果之前已经打包过,则需要clean来清除之前的文件,否则打包的内容还是之前的;代码为:
clean compile package -Ptest -Dmaven.test.skip=true
其中-Ptest表示打包时选择test环境,-Dmaven.test.skip=true表示打包时跳过单元测试文件;compile表示编译,package表示打包。在配置命令时可以插入多个操作,并按照顺序执行。
15.在pom.xml的build中可以设置哪些文件需要打包:
<!--设置哪些文件要打包-->
<resources>
<!--打包环境,具体在打包操作时选择环境-->
<resource>
<!--directory可以打包整个文件夹-->
<directory>src/main/resources/${env}</directory>
</resource>
<!--选择文件-->
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
<include>**/*.tld</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
16.在pom.xml中导入在resources中写好的环境(以下代码属于单独的,不在任何标签中),其中id随便取用来区分,env则跟对应文件名保持一致:
<!--打包环境配置,包括开发环境、测试环境、正式环境-->
<profiles>
<!--添加dev开发环境-->
<profile>
<id>dev</id>
<properties>
<env>dev</env>
</properties>
<!--如果打包时未设置环境(即配置命令时没有-P),则默认开发环境-->
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<!--测试环境-->
<profile>
<id>test</id>
<properties>
<env>test</env>
</properties>
</profile>
<!--正式环境-->
<profile>
<id>product</id>
<properties>
<env>product</env>
</properties>
</profile>
</profiles>