您的位置:首页 > 其它

Maven构建多模块项目

2016-11-23 11:43 387 查看
摘要:本文要用Maven来构建一个多模块的web项目   出处http://blog.csdn.net/evankaka

项目结构如下:

  system-parent

        |----pom.xml

        |----system-domain

                |----pom.xml

        |----system-dao

                |----pom.xml

        |----system-service

                |----pom.xml

        |----system-web

                |----pom.xml




一、创建system-parent项目

  创建system-parent,用来给各个子模块继承



勾选create a simple...



注意要选pom



创建好的结构如下,把src文件删除掉




二、创建sytem-domain模块

项目右键-》new->other



注意选择maven module





packageing,选择jar,因为这是要打包成jar给别的模块用的



子模块添加好后如下



打开system-domain项目pom.xml文件,改成如下

[html] view
plain copy

<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/xsd/maven-4.0.0.xsd">  

  <modelVersion>4.0.0</modelVersion>  

    <parent>  

        <groupId>com.mucfc</groupId>  

        <artifactId>system-parent</artifactId>  

        <version>0.0.1-SNAPSHOT</version>  

    </parent>  

      

    <artifactId>system-domain</artifactId>  

    <packaging>jar</packaging>  

      

    <name>system-domain</name>  

    <url>http://maven.apache.org</url>  

</project>  

此时system-parent应该有这么一句

[html] view
plain copy

<modules>  

    <module>system-domain</module>  

</modules>  

表明子模块添加成功


三、[b]创建system-dao模块[/b]

步骤和2一样,命名不同



然后把再打开system-dao的项目下的pom文件,修改成如下:

[html] view
plain copy

<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/xsd/maven-4.0.0.xsd">  

  <modelVersion>4.0.0</modelVersion>  

    <parent>  

        <groupId>com.mucfc</groupId>  

        <artifactId>system-parent</artifactId>  

        <version>0.0.1-SNAPSHOT</version>  

    </parent>  

    <artifactId>system-dao</artifactId>  

    <packaging>jar</packaging>  

  

    <name>system-dao</name>  

    <url>http://maven.apache.org</url>  

    <dependencies>  

    <!--system-dao需要使用到system-domain中的类,所以需要添加对system-domain模块的依赖-->  

    <dependency>  

        <groupId>com.mucfc</groupId>  

        <artifactId>system-domain</artifactId>  

         <version>${project.version}</version>  

    </dependency>  

 </dependencies>  

</project>  


四、创建system-service模块

步骤和2一样,命名不同



然后把再打开system-service的项目下的pom文件,修改成如下:

[html] view
plain copy

<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/xsd/maven-4.0.0.xsd">  

  <modelVersion>4.0.0</modelVersion>  

    <parent>  

        <groupId>com.mucfc</groupId>  

        <artifactId>system-parent</artifactId>  

        <version>0.0.1-SNAPSHOT</version>  

    </parent>  

      

    <artifactId>system-service</artifactId>  

    <packaging>jar</packaging>  

  

    <name>system-service</name>  

    <url>http://maven.apache.org</url>  

  

    <dependencies>  

        <!--system-service依赖system-dao和system-domain但是我们只需添加system-dao的依赖即可,因为system-dao已经依赖了system-domain -->  

        <dependency>  

            <groupId>com.mucfc</groupId>  

            <artifactId>system-dao</artifactId>  

            <version>${project.version}</version>  

        </dependency>  

    </dependencies>  

</project>  


五、创建system-web模块

web项目要打包成war文件,所以有个地方要改下



这里记得要选war文件



把pom文件改成如下:

[html] view
plain copy

<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/xsd/maven-4.0.0.xsd">  

  <modelVersion>4.0.0</modelVersion>  

    <parent>  

        <groupId>com.mucfc</groupId>  

        <artifactId>system-parent</artifactId>  

        <version>0.0.1-SNAPSHOT</version>  

    </parent>  

      

    <artifactId>system-web</artifactId>  

    <packaging>war</packaging>  

  

    <name>system-web</name>  

    <url>http://maven.apache.org</url>  

  

    <dependencies>  

        <dependency>  

            <groupId>com.mucfc</groupId>  

            <artifactId>system-service</artifactId>  

            <version>${project.version}</version>  

        </dependency>  

    </dependencies>  

</project>  

然后自己在在\system-web\src\main\webapp目录中添加一个index.jsp

六、整体目录如下

                                                                                      



六、编译运行项目

  经过上面的五个步骤,相关的模块全部创建完成,怎么运行起来呢

