[技术] 用 Maven 搭建多项目 Project

2016-08-11  本文已影响0人  stevezyxie

在平时的Java web项目开发中为了便于后期的维护,我们一般会进行分模块开发,每个模块都对应着一个pom.xml。它们之间通过继承和聚合相互关联。这样各层之间的职责会比较明确,也方便后期的维护。

今天我们是使用 Maven 来构建以上的各个层。

文字内容只涉及工程搭建的基本流程,请主要参照文章末尾的源代码。

项目目录:
mavenweb-parent
  |----pom.xml
mavenweb-service
  |----pom.xml
mavenweb-web
  |----pom.xml

创建主模块:mavenweb-parent

Mavenweb Parent New Proj.png Mavenweb Parent JDK Setting.png
<project ...>
    ...
    <packaging>pom</packaging>
    
    <properties>
        <springversion>3.1.1.RELEASE</springversion>
        <junitversion>4.10</junitversion>
    </properties>
    
    <dependencyManagement>
        <dependencies>
            <!-- junit -->
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>${junitversion}</version>
                <scope>test</scope>
            </dependency>
            ...         
    </dependencyManagement>
</project>

创建子模块:mavenweb-server

<project>
    ...
    <packaging>jar</packaging>
    
    <parent>
        ...
        <relativePath>../mavenweb-parent/pom.xml</relativePath>
    </parent>
    ...
</project>
<beans>
    <!-- 扫描包 -->
    <context:component-scan base-package="com.stevexie" />
    
    <bean id="propertyConfigurer" >
        <property name="location">
            <value>file:/.../configInfo.properties</value>
        </property>
    </bean>
    
    <!-- 这个包负责读取配置文件信息 -->
    <!-- 若使用注解初始化这个类,编译后的 jar 包无法正确地读取配置,原因未知 -->
    <!-- 所以仅这个类实例需要在 xml 中定义 -->
    <bean id="configInfo" class="com.stevexie.util.ConfigInfo">
        <property name="name" value="${prop.name}"></property>
    </bean>
</beans>

创建子模块:mavenweb-client

Mavenweb Client Proj Facets Setting.png

web.xml:

<web-app >
...
<servlet-mapping>
    <servlet-name>appServlet</servlet-name>
    <!-- 使用 resful 风格的 url 前 -->
    <!-- <url-pattern>/</url-pattern> -->
    <!-- 使用 resful 风格的 url 后 -->
    <url-pattern>/login</url-pattern>
</servlet-mapping>
...
</web-app>

client-spring-mvc.xml:

<beans ">
    ...
    <mvc:default-servlet-handler />
    <!-- Enables the Spring MVC @Controller programming model -->
    <mvc:annotation-driven />
    ...
</beans>

项目 mavenweb-client 中的表单验证:

<!-- spring mvc validator -->
    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-validator</artifactId>
      <version>4.3.1.Final</version>
   </dependency>
@NotEmpty(message = "用户名不能为空")
private String name;
    
@Size(min=6 ,max= 20 ,message = "密码长度不符合标准")
private String pwd;
public class UserController {
    @RequestMapping(value="/login", method = {RequestMethod.GET})
    public String test(Model model) {
        if(!model.containsAttribute("userVo")){
            model.addAttribute("userVo", new UserVo());
        }
        return "login";
    }
    
    @RequestMapping(value="/login", method = {RequestMethod.POST})
    public ModelAndView test(ModelMap model, @Valid @ModelAttribute("userVo") UserVo userVo, BindingResult result) 
            throws NoSuchAlgorithmException {
        MsgVo msgVo = new MsgVo();
        msgVo.setMsg("Hi " + userVo.getName() + ", " + helloService.sayHello());
        
        if (result.hasErrors()) {
            return new ModelAndView("/login", null);
        }
        model.addAttribute("msgVo", msgVo);
        
        return new ModelAndView("/login_success", model);
    }
} 

再次配置 mavenweb-parent 项目

在 pom 文件中,添加两个子模块:

<modules>
    <module>../mavenweb-client</module>
    <module>../mavenweb-server</module>
</modules>

至此,工程基本设置完毕了。

编译

使用命令行:

cd ../mavenweb-proj/
mvn clean install -DskipTests=true

运行

可以使用命令把 war 包发布到 tomcat 中,并启动 tomcat 服务;或直接在 Eclipse 中运行。
为节省篇幅,这里只介绍后者;命令行的启动则会放到下一篇博文中讨论,

在 Eclipse 中运行:

Login.png Login Validation.png Login Success.png

完整源码下载:

请移步到百度网盘下载源码,并导入到 Eclipse 中。下载地址:
https://pan.baidu.com/s/1pLo0Ynx

下一篇预告

在本文基础上,添加并完善一些 web 项目的基本功能,包括:

P.S. 如有介绍不准确之处或者在运行源代码遇到问题,请在请留下您评论留言。您的鼓励永远是我前进的动力。

参考资料

上一篇 下一篇

猜你喜欢

热点阅读