最近研究了下maven构建项目,确实使用比较方便。学习了pom文件结构和maven的生命周期后,自己建了个maven工程,下面记录下开发环境搭建步骤。
【1】 首先在本机安装个maven,不要使用eclipse自带的maven插件,直接在http://maven.apache.org/获取安装包,参照里面的步骤设置环境变量,设置完后可以在命令行里面用set命令查看下是否生效,比如set path可以查看path,就跟linux下用echo查看环境变量一样。
- Unzip the distribution archive, i.e. apache-maven-3.1.1-bin.zip to the directory you wish to install Maven 3.1.1. These instructions assume you choseC:\Program Files\Apache Software Foundation. The subdirectory apache-maven-3.1.1 will be created from the archive.
- Add the M2_HOME environment variable by opening up the system properties (WinKey + Pause), selecting the "Advanced" tab, and the "Environment Variables" button, then adding the M2_HOME variable in the user variables with the value C:\Program Files\Apache Software Foundation\apache-maven-3.1.1. Be sure to omit any quotation marks around the path even if it contains spaces. Note: For Maven 2.0.9, also be sure that the M2_HOMEdoesn't have a '\' as last character.
- In the same dialog, add the M2 environment variable in the user variables with the value %M2_HOME%\bin.
- Optional: In the same dialog, add the MAVEN_OPTS environment variable in the user variables to specify JVM properties, e.g. the value -Xms256m -Xmx512m. This environment variable can be used to supply extra options to Maven.
- In the same dialog, update/create the Path environment variable in the user variables and prepend the value %M2% to add Maven available in the command line.
- In the same dialog, make sure that JAVA_HOME exists in your user variables or in the system variables and it is set to the location of your JDK, e.g.C:\Program Files\Java\jdk1.5.0_02 and that %JAVA_HOME%\bin is in your Path environment variable.
- Open a new command prompt (Winkey + R then type cmd) and run mvn --version to verify that it is correctly installed.
安装后,设置下eclipse里面的maven路径,选择下setting配置文件
注意settings.xml中本地仓库路径的设定<localRepository>E:/repository</localRepository>,其他的一些配置这里先不提了。
【2】上述步骤完成后,就可以新建Maven工程了,新建maven工程,下图选择的是webapp工程,下面输入Group Id和Artifact Id即可完成工程的创建
【3】选中项目根节点新建source folder目录,典型的目录结构如下图
配置java文件编译后class目标文件的保存路径
【4】右键工程,选择Run as Maven install即可生成war包