您的位置:首页 > 其它

搭建横向、纵向分层的大型Maven工程项目

2016-12-21 16:13 393 查看
实验环境:
Eclipse版本:Neon Release (4.6.0)
JDK版本:jdk1.7
Maven版本:apache-maven-3.3.9

1.指定工作空间。
打开eclipse,指定一个新的workspace,这里指定为D:\workspace\jthinking。
2.调整eclipse视图显示。
调出Navigator和Package Explorer,将暂时用不到的视图关闭。
3.调整默认配置。
调整eclipse默认配置。
Window->Preferences->General->Workspace->Text file encoding选择UTF-8。
Window->Preferences->Java->Compiler->Compiler compliance level设置为1.7。
Window->Preferences->Installed JREs->Add自己的jdk,不要用默认的jre,版本必须统一。
Window->Preferences->Maven->Installations->Add自己的Maven,不要用默认的。
Window->Preferences->Maven->User Settings->指定自己Maven下的settings.xml文件,在这之前设置一下Maven的本地仓库目录。
调整Maven默认配置。
更改Maven工程默认的jdk版本,在maven的配置文件settings.xml中的<profiles>标签里添加如下代码,这里设置默认JRE编译版本为1.7。
<profile>
<id>jdk-1.7</id>

<activation>
<activeByDefault>true</activeByDefault>
<jdk>1.7</jdk>
</activation>

<properties>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<maven.compiler.compilerVersion>1.7</maven.compiler.compilerVersion>
</properties>
</profile>

maven在默认情况下会从Maven的官网下载所需jar包,但因为是国外网站,速度可能会很慢,可以配置阿里云的私服,在settings.xml中添加如下配置信息:

<mirror>
<id>alimaven</id>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<mirrorOf>central</mirrorOf>
</mirror>


4. 新建工程:
(1)新建parent工程。parent工程主要负责jar包的管理。为pom类型。
在Package Explorer视图空白处右键
new-->Maven Project-->next-->选Artifact Id为quickstart-->next-->
Group Id:一般把域名倒过来填。e.g. com.jthinking
Artifact Id:填项目名。e.g. jthinking-parent
点击Finish。
在工程名上右键Properties,确定已将Java Build Path、Java Compiler的jdk版本统一为1.7。
双击pom.xml文件。在Overview中将Artifact的Packaging改为pom,保存。
将项目默认生成的测试类和低版本的jUnit删除。
如果这时工程报错,在工程右键点击Maven下的Update Project。
添加jar包:
将以下虚线中的内容复制到pom.xml文件中替换原有的properties标签。
<!-- 集中定义依赖版本号 -->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<junit.version>4.10</junit.version>
<spring.version>4.1.3.RELEASE</spring.version>

<mybatis.version>3.2.8</mybatis.version>
<mybatis.spring.version>1.2.2</mybatis.spring.version>
<mybatis.paginator.version>1.2.15</mybatis.paginator.version>
<mysql.version>5.1.32</mysql.version>
<bonecp-spring.version>0.8.0.RELEASE</bonecp-spring.version>
<druid.version>1.0.9</druid.version>

<mapper.version>2.3.2</mapper.version>
<pagehelper.version>3.4.2</pagehelper.version>
<jsqlparser.version>0.9.1</jsqlparser.version>

<slf4j.version>1.6.4</slf4j.version>
<jstl.version>1.2</jstl.version>
<servlet-api.version>2.5</servlet-api.version>
<jsp-api.version>2.0</jsp-api.version>
<joda-time.version>2.5</joda-time.version>

<commons-lang3.version>3.3.2</commons-lang3.version>
<commons-io.version>2.4</commons-io.version>
<commons-fileupload.version>1.3.1</commons-fileupload.version>

<jackson.version>2.4.2</jackson.version>
<httpclient.version>4.3.5</httpclient.version>
<jedis.version>2.6.0</jedis.version>
</properties>

<dependencies>
<!-- 单元测试 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>

<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</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-webmvc</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-aspects</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- Mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>${mybatis.spring.version}</version>
</dependency>
<dependency>
<groupId>com.github.miemiedev</groupId>
<artifactId>mybatis-paginator</artifactId>
<version>${mybatis.paginator.version}</version>
</dependency>
<!-- MySql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>

<!-- 通用Mapper -->
<dependency>
<groupId>com.github.abel533</groupId>
<artifactId>mapper</artifactId>
<version>${mapper.version}</version>
</dependency>

<!-- 分页插件 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>${pagehelper.version}</version>
</dependency>
<dependency>
<groupId>com.github.jsqlparser</groupId>
<artifactId>jsqlparser</artifactId>
<version>${jsqlparser.version}</version>
</dependency>
<!-- 连接池 -->
<dependency>
<groupId>com.jolbox</groupId>
<artifactId>bonecp-spring</artifactId>
<version>${bonecp-spring.version}</version>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j.version}</version>
</dependency>

<!-- Jackson Json处理工具包 -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
<!-- httpclient -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>${httpclient.version}</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.3.1</version>
</dependency>

