maven创建父子类项目

1、 创建 聚合 模块
 
1)创建父模块,点击右键,选择NEW -->project-->maven-->maven Project。父类pom修改: <!--指明打包类型-->
<packaging>pom</packaging>  
 
<!--引用子类  -->
<modules>  
    <module>../ace-api</module>  
    <module>../ace-core</module>  
    <module>../ace-entity</module>
    <module>../ace-service</module>
  </modules>
 
注意:需要删除src等多余文件夹,因为聚合模块仅仅是帮助聚合其他模块构建的工具,其本身并没有实质的内容
 

2)将父模块项目中的src文件删除(可有可无,主要看个人需要)
 选中项目父模块,点击右键,选择NEW -->project-->maven-->maven Module,点击下一步,在出现的界面中输入子模块的名称,点击下一步,出现Select an Archetype界面。这时选择maven-Archetype-site-quickStart或者maven-Archetype-webapp(构建web层时使用),然后选择完成,即生成子项目。

 随后按照提示创建Maven聚合模块

 
 
 
2、创建模块(Java工程)
输入Module Name(不需要选中Create a simple project(skip archetype seletion)),点击下一步,其后创建方法与创建聚合模块相同。
Catalog选择Internal,Filter输入web,选择maven-archetype-webapp,创建Maven Web项目,随后步骤同上。
 
3.修改配置文件
 
 
子类pom修改:
<packaging>jar</packaging>
 
<parent>   
<groupId>cn.ace.parent</groupId>
<artifactId>ace-parent</artifactId>
<version>1.0</version>   
<relativePath>../ace-parent/pom.xml</relativePath>   
  </parent> 
 
<dependencies>
     
     <dependency>   
    <groupId>cn.ace.parent</groupId>
<artifactId>ace-parent</artifactId>
    <version>1.0</version>   
    </dependency> 
    
  
  
  </dependencies>
 
修改parent目录中的pom.xml文件,把 <groupId>cn.ace.servie </groupId><version>1.0-SNAPSHOT</version>去掉,加上 <packaging>jar</packaging>,因为groupId和version会继承system-parent中的groupId和version,packaging设置打包方式为jar
 

注意的是<relativePath>标签,如果pom的层次关系就像本例中的那样只隔一层,则可以省略这个。maven同样可以找到子pom。

子pom中引入<parent>标签后,就会从父pom继承<version>等属性了

 

4、除了jar包依赖,插件也可以通过这样的方式进行管理

<!-- parent -->
<build>
   <pluginManagement>
      <plugins>
          <plugin>
               <groupId>org.apache.maven.plugins</groupId>
               <artifactId>maven-source-plugin</artifactId>
               <version>2.1.1</version>
          </plugin>
      </plugins>
   </pluginManagement>
</build>

 

 

<!-- childA -->
<build>   
   <plugins>
      <plugin>
           <groupId>org.apache.maven.plugins</groupId>
           <artifactId>maven-source-plugin</artifactId>
      </plugin>
   </plugins>
</build>

 

如果子pom间存在引用关系,比如childA引用到了childB的jar包
<dependency>
   <groupId>com.module</groupId>
   <artifactId>childA</artifactId>       <!--加上childA的依赖-->
   <version>1.0.0</version>
</dependency>
 
 
5.所有maven的引用放在parent中,子类继承父类。
 
 
 
6.问题解析:
1.如果项目名报错,则,报错的父子类全选右键选择maven-update project,就ok。
 

2.错误描述:

SEVERE: Error configuring application listener of class org.springframework.web.util.Log4jConfigListener
java.lang.ClassNotFoundException: org.springframework.web.util.Log4jConfigListener

问题解析:

Maven项目中所有依赖(jdk/jar/classes)关系都被其管理。所以如果确定项目中确实存在该包或文件(org.springframework.web.util.Log4jConfigListener),那必定是项目没有添加maven依赖所致。

解决方案:

 

ctrl+Enter 报错项目->Deployment Assembly->Add->Java buid path entries->Next->Maven Dependencies

Clear!

此时Servers模块中的相关项目模块下多了个字节点spring-web-3.2.3.RELEASE.jar(web.xml中配置的Log4jConfigListener类所在的包)

 
 
 
 
 
 
 
 
 
 
 

猜你喜欢

转载自starbhhc.iteye.com/blog/2320270
今日推荐