maven 总结

来源:互联网 发布:峨眉山旅游攻略 知乎 编辑:程序博客网 时间:2024/06/15 09:53

1、父子模块

     maven的父子模块定义只需要在子模块定义父模块坐标,

<parent>    <artifactId>ec</artifactId>    <groupId>com.ec</groupId>    <version>0.0.1-SNAPSHOT</version></parent>

定义了父模块之后,子模块会继承父模块的groupId和version标签,所以子模块这两个标签可以不必填;

可被继承的标签有:

groupId:项目组ID,项目坐标的核心元素

version: 项目版本, 项目坐标的核心元素

description: 项目的描述信息

organization: 项目的组织信息

inceptionYear: 项目的创始年份

url: 项目的URL地址

developers: 项目开发者信息

contributors: 项目的贡献者信息

distributionManagement: 项目的部署配置

issueManagement: 项目的缺陷跟踪系统信息

ciManagement: 项目的持续集成系统信息

scm: 项目的版本控制系统信息

mailingLists: 项目的邮件列表信息

properties: 自定义的maven属性

dependencies: 项目的依赖配置

dependencyManagement: 项目的依赖管理配置

repositories: 项目的仓库配置

build: 包括项目的源码目录配置、输出目录配置、插件配置、插件管理配置等

reporting: 包括项目的报告输出目录配置、报告插件配置等


2、通常项目定义父子关系后都定义聚合关系

<!--子模块--><modules>    <module>common</module>    <module>common_service_utils</module>    <module>common_web_utils</module>    <module>common_config_utils</module>
</modules>3、一般定义了父子模块的项目,父模块的打包方式都是pom,子模块不限制
4、dependencyManagement标签父模块定义,为了解决依赖架包的版本问题,我们可以在父模块对依赖进行管理,定义后的依赖包只有在子模块需要的时候,只需要指定groupId和artifactId即可,无特殊要求不需要在单独指定版本号。
5、profiles标签

我们可以定义profiles,来进行系统多资源环境动态切换效果,一般父模块定义后,子模块会被继承不需要单独再定义。

<profiles>    <!-- 多环境移植配置 start -->    <!--默认:开发环境配置-->    <profile>        <id>dev</id>        <properties>            <profiles.active>dev</profiles.active>        </properties>        <activation>            <activeByDefault>true</activeByDefault>        </activation>    </profile>    <!--测试环境配置-->    <profile>        <id>test</id>        <properties>            <profiles.active>test</profiles.active>        </properties>    </profile>    <!--产品配置-->    <profile>        <id>product</id>        <properties>            <profiles.active>product</profiles.active>        </properties>    </profile>    <!-- 多环境移植配置 end --></profiles>

6、build 标签

6.1testResources、resources标签可以指定系统的测试资源和资源文件的位置,也可以将源路径文件移动到目标路径(通过targetPath标签实现),maven打包默认读取的是resources标签下的资源文件

6.2plugins标签

maven的生命周期是通过配置插件的形式进行相应的处理的。父模块插件的配置实例如下:

