maven构建项目,分包结构

原文来自:https://blog.csdn.net/lemontreey/article/details/54293755

先说项目使用Maven的好处

1、项目构建。Maven定义了软件开发的整套流程体系,并进行了封装,开发人员只需要指定项目的构建流程,无需针对每个流程编写自己的构建脚本。

2、依赖管理。除了项目构建,Maven最核心的功能是软件包的依赖管理,能够自动分析项目所需要的依赖软件包,并到Maven中心仓库去下载。

A)管理依赖的jar包

B)管理工程之间的依赖关系。


传统工程结构


 



Maven管理的工程结构:

不使用maven:工程部署时需要手动复制jar包。完成工程构建。非常繁琐。

使用maven进行工程构建:

使用maven可以实现一步构建。




1.创建springmvc-parent


1.1创建maven工程




1.1.2修改pom文件


[html]  view plain  copy
  1. <!-- 集中定义依赖版本号 -->  
  2.     <properties>  
  3.         <junit.version>4.12</junit.version>  
  4.         <spring.version>4.1.3.RELEASE</spring.version>  
  5.         <mybatis.version>3.2.8</mybatis.version>  
  6.         <mybatis.spring.version>1.2.2</mybatis.spring.version>  
  7.         <mybatis.paginator.version>1.2.15</mybatis.paginator.version>  
  8.         <mysql.version>5.1.32</mysql.version>  
  9.         <slf4j.version>1.6.4</slf4j.version>  
  10.         <jackson.version>2.4.2</jackson.version>  
  11.         <druid.version>1.0.9</druid.version>  
  12.         <httpclient.version>4.3.5</httpclient.version>  
  13.         <jstl.version>1.2</jstl.version>  
  14.         <servlet-api.version>2.5</servlet-api.version>  
  15.         <jsp-api.version>2.0</jsp-api.version>  
  16.         <joda-time.version>2.5</joda-time.version>  
  17.         <commons-lang3.version>3.3.2</commons-lang3.version>  
  18.         <commons-io.version>1.3.2</commons-io.version>  
  19.         <commons-net.version>3.3</commons-net.version>  
  20.         <pagehelper.version>3.4.2-fix</pagehelper.version>  
  21.         <jsqlparser.version>0.9.1</jsqlparser.version>  
  22.         <commons-fileupload.version>1.3.1</commons-fileupload.version>  
  23.         <jedis.version>2.7.2</jedis.version>  
  24.         <solrj.version>4.10.3</solrj.version>  
  25.     </properties>  
  26. <!-- 只定义以来的版本,并不实际依赖-->  
  27.   <dependencyManagement>  
  28.         <dependencies>  
  29.             <!-- 时间操作组件 -->  
  30.             <dependency>  
  31.                 <groupId>joda-time</groupId>  
  32.                 <artifactId>joda-time</artifactId>  
  33.                 <version>${joda-time.version}</version>  
  34.             </dependency>  
  35.             <!-- Apache工具组件 -->  
  36.             <dependency>  
  37.                 <groupId>org.apache.commons</groupId>  
  38.                 <artifactId>commons-lang3</artifactId>  
  39.                 <version>${commons-lang3.version}</version>  
  40.             </dependency>  
  41.             <dependency>  
  42.                 <groupId>org.apache.commons</groupId>  
  43.                 <artifactId>commons-io</artifactId>  
  44.                 <version>${commons-io.version}</version>  
  45.             </dependency>  
  46.             <dependency>  
  47.                 <groupId>commons-net</groupId>  
  48.                 <artifactId>commons-net</artifactId>  
  49.                 <version>${commons-net.version}</version>  
  50.             </dependency>  
  51.             <!-- Jackson Json处理工具包 -->  
  52.             <dependency>  
  53.                 <groupId>com.fasterxml.jackson.core</groupId>  
  54.                 <artifactId>jackson-databind</artifactId>  
  55.                 <version>${jackson.version}</version>  
  56.             </dependency>  
  57.             <!-- httpclient -->  
  58.             <dependency>  
  59.                 <groupId>org.apache.httpcomponents</groupId>  
  60.                 <artifactId>httpclient</artifactId>  
  61.                 <version>${httpclient.version}</version>  
  62.             </dependency>  
  63.             <!-- 单元测试 -->  
  64.             <dependency>  
  65.                 <groupId>junit</groupId>  
  66.                 <artifactId>junit</artifactId>  
  67.                 <version>${junit.version}</version>  
  68.                 <scope>test</scope>  
  69.             </dependency>  
  70.             <!-- 日志处理 -->  
  71.             <dependency>  
  72.                 <groupId>org.slf4j</groupId>  
  73.                 <artifactId>slf4j-log4j12</artifactId>  
  74.                 <version>${slf4j.version}</version>  
  75.             </dependency>  
  76.             <!-- Mybatis -->  
  77.             <dependency>  
  78.                 <groupId>org.mybatis</groupId>  
  79.                 <artifactId>mybatis</artifactId>  
  80.                 <version>${mybatis.version}</version>  
  81.             </dependency>  
  82.             <dependency>  
  83.                 <groupId>org.mybatis</groupId>  
  84.                 <artifactId>mybatis-spring</artifactId>  
  85.                 <version>${mybatis.spring.version}</version>  
  86.             </dependency>  
  87.             <dependency>  
  88.                 <groupId>com.github.miemiedev</groupId>  
  89.                 <artifactId>mybatis-paginator</artifactId>  
  90.                 <version>${mybatis.paginator.version}</version>  
  91.             </dependency>  
  92.             <dependency>  
  93.                 <groupId>com.github.pagehelper</groupId>  
  94.                 <artifactId>pagehelper</artifactId>  
  95.                 <version>${pagehelper.version}</version>  
  96.             </dependency>  
  97.             <!-- MySql -->  
  98.             <dependency>  
  99.                 <groupId>mysql</groupId>  
  100.                 <artifactId>mysql-connector-java</artifactId>  
  101.                 <version>${mysql.version}</version>  
  102.             </dependency>  
  103.             <!-- 连接池 -->  
  104.             <dependency>  
  105.                 <groupId>com.alibaba</groupId>  
  106.                 <artifactId>druid</artifactId>  
  107.                 <version>${druid.version}</version>  
  108.             </dependency>  
  109.             <!-- Spring -->  
  110.             <dependency>  
  111.                 <groupId>org.springframework</groupId>  
  112.                 <artifactId>spring-context</artifactId>  
  113.                 <version>${spring.version}</version>  
  114.             </dependency>  
  115.             <dependency>  
  116.                 <groupId>org.springframework</groupId>  
  117.                 <artifactId>spring-beans</artifactId>  
  118.                 <version>${spring.version}</version>  
  119.             </dependency>  
  120.             <dependency>  
  121.                 <groupId>org.springframework</groupId>  
  122.                 <artifactId>spring-webmvc</artifactId>  
  123.                 <version>${spring.version}</version>  
  124.             </dependency>  
  125.             <dependency>  
  126.                 <groupId>org.springframework</groupId>  
  127.                 <artifactId>spring-jdbc</artifactId>  
  128.                 <version>${spring.version}</version>  
  129.             </dependency>  
  130.             <dependency>  
  131.                 <groupId>org.springframework</groupId>  
  132.                 <artifactId>spring-aspects</artifactId>  
  133.                 <version>${spring.version}</version>  
  134.             </dependency>  
  135.             <!-- JSP相关 -->  
  136.             <dependency>  
  137.                 <groupId>jstl</groupId>  
  138.                 <artifactId>jstl</artifactId>  
  139.                 <version>${jstl.version}</version>  
  140.             </dependency>  
  141.             <dependency>  
  142.                 <groupId>javax.servlet</groupId>  
  143.                 <artifactId>servlet-api</artifactId>  
  144.                 <version>${servlet-api.version}</version>  
  145.                 <scope>provided</scope>  
  146.             </dependency>  
  147.             <dependency>  
  148.                 <groupId>javax.servlet</groupId>  
  149.                 <artifactId>jsp-api</artifactId>  
  150.                 <version>${jsp-api.version}</version>  
  151.                 <scope>provided</scope>  
  152.             </dependency>  
  153.             <!-- 文件上传组件 -->  
  154.             <dependency>  
  155.                 <groupId>commons-fileupload</groupId>  
  156.                 <artifactId>commons-fileupload</artifactId>  
  157.                 <version>${commons-fileupload.version}</version>  
  158.             </dependency>  
  159.             <!-- Redis客户端 -->  
  160.             <dependency>  
  161.                 <groupId>redis.clients</groupId>  
  162.                 <artifactId>jedis</artifactId>  
  163.                 <version>${jedis.version}</version>  
  164.             </dependency>  
  165.             <!-- solr客户端 -->  
  166.             <dependency>  
  167.                 <groupId>org.apache.solr</groupId>  
  168.                 <artifactId>solr-solrj</artifactId>  
  169.                 <version>${solrj.version}</version>  
  170.             </dependency>  
  171.         </dependencies>  
  172.     </dependencyManagement>  
  173.       
  174.     <build>  
  175.         <finalName>${project.artifactId}</finalName>  
  176.         <plugins>  
  177.             <!-- 资源文件拷贝插件,实际依赖 -->  
  178.             <plugin>  
  179.                 <groupId>org.apache.maven.plugins</groupId>  
  180.                 <artifactId>maven-resources-plugin</artifactId>  
  181.                 <version>2.7</version>  
  182.                 <configuration>  
  183.                     <encoding>UTF-8</encoding>  
  184.                 </configuration>  
  185.             </plugin>  
  186.             <!-- java编译插件 -->  
  187.             <plugin>  
  188.                 <groupId>org.apache.maven.plugins</groupId>  
  189.                 <artifactId>maven-compiler-plugin</artifactId>  
  190.                 <version>3.2</version>  
  191.                 <configuration>  
  192.                     <source>1.7</source>  
  193.                     <target>1.7</target>  
  194.                     <encoding>UTF-8</encoding>  
  195.                 </configuration>  
  196.             </plugin>  
  197.         </plugins>  
  198.         <pluginManagement>  
  199.             <plugins>  
  200.                 <!-- 配置Tomcat插件 -->  
  201.                 <plugin>  
  202.                     <groupId>org.apache.tomcat.maven</groupId>  
  203.                     <artifactId>tomcat7-maven-plugin</artifactId>  
  204.                     <version>2.2</version>  
  205.                 </plugin>  
  206.             </plugins>  
  207.         </pluginManagement>  
  208.     </build>    


1.1.3 将spring-parent安装到本地仓库



2.创建spring-common


注意:我们这里创建common项目的用途是用来放其他工程需要用到的通用组件、工具类、以及单元测试等等,可以让整个结构体系看起来更加清晰,明确


2.1.1 创建工程(由于我之前建过这个项目所以我这里只是演示下)





2.1.2修改pom文件


[html]  view plain  copy
  1. <!-- jar包的依赖 -->  
  2.     <dependencies>  
  3.         <!-- 时间操作组件 -->  
  4.         <dependency>  
  5.             <groupId>joda-time</groupId>  
  6.             <artifactId>joda-time</artifactId>  
  7.         </dependency>  
  8.         <!-- Apache工具组件 -->  
  9.         <dependency>  
  10.             <groupId>org.apache.commons</groupId>  
  11.             <artifactId>commons-lang3</artifactId>  
  12.         </dependency>  
  13.         <dependency>  
  14.             <groupId>org.apache.commons</groupId>  
  15.             <artifactId>commons-io</artifactId>  
  16.         </dependency>  
  17.         <dependency>  
  18.             <groupId>commons-net</groupId>  
  19.             <artifactId>commons-net</artifactId>  
  20.         </dependency>  
  21.         <!-- Jackson Json处理工具包 -->  
  22.         <dependency>  
  23.             <groupId>com.fasterxml.jackson.core</groupId>  
  24.             <artifactId>jackson-databind</artifactId>  
  25.         </dependency>  
  26.         <!-- httpclient -->  
  27.         <dependency>  
  28.             <groupId>org.apache.httpcomponents</groupId>  
  29.             <artifactId>httpclient</artifactId>  
  30.         </dependency>  
  31.         <!-- 单元测试 -->  
  32.         <dependency>  
  33.             <groupId>junit</groupId>  
  34.             <artifactId>junit</artifactId>  
  35.             <scope>test</scope>  
  36.         </dependency>  
  37.         <!-- 日志处理 -->  
  38.         <dependency>  
  39.             <groupId>org.slf4j</groupId>  
  40.             <artifactId>slf4j-log4j12</artifactId>  
  41.         </dependency>  
  42.     </dependencies>  

