利用m2e插件搭建Java Web工程,管理依赖
最近正好在学习Maven的项目管理,整理自己的开发的项目的依赖关系,又恰好需要新建一个web工程。正好一遍学习,一遍记录下来,与大家分享,同时有问题的地方也欢迎大家指出、讨论。
环境依赖:
Eclipse+Maven Eclipse Plugin
1. 首先当然是新建一个web工程了。
2. 选择工程->右键菜单->configure->convert to Maven project
转换好后,工程上会带一个M的标志。
3. 配置依赖关系pom.xml
这里一般都采用一个parent工程统一管理依赖jar包的版本,以免出现jar冲突的情况。
这里我用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>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-core</artifactId>
- <version>${springframework.version}</version>
- </dependency>
- …………
- </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文件:
- <modelVersion>4.0.0</modelVersion>
- <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>
- </dependency>
- ………………
- </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>
- </project>
这里着重说明下<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,如需转载请自行联系原作者