<!--编译插件--><plugins>    <plugin>        <artifactId>maven-compiler-plugin</artifactId>        <version>3.5.1</version>        <configuration>            <source>${jdk.version}</source>            <target>${jdk.version}</target>            <optimize>true</optimize>            <debug>true</debug>            <encoding>${project.build.sourceEncoding}</encoding>        </configuration>    </plugin>    <!-- Jar打包插件,配置manifest文件,加入lib包的jar依赖 -->    <plugin>        <groupId>org.apache.maven.plugins</groupId>        <artifactId>maven-jar-plugin</artifactId>        <version>2.6</version>        <configuration>            <archive>                <addMavenDescriptor>false</addMavenDescriptor>                <index>false</index>                <manifest>                    <addDefaultImplementationEntries>false                    </addDefaultImplementationEntries>                </manifest>            </archive>        </configuration>    </plugin>    <!-- war 打包插件, 设定war包名称不带版本号 -->    <plugin>        <groupId>org.apache.maven.plugins</groupId>        <artifactId>maven-war-plugin</artifactId>        <version>2.6</version>        <configuration>            <archive>                <addMavenDescriptor>true</addMavenDescriptor>                <index>true</index>                <manifest>                    <addDefaultImplementationEntries>true                    </addDefaultImplementationEntries>                </manifest>            </archive>            <packagingExcludes>            </packagingExcludes>            <warSourceExcludes>            </warSourceExcludes>            <webappDirectory>${project.build.directory}/${project.artifactId}            </webappDirectory>        </configuration>    </plugin>    <!--Junit测试插件-->    <plugin>        <groupId>org.apache.maven.plugins</groupId>        <artifactId>maven-surefire-plugin</artifactId>        <version>2.19.1</version>        <configuration>            <!-- <argLine>-Xmx1024m -XX:PermSize=256m -XX:MaxPermSize=256m</argLine>-->            <includes>                <include>**/*Test.java</include>            </includes>            <excludes>                <exclude>**/TestConstants.java</exclude>            </excludes>            <forkMode>once</forkMode>            <skipTests>false</skipTests>        </configuration>    </plugin>    <!--源码插件-->    <plugin>        <groupId>org.apache.maven.plugins</groupId>        <artifactId>maven-source-plugin</artifactId>        <version>3.0.0</version>        <configuration>            <attach>true</attach>        </configuration>    </plugin>    <!-- resource插件 -->    <plugin>        <groupId>org.apache.maven.plugins</groupId>        <artifactId>maven-resources-plugin</artifactId>        <version>2.7</version>        <configuration>            <encoding>${project.build.sourceEncoding}</encoding>        </configuration>    </plugin>    <!-- dependency插件 -->    <plugin>        <groupId>org.apache.maven.plugins</groupId>        <artifactId>maven-dependency-plugin</artifactId>        <version>2.10</version>    </plugin>    <!-- install插件 -->    <plugin>        <groupId>org.apache.maven.plugins</groupId>        <artifactId>maven-install-plugin</artifactId>        <version>2.5.2</version>    </plugin>    <!-- clean插件 -->    <plugin>        <groupId>org.apache.maven.plugins</groupId>        <artifactId>maven-clean-plugin</artifactId>        <version>3.0.0</version>    </plugin>    <!-- ant插件 -->    <plugin>        <groupId>org.apache.maven.plugins</groupId>        <artifactId>maven-antrun-plugin</artifactId>        <version>1.8</version>    </plugin>    <!--assembly 打包插件-->    <plugin>        <groupId>org.apache.maven.plugins</groupId>        <artifactId>maven-assembly-plugin</artifactId>        <version>2.2.1</version>        <configuration>            <descriptorRefs>                <descriptorRef>jar-with-dependencies</descriptorRef>            </descriptorRefs>        </configuration>        <executions>            <execution>                <id>make-assembly</id>                <phase>package</phase>                <goals>                    <goal>single</goal>                </goals>            </execution>        </executions>    </plugin></plugins>
子模块对于jar和war可进行相应的修改,实例如下:

jar

  <!-- 打包jar文件时,配置manifest文件,加入lib包的jar依赖 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>lib</classpathPrefix>
                            <!--打包时MANIFEST.MF文件不记录的时间戳版本-->
                            <useUniqueVersions>false</useUniqueVersions>
                            <mainClass>com.ec.DubboRun</mainClass>
                            <!--dubbo 自带的启动方法,可实现优雅关机-->
                            <!--<mainClass>com.alibaba.dubbo.container.Main</mainClass>-->
                        </manifest>
                    </archive>
                </configuration>
            </plugin>

war

 <!-- war 打包插件, 设定war包名称不带版本号 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.6</version>
                <configuration>
                    <packagingExcludes>
                        <!-- WEB-INF/classes/com/thinkgem/jeesite/** -->
                        WEB-INF/classes/org/apache/ibatis/**,
                        WEB-INF/classes/org/mybatis/spring/**
                    </packagingExcludes>
                    <warSourceExcludes>
                        static/bootstrap/2.3.1/docs/**,
                        static/ckeditor/_samples/**,
                        static/ckeditor/_source/**,
                        static/ckfinder/_samples/**,
                        static/ckfinder/help/**,
                        static/compressor*/**,
                        static/jquery-jbox/2.3/docs/**,
                        static/jquery-jbox/2.3/Skins2/**,
                        static/jquery-validation/1.11.0/demo/**,
                        static/jquery-ztree/3.5.12/demo/**,
                        static/My97DatePicker/docs/**,
                        static/supcan/doc/**,
                        static/SuperSlide/demo/**,
                        static/treeTable/demo/**<!-- , -->
                        <!-- userfiles/** --><!-- ,/**/*.jsp -->,
                        test/**
                    </warSourceExcludes>
                    <webappDirectory>${project.build.directory}/${project.artifactId}</webappDirectory><!--
                    <webXml>${project.basedir}/target/jspweb.xml</webXml> -->
                    <warName>pt</warName>
                </configuration>
            </plugin>

7、本地依赖项目,如果提示找不到包,原因是因为maven实际依赖都是对本地仓库进行依赖的,解决方式将本地被依赖的项目执行install命令,将被依赖包安装到本地仓库供其他项目依赖引用。
原创粉丝点击