<!-- JSP相关 -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>${jstl.version}</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>${servlet-api.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jsp-api</artifactId>
<version>${jsp-api.version}</version>
<scope>provided</scope>
</dependency>

<!-- 时间操作组件 -->
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>${joda-time.version}</version>
</dependency>

<!-- Apache工具组件 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${commons-lang3.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
<version>${commons-io.version}</version>
</dependency>

<!-- 文件上传组件 -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>${commons-fileupload.version}</version>
</dependency>

<!-- jedis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>${jedis.version}</version>
</dependency>

<!-- 字符加密、解密 -->
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.9</version>
</dependency>

<!-- 数据校验 -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.1.3.Final</version>
</dependency>

</dependencies>

<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<!-- 资源文件拷贝插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.7</version>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<!-- java编译插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>

parent工程配置完成。

(2)新建common工程。common工程主要负责工具类的管理。为jar类型。
在Package Explorer视图空白处右键
new-->Maven Project-->next-->选Artifact Id为quickstart-->next-->
Group Id:一般把域名倒过来填。e.g. com.jthinking
Artifact Id:填项目名。e.g. jthinking-common
点击Finish。
在工程名上右键Properties,将Java Build Path、Java Compiler的jdk版本统一为1.7。
双击pom.xml文件。
在Overview中将Artifact的Packaging改为jar,保存。
在Parent选项栏右边点击select parent,选择上面创建的parent工程,点击OK,保存。
将项目默认生成的测试类和低版本的jUnit删除。
如果这时工程报错,在工程右键点击Maven下的Update Project。
将common工程添加到jthinking-parent依赖。
在jthinking-parent的pom.xml的Dependencies中点击Add,选择jthinking-common工程,添加,保存。

(3)新建manage工程。manage工程是java后台聚合工程。为pom类型。
它包含四个模块:
com.jthinking.manage.pojo
com.jthinking.manage.mapper
com.jthinking.manage.service
com.jthinking.manage.web
首先新建manage工程,步骤与上述工程的新建类似,需要注意的是:
pom.xml文件的Overview的Artifact的Packaging为pom。
Parent同样是上面创建的parent工程。
新建pojo模块:
在jthinking-manage工程上右键new-->other-->找到Maven Module-->
next-->指定Module Name jthinking-manage-pojo,Parent Project已经自动指定为jthinking-manage,
如果没有自动指定,要手动指定为jthinking-manage-->next-->选择quickstart-->Finish。
接下来将jdk的版本统一为1.7。删除旧版本的jUnit,删除添加的测试类。
pom.xml文件的Overview的Artifact的Packaging为jar。
Parent默认为jthinking-manage。

新建mapper模块:
除了Module Name改为jthinking-manage-mapper,Dependencies依赖依次类推为pojo,别的配置与pojo步骤相同。
新建service模块:
除了Module Name改为jthinking-manage-service,Dependencies依赖依次类推为mapper,别的与pojo的配置步骤相同。
新建web模块:
在jthinking-manage工程上右键new-->other-->找到Maven Module-->
next-->指定Module Name jthinking-manage-web,Parent Project已经自动指定为jthinking-manage,
如果没有自动指定,要手动指定为jthinking-manage-->next-->选择webapp-->Finish。
接下来将jdk的版本统一为1.7。注意web配置jdk版本时多一项为Project Facets。
删除旧版本的jUnit,删除添加的测试类。
pom.xml文件的Overview的Artifact的Packaging为war。
Parent默认为jthinking-manage。
Dependencies依赖依次类推为service。
增加Tomcat插件:在pom.xml中添加如下配置。
<build>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<port>8081</port>
<path>/</path>
</configuration>
</plugin>
</plugins>
</build>


注意!module新建的顺序必须按照pojo,mapper,service,web的顺序。

5. 至此,工程新建完成。

6. 总结:
Maven中的继承和依赖
Maven是一个jar包管理工具,所有继承和依赖的关系都是jar包与jar包之间的关系。
比如一个项目的包结构
com.jthinking.pojo
com.jthinking.dao
com.jthinking.service
com.jthinking.util
com.jthinking.web
如果我们用普通的Maven工程构建我们的web应用。Maven会将所有的class文件按照包结构放到WEB-INF/classes目录下。
但是如果我们按照这种分层的方式构建Maven工程,则Maven会把我们的class文件安包名打成几个jar文件,放到web工程的WEB-INF/lib目录下。

什么是依赖:
依赖就是我们在Maven工程的pom.xml中增加一个dependency标签,
Maven就会自动导入标签所指定的jar包,以及该jar包所需的关联jar包。
什么是继承:
继承的意思是,继承另一个工程,Maven会自动导入那个工程的pom.xml中所有的jar包,以及这些jar包所需的关联jar包。
不应该在这个工程中编码,它只起一个组织jar包的作用。注意此工程的pom.xml的Packaging标签值必须是pom。

继承主要就是为了解决重复编写配置文件的问题,以及大型项目的各个模块之间jar包冲突的问题。

在本项目中,上面一共新建jthinking-parent,jthinking-common,jthinking-manage,jthinking-pojo,jthinking-mapper,jthinking-service,jthinking-web一共七个工程。
jthinking-parent工程包含了项目的各个子项目所需的公共的核心jar包,这些jar包一般是各种第三方的jar包。
jthinking-common工程包含了项目的各个子项目所需的各种工具类,这些工具类一般是由公司自己编写,将来项目发布时,
这个工程会被Maven打成一个名为jthinking-common.jar的文件,放到jthinking-web工程的WEB-INF/lib下。。
jthinking-manage工程是一个聚合工程,它包含了jthinking-pojo,jthinking-mapper,jthinking-service,jthinking-web四个模块工程,其中jthinking-web是主导,
未来项目发布时,Maven会把jthinking-pojo,jthinking-mapper,jthinking-service三个工程分别打成三个jar包,放到jthinking-web工程的WEB-INF/lib下。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: