最近正好在學習Maven的項目管理,整理自己的開發的項目的依賴關系,又恰好需要建立一個web工程。正好一遍學習,一遍記錄下來,與大家分享,同時有問題的地方也歡迎大家指出、讨論。
環境依賴:
Eclipse+Maven Eclipse Plugin
1. 首先當然是建立一個web工程了。
2. 選擇工程->右鍵菜單->configure->convert to Maven project
<a target="_blank" href="http://blog.51cto.com/attachment/201111/225055184.png"></a>
轉換好後,工程上會帶一個M的标志。
3. 配置依賴關系pom.xml
這裡一般都采用一個parent工程統一管理依賴jar包的版本,以免出現jar沖突的情況。
<a target="_blank" href="http://blog.51cto.com/attachment/201111/225122383.png"></a>
這裡我用nocloud-parent工程,統一管理jar。其pom.xml檔案如下:
<modelVersion>4.0.0</modelVersion>
<groupId>lhz.home.nocloud</groupId>
<artifactId>nocloud-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<properties>
<activemq.version>5.5.0</activemq.version>
<cglib.version>2.2.2</cglib.version>
<springframework.version>3.0.6.RELEASE</springframework.version>
<springframework-roo.version>1.0.2.RELEASE</springframework-roo.version>
…………
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.restlet.jse</groupId>
<artifactId>org.restlet</artifactId>
<version>${restlet.version}</version>
</dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${springframework.version}</version>
…………
</dependencies>
</dependencyManagement>
<repositories>
<repository>
<id>maven-restlet</id>
<name>Public online Restlet repository</name>
<url>http://maven.restlet.org</url>
</repository>
</repositories>
</project>
對于dependencyManagement的意義,本博轉帖的另一篇文章中有解釋。這裡變量${springframework.version}引用的值即為前面properties裡定義的值。
nocloud-web的pom.xml檔案:
<artifactId>nocloud-web</artifactId>
<parent>
<groupId>lhz.home.nocloud</groupId>
<artifactId>nocloud-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
………………
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.1</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/WebContent/WEB-INF/lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
這裡着重說明下<build>的部分。利用maven-dependency-plugin這個插件,可以自動講依賴的jar拷貝到指定的目錄下(這裡是WEB-INF/lib下)。${basedir}是一個變量,指目前工程根路徑。這段代碼在Eclipse中會提示錯誤。
maven-dependency-plugin (goals "copy-dependencies", "unpack") is not supported by m2e.
不過并不影響使用,可以通過指令行的方式。
進入pom檔案所在目錄。執行mvn package即可将依賴的jar拷貝到指定的目錄下。
或者加入以下内容:
<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>
<goals>
<goal>copy-dependencies</goal>
</goals>
</pluginExecutionFilter>
<action>
<ignore />
</action>
</pluginExecution>
</pluginExecutions>
</lifecycleMappingMetadata>
</configuration>
</plugin>
即可在Eclipse中正常使用。
本文轉自mushiqianmeng 51CTO部落格,原文連結:http://blog.51cto.com/mushiqianmeng/722305,如需轉載請自行聯系原作者