maven搭建多模块项目

来源:互联网 发布:网络应急演练报告 编辑:程序博客网 时间:2024/05/23 15:46

搭建多模块maven项目文章,推荐:

首先,前面几次学习已经学会了安装maven,如何创建maven项目等,最近的学习,终于有点进展了,搭建一下企业级多模块项目。

好了,废话不多说,具体如下:

首先新建一个maven项目,pom.xml的文件如下:

28103310 85791013ebf2450c80f0f6f74c5f73a2  maven搭建多模块项目

搭建多模块项目,必须要有一个packaging为pom的根目录。创建好这个maven项目后,我们对着项目右键–>new

28103915 d15b6c20727b4f3bb76219d07451bbbe  maven搭建多模块项目

输入你的项目名称

28104128 c2dec3c0eede49acbd0653e74909817a  maven搭建多模块项目

这里就不重复说创建项目了,创建好的目录结构在eclipse中如下:

28104559 76202a0de2cd49cd9d6d0a1d31b41566  maven搭建多模块项目

说明一下这些项目具体都是干嘛的:

easyframework-model:数据模型,与数据库表字段对应的实体类

easyframework-core:核心业务项目。主要是Service处理业务逻辑

easyframework-persist:数据持久层,操作低层数据库。

easyframework-utils:工具类,所有工具类都提取出来写在这个项目中。

easyframework-web :这个就是整个项目的web层了,页面的显示以及控制层

备注:创建这些项目的时候,只有easyframework-web是web项目即maven的:maven-archetype-webapp,其他的都是java项目:maven-archetype-quicktart

打开easyframework-root的pom.xml文件,你会看到模块化是这样的:

28105653 9fab1b75c9fb419589710d460c6621d1  maven搭建多模块项目

接下来是配置各个模块的依赖关系,我个人认为的项目是这样依赖的,不知道对不对,呵呵….

28115516 c6565f83ba8a4c5f96706956eea0b761  maven搭建多模块项目

举个例子easyframework-web这个项目依赖easyframework-core(业务核心)和easyframework-model(实体类),easyframework-utils(公共的工具类)这个三个模块。

那么在怎么在easyframework-web的pom.xml中体现呢,具体如下:

 28122324 02f43c2d333c4a49aff9e83cf4a6e8fa  maven搭建多模块项目

打开项目的maven依赖你会发现,已经依赖了这三个项目

28123235 b1489b764e06415eb8d109a5bd7e8351  maven搭建多模块项目

但是你应该会感觉到奇怪,为什么会有那么jar包,明明只引用了这三个项目,哪来的那么多jar包。

你会发现,我再pom.xml文件中,有个parent节点,继承了根节点的pom,这就是maven的项目继承依赖,会从父POM中继承一些值。这对构建一个大型的系统来说很有必要

这样的话你就不需要一遍又一遍的重复添加同样的依赖元素,当然,如果你在子项目中也有同样的依赖,则会覆盖父POM中的值。

父POM的的依赖如下:

copycode  maven搭建多模块项目<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>
    <groupId>com.easyframework</groupId>
    <artifactId>easyframework-root</artifactId>
    <packaging>pom</packaging>
    <version>1.0</version>
    <name>easyframework-root</name>
    <url>http://maven.apache.org</url>
    <modules>
        <module>easyframework-web</module>
        <module>easyframework-persist</module>
        <module>easyframework-core</module>
        <module>easyframework-utils</module>
        <module>easyframework-model</module>
    </modules>
    <properties>
        <!–指定Maven用什么编码来读取源码及文档 –>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <!–指定Maven用什么编码来呈现站点的HTML文件 –>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <mysql.version>5.1.25</mysql.version>
        <hibernate.version>4.2.2.Final</hibernate.version>
        <spring.version>3.2.3.RELEASE</spring.version>
        <aspectj.version>1.7.2</aspectj.version>
    </properties>
    <repositories>
        <repository>
            <id>springsource-repo</id>
            <name>SpringSource Repository</name>
            <url>http://repo.springsource.org/release</url>
        </repository>
    </repositories>
    <dependencies>
    
        <!– log4j –>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
        <!– junit –>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
        <!– mysql数据库驱动 –>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${mysql.version}</version>
        </dependency>
        <!– hibernate4 –>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>${hibernate.version}</version>
        </dependency>
        <!– aspectjweaver –>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>${aspectj.version}</version>
        </dependency>
        <!– spring3 –>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-expression</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>${spring.version}</version>
        </dependency>
    </dependencies>
    <build>
        <finalName>easyframework-root</finalName>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

当然这个父POM只是一个例子,你可以根据自己的配置添加相关的依赖,这里给一个我认为是最好用的仓库:

http://mvnrepository.com/ 相信地球人都知道这个!哈哈…..

到此就搭建好了企业级多模块的项目环境了。

0 0
原创粉丝点击