maven学习二之多模块构建

来源:互联网 发布:win10宽带拨号软件 编辑:程序博客网 时间:2024/06/05 13:22

一.Maven简单多模块构建基础

1)父pom.xml配置与说明

<packaging>pom</packaging>  

父pom.xml的packing的配置打包方式不是jar、war而是pom,这样的配置如果meaven执行打包命令的时候去找子项目模块的pom.xml去打包并构建到当前项目。

2.父模块配置子项目模:     

 <modules>                 <module>simple1</module>         <module>simple2</module> </modules>.

2)子pom.xml的配置与说明

1.子模块引入父pom:

<parent>     <groupId>parent.groupId</groupId>     <artifactId>simple-parent</artifactId>     <version>1.0</version></parent>
2.子模块只需要定义:

<artifactId>simple-children</artifactId><packaging>war</packaging>

3.在web.xml中把他们绑定在一起(即进行分发配置)。
从simple-parent项目运行mvn clean install命令进行打包,maven会自动处理正确的进行打包。

打包完成后:子项目用 <span style="font-family: Arial, Helvetica, sans-serif;">Jetty插件的执行Run也可以单独访问。</span>

maven-compiler-plugin插件用来指定命令行运行的jdk版本配置

1)企业项目第一要构建的就是对象模型,在maven中将对象模型分割成单独的项目被广泛引用。


4.优化pom.xml依赖

1)把多个模块共有的依赖上移到父pom用例如:。

<dependencyManagement>    <dependency><span style="white-space:pre"></span><groupId>org.hibernate</groupId><span style="white-space:pre"></span><artifactId>hibernate-annotations</artifactId><span style="white-space:pre"></span><version>3.3.0.ga</version>    </dependency>
<pre name="code" class="html"></dependencyManagement>

由于父POM包含一个版本和一组排除配置,所有的子POM需要使

用groupId和artifactId引用这个依赖

<dependency>   <groupId>org.hibernate</groupId>   <artifactId>hibernate</artifactId></dependency>


如果有多个值可以定义变量例如

<properties>     <hibernate.annotations.version>3.3.0.ga</hibernate.annotations.version></properties>

2)兄弟依赖的优化

1.一种方案是像其它依赖一样将它们移到父项目的dependencyManagement中,在最顶层的父项目中定义所有兄弟项目的版本。这样做当然是可以的,

但我们也可以使用内建属性org.sonatype.mavenbook和0.6-SNAPSHOT来解决这个版本问题,由于我们都共享一个共同的组,因此,通过使用内置的org.sonatype.mavenbook属性引用当前POM的组,我们能够提前保证这些声明是正确的:

<dependency>   <groupId>org.sonatype.mavenbook</groupId>   <artifactId>simple-weather</artifactId>   <version>0.6-SNAPSHOT</version></dependency><dependency>   <groupId>org.sonatype.mavenbook</groupId>   <artifactId>simple-persist</artifactId>   <version>0.6-SNAPSHOT</version></dependency>

3)插件依赖优化跟普通依赖优化类似只是配置在父pom.xml,例如:
<span style="font-family: Arial, Helvetica, sans-serif;"><pluginManagement></span>

<plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><configuration><source>1.5</source><target>1.5</target></configuration></plugin><plugin><groupId>org.codehaus.mojo</groupId><artifactId>hibernate3-maven-plugin</artifactId>
<pre name="code" class="html"><version>2.1</version><configuration><components><component><name>hbm2ddl</name><implementation>annotationconfiguration</implementation></component></components></configuration><dependencies><dependency><groupId>hsqldb</groupId><artifactId>hsqldb</artifactId><version>${hsqldb.version}</version></dependency></dependencies></plugin></plugins></pluginManagement>


4)使用插件优化

1.使用Maven Dependency插件进行优化

2)mvn dependency:analyze Maven Dependency插件能够帮助你发现对于依赖的直接引用

3)mvn dependency:tree 该目标会列出项目中所有的直接和传递性依赖









0 0
原创粉丝点击