Maven Tomcat7自动部署:

1、配置tomcat-users.xml文件

在tomcat安装目录下找到tomcat-users.xml文件。该文件路径为【tomcat安装根目录】/conf/

修改文件内容,增加下列内容:

[html] view
plain copy

<tomcat-users>  

<role rolename="manager"/>  

<role rolename="admin"/>  

<role rolename="manager-gui"/>  

 <role rolename="manager-script"/>  

<user username = "admin" password = "password" roles = "admin,manager,manager-gui,manager-script" />  

</tomcat-users>  

启动tomcat7,然后访问 http://localhost:8080/manager/html,输入admin/password,如果出现以下界面,表示tomcat一切OK

如果是Tomcat6 http://localhost:8080/manager



2、配置maven 的setting.xml 文件

在Maven的conf目录中的setting.xml servers节点增加

[html] view
plain copy

<server>  

 <id>tomcat7</id>  

 <username>admin</username>  

 <password>password</password>  

</server>  

3、配置项目pom.xml文件

[html] view
plain copy

<plugin>  

    <groupId>org.codehaus.mojo</groupId>  

    <artifactId>tomcat-maven-plugin</artifactId>  

    <version>1.1</version>  

    <configuration>  

        <url>http://localhost:8080/manager/text</url>  

        <server>tomcat7</server>  

        <username>admin</username>  

        <password>password</password>  

        <ignorePackaging>true</ignorePackaging>    

    </configuration>  

</plugin>  

注:此处的url 注意是xxx/manager/text 并非是 xxx/manager/html 原因是我用的tomcat 是tomcat7 的版本
4、cmd运行

先进入到项目所在的目录,然后运行

[html] view
plain copy

mvn tomcat:redeploy  



最终结果:

其中只有system-web是web项目,其它都不是,只是一些依赖项目



在目录D:\Java\Tool\apache-tomcat-7.0.62\webapps可以找到发布好的文件



浏览器输入:http://localhost:8080/system-web/



常见错误排除:

1.Connection refused错误

报错信息如下:

[html] view
plain copy

[ERROR]Failed to execute goal org.apache.tomcat.maven: tomcat7-maven-plugin: 2.0- SNAPSHOT: deploy (default-cli) on project helloworld: Cannot invoke Tomcat manager: Connection refused: connect -> [Help 1]  

原因:未启动Tomcat服务器

解决办法:先启动Tomcat服务器再选择Run

2. 401错误

报错信息如下:

[html] view
plain copy

[ERROR] Failed to execute goal org.apache.tomcat.maven:tomcat7-maven-plugin: 2.0-SNAPSHOT:deploy (default-cli) on project helloworld: Cannot invoke Tomcat manager: Server returned HTTP response code: 401 for URL: http://localhost:8080/manager/text/deploy?path=%2Fhelloworld -> [Help 1]  

原因:权限问题

解决办法在$CATALINA_BASE/conf/tomcat-users.xml,

如D:\apache-tomcat-7.0.34\conf\tomcat-users.xml文件中添加权限

[html] view
plain copy

<role rolename=”manager”/>  

<user username=”admin” password=”admin” roles=”manager”/>  

修改pom.xml文件,在<configuration>  

</configuration>中添加

[html] view
plain copy

<username>admin</username>    

<password>admin</password>  

3.403错误

报错信息如下:

[html] view
plain copy

[ERROR] Failed to execute goal org.apache.tomcat.maven:tomcat7-maven-plugin: 2.0-SNAPSHOT:deploy (default-cli) on project helloworld: Cannot invoke Tomcat manager: Server returned HTTP response code: 403 for URL: http://localhost:8080/manager/html/deploy?path=%2Fhelloworld -> [Help 1]  

原因:产生该问题有可能因为两个原因,具体参见解决办法

解决办法:

1)如果使用的是Tomcat 7,需要修改pom.xml中部署的url地址,将

<url>http://localhost:8080/manager</url>



<url>http://localhost:8080/manager/text</url>

在这次部署的问题上,我就是遇到了url 的问题,我用的是tomcat7

2)给tomcat用户权限分配上,需要同时具备manager-gui和manager-script权限

正确的conf/tomcat-users.xml配置应为:

[html] view
plain copy

<tomcat-users>  

<role rolename="manager-gui"/>  

<role rolename="manager-script"/>  

<user username="admin” password="admin" roles="manager-gui, manager-script"/>  

</tomcat-users>  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: