操作步骤
创建项目目录
进入“工作空间”目录,创建名为tradework 的文件夹,切换至控制台,进入该文件夹。
配置模块
生成各个模块
该步骤会依次生成项目的各个模块,但是生成的模块并没有 创建依赖,只是最简单的并且符合maven 要求的项目结构的模块,关于什么是 maven 的标准项目结构,可以参考 maven 官方文档,或者《 maven 权威指南》。
#core 模块创建
mvn archetype:create -DgroupId=uppower.trade -DartifactId=trade-core
#client 模块创建
mvn archetype:create -DgroupId=uppower.trade -DartifactId=trade-client
#server 模块创建
mvn archetype:create -DgroupId=uppower.trade -DartifactId=trade-server -DpackageName=uppower.trade -DarchetypeArtifactId=maven-archetype-webapp
配置项目模块
在tradework 根目录下新建一个 pom.xml 配置文件,加入如下内容:
- <? xml version = "1.0" encoding = "UTF-8" ?>
- < 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 >
- < parent >
- < groupId > uppower </ groupId >
- < artifactId > parent </ artifactId >
- < version > 1.0.1 </ version >
- </ parent >
- < groupId > uppower.trade </ groupId >
- < artifactId > trade-parent </ artifactId >
- < packaging > pom </ packaging >
- < version > 1.0-SNAPSHOT </ version >
- < name > trade-parent </ name >
- < url > http://maven.apache.org </ url >
- < modules >
- < module > trade-client </ module >
- < module > trade-core </ module >
- < module > trade-server </ module >
- </ modules >
- </ project >
<?xml version="1.0" encoding="UTF-8"?> <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> <parent> <groupId>uppower</groupId> <artifactId>parent</artifactId> <version>1.0.1</version> </parent> <groupId>uppower.trade</groupId> <artifactId>trade-parent</artifactId> <packaging>pom</packaging> <version>1.0-SNAPSHOT</version> <name>trade-parent</name> <url>http://maven.apache.org</url> <modules> <module>trade-client</module> <module>trade-core</module> <module>trade-server</module> </modules> </project>
这里需要注意的是modules 节点,配置了上一步生成的四个模块。这样就组织起了其依赖关系。
生成好各个模块之后在tradework 根目录下会生成三个目录,每个目录下都会有一个 pom.xml 的配置文件,将这些配置文件打开。添加 parent 节点,client模块内容如下:
- < 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 >
- < parent >
- < groupId > uppower.trade </ groupId >
- < artifactId > trade-parent </ artifactId >
- < version > 1.0-SNAPSHOT </ version >
- </ parent >
- < groupId > uppower.trade </ groupId >
- < artifactId > trade-client </ artifactId >
- < packaging > jar </ packaging >
- < version > 1.0-SNAPSHOT </ version >
- < name > trade-client </ name >
- < url > http://maven.apache.org </ url >
- < dependencies >
- < dependency >
- < groupId > junit </ groupId >
- < artifactId > junit </ artifactId >
- </ dependency >
- </ dependencies >
- </ project >
<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> <parent> <groupId>uppower.trade</groupId> <artifactId>trade-parent</artifactId> <version>1.0-SNAPSHOT</version> </parent> <groupId>uppower.trade</groupId> <artifactId>trade-client</artifactId> <packaging>jar</packaging> <version>1.0-SNAPSHOT</version> <name>trade-client</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> </dependency> </dependencies> </project>
同理,针对core模块:
- < 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 >
- < parent >
- < groupId > uppower.trade </ groupId >
- < artifactId > trade-parent </ artifactId >
- < version > 1.0-SNAPSHOT </ version >
- </ parent >
- < groupId > uppower.trade </ groupId >
- < artifactId > trade-core </ artifactId >
- < packaging > jar </ packaging >
- < version > 1.0-SNAPSHOT </ version >
- < name > trade-core </ name >
- < url > http://maven.apache.org </ url >
- < dependencies >
- < dependency >
- < groupId > junit </ groupId >
- < artifactId > junit </ artifactId >
- </ dependency >
- < dependency >
- < groupId > uppower.trade </ groupId >
- < artifactId > trade-client </ artifactId >
- < version > 1.0-SNAPSHOT </ version >
- </ dependency >
- </ dependencies >
- </ project >
<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> <parent> <groupId>uppower.trade</groupId> <artifactId>trade-parent</artifactId> <version>1.0-SNAPSHOT</version> </parent> <groupId>uppower.trade</groupId> <artifactId>trade-core</artifactId> <packaging>jar</packaging> <version>1.0-SNAPSHOT</version> <name>trade-core</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> </dependency> <dependency> <groupId>uppower.trade</groupId> <artifactId>trade-client</artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies> </project>
同理,针对server模块,其中值得注意的是,server模块是打包为war包:
- < 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 >
- < parent >
- < groupId > uppower.trade </ groupId >
- < artifactId > trade-parent </ artifactId >
- < version > 1.0-SNAPSHOT </ version >
- </ parent >
- < groupId > uppower.trade </ groupId >
- < artifactId > trade-server </ artifactId >
- < packaging > war </ packaging >
- < version > 1.0-SNAPSHOT </ version >
- < name > trade-server Maven Webapp </ name >
- < url > http://maven.apache.org </ url >
- < dependencies >
- < dependency >
- < groupId > junit </ groupId >
- < artifactId > junit </ artifactId >
- </ dependency >
- </ dependencies >
- </ project >
<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> <parent> <groupId>uppower.trade</groupId> <artifactId>trade-parent</artifactId> <version>1.0-SNAPSHOT</version> </parent> <groupId>uppower.trade</groupId> <artifactId>trade-server</artifactId> <packaging>war</packaging> <version>1.0-SNAPSHOT</version> <name>trade-server Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> </dependency> </dependencies> </project>
配置依赖
进入tradework 根目录,在 pom.xml 文件中加入 dependencies 节点,加入如下内容:
- < dependencyManagement >
- < dependencies >
- < dependency >
- < groupId > junit </ groupId >
- < artifactId > junit </ artifactId >
- < version > 4.7 </ version >
- < scope > test </ scope >
- </ dependency >
- < dependency >
- < groupId > org.springframework </ groupId >
- < artifactId > spring </ artifactId >
- < version > 2.0.7 </ version >
- </ dependency >
- < dependency >
- < groupId > org.apache.ibatis </ groupId >
- < artifactId > ibatis-sqlmap </ artifactId >
- < version > 2.3.0 </ version >
- </ dependency >
- < dependency >
- < groupId > com.ibatis </ groupId >
- < artifactId > ibatis-dao </ artifactId >
- < version > 2.1.0.565 </ version >
- </ dependency >
- < dependency >
- < groupId > commons-dbcp </ groupId >
- < artifactId > commons-dbcp </ artifactId >
- < version > 1.2.2 </ version >
- </ dependency >
- < dependency >
- < groupId > mysql </ groupId >
- < artifactId > mysql-connector-java </ artifactId >
- < version > 5.1.12 </ version >
- </ dependency >
- < dependency >
- < groupId > javax.servlet </ groupId >
- < artifactId > servlet-api </ artifactId >
- < version > 2.5 </ version >
- </ dependency >
- < dependency >
- < groupId > apache-log4j </ groupId >
- < artifactId > log4j </ artifactId >
- < version > 1.2.15 </ version >
- </ dependency >
- </ dependencies >
- </ dependencyManagement >
<dependencyManagement> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.7</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring</artifactId> <version>2.0.7</version> </dependency> <dependency> <groupId>org.apache.ibatis</groupId> <artifactId>ibatis-sqlmap</artifactId> <version>2.3.0</version> </dependency> <dependency> <groupId>com.ibatis</groupId> <artifactId>ibatis-dao</artifactId> <version>2.1.0.565</version> </dependency> <dependency> <groupId>commons-dbcp</groupId> <artifactId>commons-dbcp</artifactId> <version>1.2.2</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.12</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> </dependency> <dependency> <groupId>apache-log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.15</version> </dependency> </dependencies> </dependencyManagement>
进入tradework 根目录下,trade-server目录,在 pom.xml 文件中加入 dependencies 节点,内容如下:
- < dependencies >
- < dependency >
- < groupId > junit </ groupId >
- < artifactId > junit </ artifactId >
- </ dependency >
- < dependency >
- < groupId > org.springframework </ groupId >
- < artifactId > spring </ artifactId >
- </ dependency >
- < dependency >
- < groupId > org.apache.ibatis </ groupId >
- < artifactId > ibatis-sqlmap </ artifactId >
- </ dependency >
- < dependency >
- < groupId > com.ibatis </ groupId >
- < artifactId > ibatis-dao </ artifactId >
- </ dependency >
- < dependency >
- < groupId > commons-dbcp </ groupId >
- < artifactId > commons-dbcp </ artifactId >
- </ dependency >
- < dependency >
- < groupId > mysql </ groupId >
- < artifactId > mysql-connector-java </ artifactId >
- </ dependency >
- < dependency >
- < groupId > apache-log4j </ groupId >
- < artifactId > log4j </ artifactId >
- </ dependency >
- < dependency >
- < groupId > javax.servlet </ groupId >
- < artifactId > servlet-api </ artifactId >
- </ dependency >
- < dependency >
- < groupId > uppower.trade </ groupId >
- < artifactId > trade-client </ artifactId >
- < version > 1.0-SNAPSHOT </ version >
- </ dependency >
- < dependency >
- < groupId > uppower.trade </ groupId >
- < artifactId > trade-core </ artifactId >
- < version > 1.0-SNAPSHOT </ version >
- </ dependency >
- </ dependencies >
- < build >
- < finalName > trade-server </ finalName >
- </ build >
<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring</artifactId> </dependency> <dependency> <groupId>org.apache.ibatis</groupId> <artifactId>ibatis-sqlmap</artifactId> </dependency> <dependency> <groupId>com.ibatis</groupId> <artifactId>ibatis-dao</artifactId> </dependency> <dependency> <groupId>commons-dbcp</groupId> <artifactId>commons-dbcp</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>apache-log4j</groupId> <artifactId>log4j</artifactId> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> </dependency> <dependency> <groupId>uppower.trade</groupId> <artifactId>trade-client</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <dependency> <groupId>uppower.trade</groupId> <artifactId>trade-core</artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies> <build> <finalName>trade-server</finalName> </build>
修改配置完成之后,在tradework目录下,执行:
mvn eclipse:eclipse
生成 Eclipse 项目文件及包引用定义,注意,需确保定义Classpath Variables: M2_REPO ,指向本地maven类库目录。
打开Eclipse ,导入工程即可。