2.1.3 更新工程

右键项目->Maven->Update Project Configuration

3.创建spring-manager




注意这是个pom工程

5.1修改pom文件

[html]  view plain  copy
  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  2.     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  
  3.     <modelVersion>4.0.0</modelVersion>  
  4.     <parent>  
  5.         <groupId>com.yanl</groupId>  
  6.         <artifactId>springmvc-parent</artifactId>  
  7.         <version>0.0.1-SNAPSHOT</version>  
  8.     </parent>  
  9.     <groupId>com.yanl</groupId>  
  10.     <artifactId>spring-manager</artifactId>  
  11.     <version>0.0.1-SNAPSHOT</version>  
  12.     <packaging>pom</packaging>  
  13.     <description>聚合工程,包含4个模块三个jar模块entity,mapper,service以及war模块controller</description>  
  14.     <!-- 依赖管理 -->  
  15.     <dependencies>  
  16.         <dependency>  
  17.             <groupId>com.yanl</groupId>  
  18.             <artifactId>spring-common</artifactId>  
  19.             <version>0.0.1-SNAPSHOT</version>  
  20.         </dependency>  
  21.     </dependencies>  
  22.     <modules>  
  23.         <module>spring-manager-entity</module>  
  24.         <module>spring-manager-mapper</module>  
  25.         <module>spring-manager-service</module>  
  26.         <module>spring-manager-web</module>  
  27.     </modules>  
  28.     <build>  
  29.         <plugins>  
  30.             <plugin>  
  31.                 <groupId>org.apache.tomcat.maven</groupId>  
  32.                 <artifactId>tomcat7-maven-plugin</artifactId>  
  33.                 <configuration>  
  34.                     <port>8080</port>  
  35.                     <path>/</path>  
  36.                 </configuration>  
  37.             </plugin>  
  38.         </plugins>  
  39.     </build>  
  40. </project>  

4.创建spring-manager-entity


注意:这是一个在manager里面的模块所以右键spring-manager项目选择新建项目的Maven Module







5.创建spring-manager-mapper




5.1修改pom文件

[html]  view plain  copy
  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  2.     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  
  3.     <modelVersion>4.0.0</modelVersion>  
  4.     <parent>  
  5.         <groupId>com.yanl</groupId>  
  6.         <artifactId>spring-manager</artifactId>  
  7.         <version>0.0.1-SNAPSHOT</version>  
  8.     </parent>  
  9.     <artifactId>spring-manager-mapper</artifactId>  
  10.     <!-- 依赖管理 -->  
  11.     <dependencies>  
  12.         <dependency>  
  13.             <groupId>com.yanl</groupId>  
  14.             <artifactId>spring-manager-entity</artifactId>  
  15.             <version>0.0.1-SNAPSHOT</version>  
  16.         </dependency>  
  17.         <!-- Mybatis -->  
  18.             <dependency>  
  19.                 <groupId>org.mybatis</groupId>  
  20.                 <artifactId>mybatis</artifactId>  
  21.             </dependency>  
  22.             <dependency>  
  23.                 <groupId>org.mybatis</groupId>  
  24.                 <artifactId>mybatis-spring</artifactId>  
  25.             </dependency>  
  26.             <dependency>  
  27.                 <groupId>com.github.miemiedev</groupId>  
  28.                 <artifactId>mybatis-paginator</artifactId>  
  29.             </dependency>  
  30.             <dependency>  
  31.                 <groupId>com.github.pagehelper</groupId>  
  32.                 <artifactId>pagehelper</artifactId>  
  33.             </dependency>  
  34.             <!-- MySql -->  
  35.             <dependency>  
  36.                 <groupId>mysql</groupId>  
  37.                 <artifactId>mysql-connector-java</artifactId>  
  38.             </dependency>  
  39.             <!-- 连接池 -->  
  40.             <dependency>  
  41.                 <groupId>com.alibaba</groupId>  
  42.                 <artifactId>druid</artifactId>  
  43.             </dependency>  
  44.     </dependencies>  
  45. </project>  


6.创建spring-manager-service




6.1修改pom文件

[html]  view plain  copy
  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  2.     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  
  3.     <modelVersion>4.0.0</modelVersion>  
  4.     <parent>  
  5.         <groupId>com.yanl</groupId>  
  6.         <artifactId>spring-manager</artifactId>  
  7.         <version>0.0.1-SNAPSHOT</version>  
  8.     </parent>  
  9.     <artifactId>spring-manager-service</artifactId>  
  10.     <description>service模块</description>  
  11.     <!-- 依赖管理 -->  
  12.     <dependencies>  
  13.         <dependency>  
  14.             <groupId>com.yanl</groupId>  
  15.             <artifactId>spring-manager-mapper</artifactId>  
  16.             <version>0.0.1-SNAPSHOT</version>  
  17.         </dependency>  
  18.         <!-- Spring -->  
  19.         <dependency>  
  20.             <groupId>org.springframework</groupId>  
  21.             <artifactId>spring-context</artifactId>  
  22.         </dependency>  
  23.         <dependency>  
  24.             <groupId>org.springframework</groupId>  
  25.             <artifactId>spring-beans</artifactId>  
  26.         </dependency>  
  27.         <dependency>  
  28.             <groupId>org.springframework</groupId>  
  29.             <artifactId>spring-webmvc</artifactId>  
  30.         </dependency>  
  31.         <dependency>  
  32.             <groupId>org.springframework</groupId>  
  33.             <artifactId>spring-jdbc</artifactId>  
  34.         </dependency>  
  35.         <dependency>  
  36.             <groupId>org.springframework</groupId>  
  37.             <artifactId>spring-aspects</artifactId>  
  38.         </dependency>  
  39.     </dependencies>  
  40. </project>  





