pom.xml解析

pom.xml解析

这里对项目中的pom文件进行了分析,网上能找到很多详尽的maven介绍,这里只对自己感到疑惑的地方进行了整理。
项目由多个工程组成,其他工程依赖parent工程。这里以parent的pom.xml和employee-service-impl的pom.xml进行解析。

parent的pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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.myph</groupId>
    <artifactId>parent</artifactId>
    <packaging>pom</packaging>
    <version>1.0.0-SNAPSHOT</version>
    <name>${project.artifactId}</name>


    <profiles>
        <profile>
            <id>prd</id>
            <properties>
                <commonVersion>1.0.3-RELEASE</commonVersion>
                <userServiceVersion>1.0.1-RELEASE</userServiceVersion>
                <employeeServiceVersion>1.0.6-RELEASE</employeeServiceVersion>
                <junitServiceVersion>1.0.1-RELEASE</junitServiceVersion>
                <baseServiceVersion>1.0.1-RELEASE</baseServiceVersion>
                <memberServiceVersion>1.0.1-RELEASE</memberServiceVersion>
                <applyServiceVersion>1.1.7-RELEASE</applyServiceVersion>
                <supportIdServiceVersion>1.0.1-RELEASE</supportIdServiceVersion>
                <performanceServiceVersion>1.0.1-RELEASE</performanceServiceVersion>
            </properties>
        </profile>
    </profiles>

    <properties>
        <file_encoding>UTF-8</file_encoding>
        <project.encoding>UTF-8</project.encoding>
        <profile.name>myph</profile.name>

       <commonVersion>1.0.1-SNAPSHOT</commonVersion>
        <userServiceVersion>1.0.1-SNAPSHOT</userServiceVersion>
        <employeeServiceVersion>1.0.1-SNAPSHOT</employeeServiceVersion>
        <junitServiceVersion>1.0.1-SNAPSHOT</junitServiceVersion>
        <performanceServiceVersion>1.0.1-SNAPSHOT</performanceServiceVersion>

        <!-- Common libs -->
        <dubbo_version>2.8.4</dubbo_version>
        <zkclient_version>0.1</zkclient_version>
        <zookeeper_version>3.4.6</zookeeper_version>
        <cglib_version>3.1</cglib_version>
        <asm_version>3.1</asm_version>

        <!-- spring -->
        <spring_bom_version>4.2.0.RELEASE</spring_bom_version>
        <spring_data_version>1.7.0.RELEASE</spring_data_version>

        <!-- aspectj -->
        <aspectjlib_version>1.6.2</aspectjlib_version>
        <aspectj_version>1.7.4</aspectj_version>
        <aopalliance_version>1.0</aopalliance_version>

        <!-- remoting -->
        <hessian_version>4.0.38</hessian_version>

        <javassist_version>3.18.2-GA</javassist_version>

        <!-- Build args -->
        <skip_maven_deploy>false</skip_maven_deploy>
        <updateReleaseInfo>true</updateReleaseInfo>
        <project.build.sourceEncoding>${file_encoding}</project.build.sourceEncoding>
        <!-- activemq -->
        <active.core.version>5.7.0</active.core.version>

        <!-- for maven compiler plugin -->
        <maven_compiler_plugin_version>3.3</maven_compiler_plugin_version>
        <java_source_version>1.7</java_source_version>
        <java_target_version>1.7</java_target_version>

        <pinyin4j.version>2.5.0</pinyin4j.version>
    </properties>


    <dependencyManagement>
        <dependencies>
            <!-- service-component libs -->
            <dependency>
                <groupId>com.myph</groupId>
                <artifactId>common</artifactId>
                <version>${commonVersion}</version>
            </dependency>
            <dependency>
                <groupId>com.myph</groupId>
                <artifactId>performance-service-api</artifactId>
                <version>${performanceServiceVersion}</version>
            </dependency>
            <dependency>
                <groupId>com.myph</groupId>
                <artifactId>performance-service-impl</artifactId>
                <version>${performanceServiceVersion}</version>
            </dependency>
            <dependency>
                <groupId>com.myph</groupId>
                <artifactId>apply-service-api</artifactId>
                <version>${applyServiceVersion}</version>
            </dependency>
            <dependency>
                <groupId>com.myph</groupId>
                <artifactId>apply-service-impl</artifactId>
                <version>${applyServiceVersion}</version>
            </dependency>
            <dependency>
                <groupId>com.myph</groupId>
                <artifactId>user-service-api</artifactId>
                <version>${userServiceVersion}</version>
            </dependency>
            <dependency>
                <groupId>com.myph</groupId>
                <artifactId>user-service-impl</artifactId>
                <version>${userServiceVersion}</version>
            </dependency>

            <dependency>
                <groupId>com.myph</groupId>
                <artifactId>employee-service-api</artifactId>
                <version>${employeeServiceVersion}</version>
            </dependency>
            <dependency>
                <groupId>com.myph</groupId>
                <artifactId>employee-service-impl</artifactId>
                <version>${employeeServiceVersion}</version>
            </dependency>
            <dependency>
                <groupId>com.myph</groupId>
                <artifactId>base-service-api</artifactId>
                <version>${baseServiceVersion}</version>
            </dependency>
            <dependency>
                <groupId>com.myph</groupId>
                <artifactId>base-service-impl</artifactId>
                <version>${baseServiceVersion}</version>
            </dependency>
            <dependency>
                <groupId>com.myph</groupId>
                <artifactId>member-service-api</artifactId>
                <version>${memberServiceVersion}</version>
            </dependency>
            <dependency>
                <groupId>com.myph</groupId>
                <artifactId>member-service-impl</artifactId>
                <version>${memberServiceVersion}</version>
            </dependency>


            <!--dubbo -->
            <dependency>
                <groupId>com.github.sgroschupf</groupId>
                <artifactId>zkclient</artifactId>
                <version>${zkclient_version}</version>
                <exclusions>
                    <exclusion>
                        <groupId>log4j</groupId>
                        <artifactId>log4j</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
            <dependency>
                <groupId>org.apache.zookeeper</groupId>
                <artifactId>zookeeper</artifactId>
                <version>${zookeeper_version}</version>
                <exclusions>
                    <exclusion>
                        <groupId>log4j</groupId>
                        <artifactId>log4j</artifactId>
                    </exclusion>
                    <exclusion>
                        <groupId>org.slf4j</groupId>
                        <artifactId>slf4j-log4j12</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>dubbo</artifactId>
                <version>${dubbo_version}</version>
            </dependency>


            <!-- begin httpcomponents -->
            <dependency>
                <groupId>org.apache.httpcomponents</groupId>
                <artifactId>httpcore</artifactId>
                <version>${apache_httpcore_version}</version>
            </dependency>
            <dependency>
                <groupId>org.apache.httpcomponents</groupId>
                <artifactId>httpclient-cache</artifactId>
                <version>${apache_httpclient_version}</version>
            </dependency>
            <dependency>
                <groupId>org.apache.httpcomponents</groupId>
                <artifactId>httpmime</artifactId>
                <version>${apache_httpclient_version}</version>
            </dependency>
        <!-- end httpcomponents -->

            <dependency>
                <groupId>commons-beanutils</groupId>
                <artifactId>commons-beanutils</artifactId>
                <version>${commons_beanutils_version}</version>
                <exclusions>
                    <exclusion>
                        <groupId>commons-logging</groupId>
                        <artifactId>commons-logging</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
            <dependency>
                <groupId>org.apache.commons</groupId>
                <artifactId>commons-lang3</artifactId>
                <version>${commons_lang3_version}</version>
            </dependency>

            <!-- spring -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-framework-bom</artifactId>
                <version>${spring_bom_version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <dependency>
                <groupId>org.springframework.data</groupId>
                <artifactId>spring-data-commons</artifactId>
                <version>${spring_data_version}</version>
            </dependency>
            <!-- Aspectj -->
            <dependency>
                <groupId>org.aspectj</groupId>
                <artifactId>aspectjlib</artifactId>
                <version>${aspectjlib_version}</version>
            </dependency>
            <dependency>
                <groupId>org.aspectj</groupId>
                <artifactId>aspectjrt</artifactId>
                <version>${aspectj_version}</version>
            </dependency>
            <dependency>
                <groupId>org.aspectj</groupId>
                <artifactId>aspectjweaver</artifactId>
                <version>${aspectj_version}</version>
            </dependency>
            <dependency>
                <groupId>aopalliance</groupId>
                <artifactId>aopalliance</artifactId>
                <version>${aopalliance_version}</version>
            </dependency>
            <dependency>
                <groupId>redis.clients</groupId>
                <artifactId>jedis</artifactId>
                <version>${jedis_version}</version>
            </dependency>

            <!-- Servlet libs -->
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>javax.servlet-api</artifactId>
                <version>${servlet_version}</version>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>javax.servlet.jsp</groupId>
                <artifactId>javax.servlet.jsp-api</artifactId>
                <version>${servlet_jsp_version}</version>
                <scope>provided</scope>
            </dependency>

            <dependency>
                <groupId>ch.qos.logback</groupId>
                <artifactId>logback-core</artifactId>
                <version>${logback_version}</version>
            </dependency>
            <!--commons-logging到slf4j的桥梁-->
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>jcl-over-slf4j</artifactId>
                <version>${jcl-over-slf4j_version}</version>
            </dependency>

            <!-- Test libs -->
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>${junit_version}</version>
            </dependency>
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <version>${lombok_verion}</version>
            </dependency>

            <dependency>
                <groupId>org.apache.hbase</groupId>
                <artifactId>hbase-client</artifactId>
                <version>${hbase_version}</version>
                <exclusions>
                    <exclusion>
                        <groupId>jdk.tools</groupId>
                        <artifactId>jdk.tools</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
            <dependency>
                <groupId>org.apache.hbase</groupId>
                <artifactId>hbase-common</artifactId>
                <version>${hbase_version}</version>
            </dependency>
            <dependency>
                <groupId>org.apache.hbase</groupId>
                <artifactId>hbase-rest</artifactId>
                <version>${hbase_version}</version>
            </dependency>
        </dependencies>
    </dependencyManagement>


    <dependencies>
        <!-- apache -->
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>commons-logging</groupId>
                    <artifactId>commons-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
        </dependency>
        <dependency>
            <groupId>commons-beanutils</groupId>
            <artifactId>commons-beanutils</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>commons-logging</groupId>
                    <artifactId>commons-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
        </dependency>
        <dependency>
            <groupId>commons-codec</groupId>
            <artifactId>commons-codec</artifactId>
        </dependency>
        <dependency>
            <groupId>commons-collections</groupId>
            <artifactId>commons-collections</artifactId>
        </dependency>
        <dependency>
            <groupId>commons-digester</groupId>
            <artifactId>commons-digester</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>commons-logging</groupId>
                    <artifactId>commons-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>cglib</groupId>
            <artifactId>cglib-nodep</artifactId>
        </dependency>
        <dependency>
            <groupId>asm</groupId>
            <artifactId>asm</artifactId>
        </dependency>

        <!-- spring -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>commons-logging</groupId>
                    <artifactId>commons-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-commons</artifactId>
        </dependency>
        <!-- Aspectj -->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjlib</artifactId>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
        </dependency>
        <dependency>
            <groupId>aopalliance</groupId>
            <artifactId>aopalliance</artifactId>
        </dependency>
        <!-- fast json -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
        </dependency>

        <!-- orm -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
        </dependency>

        <!-- redis -->
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-redis</artifactId>
        </dependency>
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
        </dependency>

        <!-- Log libs -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-core</artifactId>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>jcl-over-slf4j</artifactId>
        </dependency>
        <!--log4j1到slf4j的桥梁-->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>log4j-over-slf4j</artifactId>
        </dependency>
        <!--jul-to-slf4j-->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>jul-to-slf4j</artifactId>
        </dependency>
        <dependency>
            <groupId>org.logback-extensions</groupId>
            <artifactId>logback-ext-spring</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>

        <!--hbase-->
        <dependency>
            <groupId>org.apache.hbase</groupId>
            <artifactId>hbase-client</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>jdk.tools</groupId>
                    <artifactId>jdk.tools</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <!-- begin activemq -->
        <dependency>
            <groupId>org.apache.activemq</groupId>
            <artifactId>activemq-core</artifactId>
            <version>${active.core.version}</version>
        </dependency>
        <!-- end activemq-->
        <dependency>
            <groupId>org.apache.hbase</groupId>
            <artifactId>hbase-common</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.hbase</groupId>
            <artifactId>hbase-rest</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>jdk.tools</groupId>
                    <artifactId>jdk.tools</artifactId>
                </exclusion>
                <exclusion>
                    <artifactId>servlet-api</artifactId>
                    <groupId>javax.servlet</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>jasper-compiler</artifactId>
                    <groupId>tomcat</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>jasper-runtime</artifactId>
                    <groupId>tomcat</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>jsp-api</artifactId>
                    <groupId>javax.servlet.jsp</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>pinyin4j</groupId>
            <artifactId>pinyin4j</artifactId>
        </dependency>
    </dependencies>

    <!-- 构建配置 -->
    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-resources-plugin</artifactId>
                    <version>${maven_resources_plugin_version}</version>
                    <configuration>
                        <encoding>${file_encoding}</encoding>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-source-plugin</artifactId>
                    <executions>
                        <execution>
                            <id>attach-sources</id>
                            <goals>
                                <goal>jar</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-jar-plugin</artifactId>
                    <version>${maven_jar_plugin_version}</version>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-war-plugin</artifactId>
                    <version>${maven_war_plugin_version}</version>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-install-plugin</artifactId>
                    <version>${maven_install_plugin_version}</version>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-deploy-plugin</artifactId>
                    <version>${maven_deploy_plugin_version}</version>
                    <configuration>
                        <updateReleaseInfo>true</updateReleaseInfo>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>${maven_compiler_plugin_version}</version>
                    <configuration>
                        <source>${java_source_version}</source>
                        <target>${java_target_version}</target>
                        <encoding>${file_encoding}</encoding>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <configuration>
                        <skip>true</skip>
                    </configuration>
                    <version>${maven_surefire_plugin_version}</version>
                </plugin>
            </plugins>
        </pluginManagement>

    </build>

    <distributionManagement>
        <repository>
            <id>nexus-releases</id>
            <name>Nexus Release Repository</name>
            <url>${repo.internal.releases.url}</url>
        </repository>
        <snapshotRepository>
            <id>nexus-snapshots</id>
            <name>Nexus Snapshot Repository</name>
            <url>${repo.internal.snapshots.url}</url>
        </snapshotRepository>
    </distributionManagement>
</project>

employee的pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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">
    <parent>
        <artifactId>parent</artifactId>
        <groupId>com.myph</groupId>
        <version>1.0.0-SNAPSHOT</version>
        <relativePath>../../../01基础层/parent/pom.xml</relativePath>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>employee-service-impl</artifactId>
    <packaging>jar</packaging>
    <version>${employeeServiceVersion}</version>
    <dependencies>
        <dependency>
            <groupId>com.myph</groupId>
            <artifactId>employee-service-api</artifactId>
            <version>${employeeServiceVersion}</version>
        </dependency>
        <dependency>
            <groupId>com.myph</groupId>
            <artifactId>common</artifactId>
            <version>${commonVersion}</version>
        </dependency>
        <dependency>
            <groupId>com.myph</groupId>
            <artifactId>base-service-api</artifactId>
            <version>${baseServiceVersion}</version>
        </dependency> 

        <!--运行junit单元测试类需要的-->
        <dependency>
            <groupId>com.myph</groupId>
            <artifactId>junit-service</artifactId>
            <version>${junitServiceVersion}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- database -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>


</project>

可以看到employee的pom.xml较为简单,因为子项目中没有太多特殊需要引用的jar包,父项目中的dependencies可以被子项目继承。

如何找到想要引入的jar包groupId、artifactId、version

可以在maven仓库中寻找,http://mvnrepository.com/,将对应的信息复制进pom.xml即可。
这里写图片描述

${##}的使用

通过${##}这种格式,可以引用到setting.xml、maven内置变量、maven中通过<properties>定义的变量。

<name>${project.artifactId}</name>

这样写等价于<name>parent</name>

jar包冲突的解决

如果A依赖B的1.0版本
D依赖C,C依赖B的2.0版本,那么maven会有一个默认的加载顺序
路径优先:1.对于该项目而言,根据jar包的依赖路径,短的优先
声明优先:2.路径一样,被依赖者声明的前后有关,同一个pom,前面的优先
覆写优先:3.子pom优先于父pom
上面这种情况,可能我们引用了1.0版本,但1.0版本不能满足我们的要求,可以找到A,在引用A的位置加上

<exclusions>
    <exclusion>
        <groupId>jdk.tools</groupId>
         <artifactId>jdk.tools</artifactId>
     </exclusion>
</exclusions>

这样可以去除A的依赖。
如何找到jar包间的依赖关系呢?在eclipse中可以很直观的看到。
这里写图片描述

dependencyManagement与dependencies

当模块较多时,为了让多个项目依赖统一版本的jar包,抽象出了parent用于统一管理,在parent中的dependencyManagement注明版本号,让子项目中引用一个依赖而不用显示的列出版本号,Maven会沿着父子层次向上走,直到找到一个拥有dependencyManagement元素的项目。
这样做的好处:
统一管理项目的版本号,确保应用的各个项目的依赖和版本一致,才能保证测试的和发布的是相同的成果,因此,在顶层pom中定义共同的依赖关系。同时可以避免在每个使用的子项目中都声明一个版本号,这样想升级或者切换到另一个版本时,只需要在父类容器里更新,不需要任何一个子项目的修改;如果某个子项目需要另外一个版本号时,只需要在dependencies中声明一个版本号即可。子类就会使用子类声明的版本号,不继承于父类版本号。
区别:
dependencies即使在子项目中不写该依赖项,那么子项目仍然会从父项目中继承该依赖项(全部继承)。
dependencyManagement里只是声明依赖,并不实现引入,因此子项目需要显示的声明需要用的依赖。如果不在子项目中声明依赖,是不会从父项目中继承下来的;只有在子项目中写了该依赖项,并且没有指定具体版本,才会从父项目中继承该项,并且version和scope都读取自父pom;另外如果子项目中指定了版本号,那么会使用子项目中指定的jar版本。

build参数

parent下的pom.xml中通过build引入了一些插件,这些插件各有用处。
maven-resources-plugin:处理资源文件
maven-source-plugin:提供项目自动将源码打包并发布的功能
maven-jar-plugin:打包可执行jar
maven-war-plugin:打包可执行war
maven-install-plugin默认绑定在maven的生命周期install阶段
maven-deploy-plugin、
maven-compiler-plugin:编译源码,下面可以指定编译器版本。
maven-surefire-plugin:surefire 插件用来在maven构建生命周期的test phase执行一个应用的单元测试

distributionManagement参数

分发构件至远程仓库
mvn install 会将项目生成的构件安装到本地Maven仓库,mvn deploy 用来将项目生成的构件分发到远程Maven仓库。本地Maven仓库的构件只能供当前用户使用,在分发到远程Maven仓库之后,所有能访问该仓库的用户都能使用你的构件。
我们需要配置POM的distributionManagement来指定Maven分发构件的位置。

version下的SNAPSHOT与RELEASE

在使用maven过程中,我们在开发阶段经常性的会有很多公共库处于不稳定状态,随时需要修改并发布,可能一天就要发布一次,遇到bug时,甚至一天要发布N次。我们知道,maven的依赖管理是基于版本管理的,对于发布状态的artifact,如果版本号相同,即使我们内部的镜像服务器上的组件比本地新,maven也不会主动下载的。如果我们在开发阶段都是基于正式发布版本来做依赖管理,那么遇到这个问题,就需要升级组件的版本号,可这样就明显不符合要求和实际情况了。但是,如果是基于快照版本,那么问题就自热而然的解决了,而maven已经为我们准备好了这一切。
maven中的仓库分为两种,snapshot快照仓库和release发布仓库。snapshot快照仓库用于保存开发过程中的不稳定版本,release正式仓库则是用来保存稳定的发行版本。定义一个组件/模块为快照版本,只需要在pom文件中在该模块的版本号后加上-SNAPSHOT即可。
maven2会根据模块的版本号(pom文件中的version)中是否带有-SNAPSHOT来判断是快照版本还是正式版本。如果是快照版本,那么在mvn deploy时会自动发布到快照版本库中,而使用快照版本的模块,在不更改版本号的情况下,直接编译打包时,maven会自动从镜像服务器上下载最新的快照版本。如果是正式发布版本,那么在mvn deploy时会自动发布到正式版本库中,而使用正式版本的模块,在不更改版本号的情况下,编译打包时如果本地已经存在该版本的模块则不会主动去镜像服务器上下载。
所以,我们在开发阶段,可以将公用库的版本设置为快照版本,而被依赖组件则引用快照版本进行开发,在公用库的快照版本更新后,我们也不需要修改pom文件提示版本号来下载新的版本,直接mvn执行相关编译、打包命令即可重新下载最新的快照库了,从而也方便了我们进行开发。

scope参数

compile (编译范围)
compile是默认的范围;如果没有提供一个范围,那该依赖的范围就是编译范围。编译范围依赖在所有的classpath 中可用,同时它们也会被打包。

provided (已提供范围)
provided 依赖只有在当JDK 或者一个容器已提供该依赖之后才使用。例如, 如果你开发了一个web 应用,你可能在编译 classpath 中需要可用的Servlet API 来编译一个servlet,但是你不会想要在打包好的WAR 中包含这个Servlet API;这个Servlet API JAR 由你的应用服务器或者servlet 容器提供。已提供范围的依赖在编译classpath (不是运行时)可用。它们不是传递性的,也不会被打包。

runtime (运行时范围)
runtime 依赖在运行和测试系统的时候需要,但在编译的时候不需要。比如,你可能在编译的时候只需要JDBC API JAR,而只有在运行的时候才需要JDBC
驱动实现。
test (测试范围)
test范围依赖 在一般的编译和运行时都不需要,它们只有在测试编译和测试运行阶段可用。

system (系统范围)
system范围依赖与provided 类似,但是你必须显式的提供一个对于本地系统中JAR 文件的路径。这么做是为了允许基于本地对象编译,而这些对象是系统类库的一部分。这样的构件应该是一直可用的,Maven 也不会在仓库中去寻找它。如果你将一个依赖范围设置成系统范围,你必须同时提供一个 systemPath 元素。注意该范围是不推荐使用的(你应该一直尽量去从公共或定制的 Maven 仓库中引用依赖)。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值