Maven(二) 构建聚合项目,父子项目

来源:互联网 发布:招网络分销方案 编辑:程序博客网 时间:2024/06/16 12:04

开发环境

jdk8,myEclipse2017 ci

新建项目

NEW–>Other…–>Maven Project

这里写图片描述
然后Next
这里写图片描述

这里写图片描述

修改pom.xml,在<dependencies>中加入jar依赖,没有的可去网上查(https://mvnrepository.com/),在<build> <plugins>加入第三方控件

<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>  <groupId>com.myMavenTest</groupId>  <artifactId>M_parent</artifactId>  <version>0.0.1-SNAPSHOT</version>  <packaging>pom</packaging>  <dependencies>      <dependency>        <groupId>org.apache.poi</groupId>        <artifactId>poi</artifactId>        <version>3.15</version>        </dependency>        <dependency>        <groupId>net.sf.ehcache</groupId>        <artifactId>ehcache-core</artifactId>        <version>2.6.6</version>        </dependency>        <dependency>            <groupId>org.codehaus.plexus</groupId>            <artifactId>plexus-compiler-javac</artifactId>            <version>1.8.1</version>        </dependency>        <dependency>            <groupId>org.mybatis</groupId>            <artifactId>mybatis-ehcache</artifactId>            <version>1.0.0</version>        </dependency>        <dependency>            <groupId>org.slf4j</groupId>            <artifactId>slf4j-api</artifactId>            <version>1.7.25</version>        </dependency>        <dependency>            <groupId>com.github.pagehelper</groupId>            <artifactId>pagehelper</artifactId>            <version>5.0.4</version>        </dependency>          <dependency>            <groupId>org.apache.poi</groupId>            <artifactId>poi-ooxml</artifactId>            <version>3.15</version>        </dependency>          <dependency>            <groupId>org.apache.directory.studio</groupId>            <artifactId>org.apache.commons.codec</artifactId>            <version>1.8</version>        </dependency>        <dependency>            <groupId>org.aspectj</groupId>            <artifactId>aspectjweaver</artifactId>            <version>1.7.2</version>        </dependency>        <dependency>          <groupId>com.github.cafapi.decoder</groupId>          <artifactId>decoder-js</artifactId>          <version>1.10.0-205</version>        </dependency>        <dependency>          <groupId>javax</groupId>          <artifactId>javaee-api</artifactId>          <version>7.0</version>          <scope>provided</scope>        </dependency>        <!-- Apache工具组件 -->        <dependency>            <groupId>commons-lang</groupId>            <artifactId>commons-lang</artifactId>            <version>2.6</version>        </dependency>        <dependency>            <groupId>commons-io</groupId>            <artifactId>commons-io</artifactId>            <version>2.4</version>        </dependency>         <!-- json 处理的jar包 -->        <dependency>            <groupId>org.codehaus.jackson</groupId>            <artifactId>jackson-mapper-asl</artifactId>            <version>1.9.13</version>        </dependency>        <!-- httpclient -->        <dependency>         <groupId>org.apache.httpcomponents</groupId>         <artifactId>httpclient</artifactId>         <version>4.3.1</version>        </dependency>        <!-- 单元测试 -->        <dependency>         <groupId>junit</groupId>         <artifactId>junit</artifactId>         <version>4.10</version>        </dependency>         <!-- 日志处理 -->        <dependency>         <groupId>log4j</groupId>         <artifactId>log4j</artifactId>         <version>1.2.16</version>        </dependency>        <!-- Mybatis -->         <dependency>         <groupId>org.mybatis</groupId>         <artifactId>mybatis-spring</artifactId>         <version>1.2.2</version>         </dependency>         <dependency>         <groupId>org.mybatis</groupId>         <artifactId>mybatis</artifactId>         <version>3.2.7</version>         </dependency>         <!-- MySql -->          <dependency>              <groupId>mysql</groupId>              <artifactId>mysql-connector-java</artifactId>              <version>5.1.32</version>          </dependency>          <dependency>              <groupId>commons-dbcp</groupId>              <artifactId>commons-dbcp</artifactId>              <version>1.4</version>          </dependency>          <dependency>              <groupId>commons-pool</groupId>              <artifactId>commons-pool</artifactId>              <version>1.5.6</version>          </dependency>          <dependency>              <groupId>commons-codec</groupId>               <artifactId>commons-codec</artifactId>               <version>20041127.091804</version>           </dependency>            <!-- 连接池 -->           <dependency>                 <groupId>com.alibaba</groupId>                 <artifactId>druid</artifactId>                 <version>1.0.9</version>           </dependency>           <!-- spring -->           <dependency>                <groupId>org.springframework</groupId>                <artifactId>spring-context</artifactId>                <version>4.1.0.RELEASE</version>            </dependency>            <dependency>                <groupId>org.springframework</groupId>                <artifactId>spring-beans</artifactId>                <version>4.1.0.RELEASE</version>            </dependency>            <dependency>                <groupId>org.springframework</groupId>                <artifactId>spring-webmvc</artifactId>                <version>4.0.5.RELEASE</version>             </dependency>             <dependency>                <groupId>org.springframework</groupId>                <artifactId>spring-jdbc</artifactId>                <version>4.1.0.RELEASE</version>             </dependency>             <dependency>                <groupId>org.springframework</groupId>                <artifactId>spring-aspects</artifactId>                <version>4.0.7.RELEASE</version>            </dependency>            <dependency>                <groupId>org.springframework</groupId>                <artifactId>spring-core</artifactId>                <version>4.1.3.RELEASE</version>            </dependency>            <!-- JSP相关 -->            <dependency>                <groupId>jstl</groupId>                <artifactId>jstl</artifactId>                <version>1.2</version>            </dependency>            <dependency>                <groupId>javax.servlet</groupId>                <artifactId>servlet-api</artifactId>                <version>2.5</version>                <scope>provided</scope>            </dependency>            <dependency>                <groupId>org.mortbay.jetty</groupId>                <artifactId>jsp-api-2.1</artifactId>                <version>6.1.10</version>                <scope>provided</scope>            </dependency>            <!-- 文件上传 -->            <dependency>                <groupId>commons-fileupload</groupId>                <artifactId>commons-fileupload</artifactId>                <version>1.3</version>            </dependency>        </dependencies>        <build>            <plugins>                <plugin>                    <groupId>org.apache.maven.plugins</groupId>                    <artifactId>maven-compiler-plugin</artifactId>                    <version>3.3</version>                    <configuration>                        <source>1.8</source>                        <target>1.8</target>                        <encoding>UTF-8</encoding>                    </configuration>                </plugin>                <plugin>                    <groupId>org.apache.tomcat.maven</groupId>                    <artifactId>tomcat7-maven-plugin</artifactId>                    <version>2.2</version>                </plugin>                <plugin>                    <artifactId>maven-war-plugin</artifactId>                    <configuration>                        <version>3.0</version>                        <failOnMissingWebXml>false</failOnMissingWebXml>                    </configuration>                </plugin>            </plugins>        </build></project>

建接口项目

这里写图片描述
这里写图片描述这里写图片描述

修改pom.xml

<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>  <groupId>com.myMavenTest</groupId>  <artifactId>M_service</artifactId>  <version>0.0.1-SNAPSHOT</version>  <parent>  <groupId>com.myMavenTest</groupId>  <artifactId>M_parent</artifactId>  <version>0.0.1-SNAPSHOT</version>  </parent>  <build/></project>

<parent>标签是引入父项目的,<groupId><artifactId> <version>都是从父项目的pom.xml里复制过来的。
右击项目,Maven->Update Project….

下面讲怎么新建java Web项目

重复new–》Maven Project
这里写图片描述

这里写图片描述
关于j2SE-1.5这块上面忘了说了,在pom.xml里加入

<build>    <plugins>         <plugin>             <groupId>org.apache.maven.plugins</groupId>             <artifactId>maven-compiler-plugin</artifactId>             <version>3.3</version>             <configuration>                 <source>1.8</source>                 <target>1.8</target>                 <encoding>UTF-8</encoding>             </configuration>         </plugin>     </plugins> </build>

`

就回变成javaSE-1.8,(我在M_parent的配置文件里写的,所以子项目里不写,继承了父项目,一样);

<plugin>    <artifactId>maven-war-plugin</artifactId>    <configuration>        <version>3.0</version>        <failOnMissingWebXml>false</failOnMissingWebXml>    </configuration></plugin>

这里写图片描述

加入配置文件,补齐项目结构
这里写图片描述

把M_web和M_service聚合成一个大的项目
新建一个web工程
这里写图片描述

修改配置文件

<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>  <groupId>com.myMavenTest</groupId>  <artifactId>M_mytest</artifactId>  <version>0.0.1-SNAPSHOT</version>  <packaging>war</packaging>  <!-- 添加依赖 -->  <dependencies>  <!-- 必须和分项目里的一样 -->  <dependency>   <groupId>com.myMavenTest</groupId>  <artifactId>M_service</artifactId>  <version>0.0.1-SNAPSHOT</version>  <type>jar</type>  </dependency>  <dependency>  <groupId>com.myMavenTest</groupId>  <artifactId>M_web</artifactId>  <version>0.0.1-SNAPSHOT</version>  <type>war</type>  </dependency>  <!-- end -->  <dependency>      <groupId>javax.servlet</groupId>      <artifactId>javax.servlet-api</artifactId>      <version>3.0.1</version>      <scope>provided</scope>  </dependency>  <dependency>      <groupId>javax.servlet.jsp</groupId>      <artifactId>jsp-api</artifactId>      <version>2.2</version>      <scope>provided</scope>  </dependency>  </dependencies>   <build>  <plugins>  <plugin>  <groupId>org.apache.maven.plugins</groupId>    <artifactId>maven-war-plugin</artifactId>    <version>2.1.1</version>    <configuration>    <failOnMissingWebXml>false</failOnMissingWebXml>      <packagingExcludes>WEB-INF/web.xml</packagingExcludes>    <overlays>    <overlay>    <groupId>com.myMavenTest</groupId>    <artifactId>M_web</artifactId>    </overlay>    </overlays>    </configuration>  </plugin>   <plugin>       <groupId>org.apache.maven.plugins</groupId>       <artifactId>maven-compiler-plugin</artifactId>       <version>3.3</version>       <configuration>           <source>1.8</source>           <target>1.8</target>           <encoding>UTF-8</encoding>       </configuration>   </plugin>  </plugins>  </build></project>

然后分别选M_parent,M_service,M_web然后右击,Run As—->Maven install,一定要先是M_parent,应为另外两个项目里引入了该项目的jar包,如果先执行其他项目,就会报错,说找不到M_parent,三个项目install之后就可以在本地仓库里看到这三个jar的依赖了。
这里写图片描述

最后执行M_mytest Run As—》Maven install

在tomcat里执行M_mytest
这里写图片描述

运行成功
这里写图片描述

从tomcat里瞅一眼,完美
这里写图片描述