7.创建spring-manager-web(这个就是我们的前端控制器controller)




7.1修改pom文件

[html]  view plain  copy
  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  2.     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  
  3.     <modelVersion>4.0.0</modelVersion>  
  4.     <parent>  
  5.         <groupId>com.yanl</groupId>  
  6.         <artifactId>spring-manager</artifactId>  
  7.         <version>0.0.1-SNAPSHOT</version>  
  8.     </parent>  
  9.     <artifactId>spring-manager-web</artifactId>  
  10.     <packaging>war</packaging>  
  11.     <!-- 依赖管理 -->  
  12.     <dependencies>  
  13.         <dependency>  
  14.             <groupId>com.yanl</groupId>  
  15.             <artifactId>spring-manager-service</artifactId>  
  16.             <version>0.0.1-SNAPSHOT</version>  
  17.         </dependency>  
  18.         <!-- JSP相关 -->  
  19.         <dependency>  
  20.             <groupId>jstl</groupId>  
  21.             <artifactId>jstl</artifactId>  
  22.             <version>${jstl.version}</version>  
  23.         </dependency>  
  24.         <dependency>  
  25.             <groupId>javax.servlet</groupId>  
  26.             <artifactId>servlet-api</artifactId>  
  27.             <version>${servlet-api.version}</version>  
  28.             <scope>provided</scope>  
  29.         </dependency>  
  30.         <dependency>  
  31.             <groupId>javax.servlet</groupId>  
  32.             <artifactId>jsp-api</artifactId>  
  33.             <version>${jsp-api.version}</version>  
  34.             <scope>provided</scope>  
  35.         </dependency>  
  36.         <!-- 文件上传组件 -->  
  37.         <dependency>  
  38.             <groupId>commons-fileupload</groupId>  
  39.             <artifactId>commons-fileupload</artifactId>  
  40.             <version>${commons-fileupload.version}</version>  
  41.         </dependency>  
  42.     </dependencies>  
  43. </project>  

至此我们的工程建立完成,我们需要测试这个工程能不能用还需要配置如下




8.配置tomcat插件


运行web工程需要添加一个tomcat插件。插件必须添加到spring-manager工程中。因为spring-manager是聚合工程。在运行时需要把子工程聚合到一起才能运行。


上面在创建spring-manager时我已经配置了,这里在配下,提醒。


[html]  view plain  copy
  1. <build>  
  2.         <plugins>  
  3.             <plugin>  
  4.                 <groupId>org.apache.tomcat.maven</groupId>  
  5.                 <artifactId>tomcat7-maven-plugin</artifactId>  
  6.                 <configuration>  
  7.                     <port>8080</port>  
  8.                     <path>/</path>  
  9.                 </configuration>  
  10.             </plugin>  
  11.         </plugins>  
  12.     </build>  

注意:在运行聚合工程之前需要将parent,common工程安装至本地仓库,不然会报找不到依赖的错误。
右键工程- Run as - mvn install

9.运行聚合工程




看到如下,说明运行成功



如果运行聚合工程报错invalid LOC header (bad signature),说明jar没有被maven依赖,有可能是你改了本地maven仓库地址引起的,需要删除之前存在的jar文件夹,让maven重新下载。

使用maven命令运行该工程最好使用 clean tomcat7:run。

然后打开http://localhost:8080/你会看到



大功告成,说明我们的聚合工程没有问题,这里有几点需要解释下

10.1.关于mybatis分页插件pagehelper的问题




有关分页插件请看我之前写过的一篇pagehelper文章。

10.2  Java编译插件




10.3  Build时控制台报错,一般是提示parent,common没有安装,你右键这两个项目安装下在运行spring-manager就可以了。


10.4 需要用到的仓库我上传到我的资源里面了(由于文件太大,我传百度网盘了,传送门 http://pan.baidu.com/s/1cmrIgu),如果maven下载不了直接将这个仓库覆盖你自己的本地仓库,一般本地仓库在C盘用户.m2文件夹下,祝你成功。


10.5 工程项目在我资源页http://download.csdn.net/detail/lemontreey/9738477,我没有clean所以比较大。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值