`Maven`常用几个点

1 阅读1分钟

Maven常用几个点

1.Maven是什么?

Maven是一个大家伙,核心是依赖管理。这个工具更像是一个目录,帮你描述项目的梗概。

官方的说法是,Maven是一个项目管理工具,它包含一个项目对象模型(Project Ovject Model),一组标准集合,一个项目生命周期(Project Lifecycle),一个依赖管理系统(Dependency Management System),和用来运行定义在生命周期阶段(phase)中插件(plugin)目标(goal)的逻辑。

Maven的核心设计思想是约定大于配置。在没有自定义的情况下,Maven会假定源代码在${basedir}/src/main/java,假定资源文件在${basedir}/src/main/resource。假定测试代码在${basedir}/src/test。项目假定会产生一个jar文件。Maven假定你想要把编译好的字节码放到${basedir}/target/classes,并且在${basedir}/target创建一个可分发的JAR文件。

2.Maven构建中几个关键功能

2.1.忽略失败的单元测试

如果你的目的只是希望构建输出,而不关注失败的单元测试,那就必须告诉Maven让它忽略测试失败。如果不告诉Maven,当Maven遇到测试失败时会停止当前构建。

<project>
[...]
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <testFailureIgnore>true</testFailureIgnore>
                </configuration>
            </plugin>
        </plugins>
    </build>
[...]
</project>

2.2.跳过单元测试

如果你觉得单元测试花了太多时间,又或者单元测试错误太多,控制台中错误输出太多,你可以告诉Maven让它跳过单元测试。

<project>
[...]
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <skip>true</skip>
                </configuration>
            </plugin>
        </plugins>
    </build>
[...]
</project>

3.使用Maven构建一个多模块应用

多模块项目在根(父)POM引用子模块。

POM

<project>
[...]
    <packgeing>pom</packgeing>
    <modules>
        <module>simple-weather</module>
        <module>simple-webapp</module>
    </modules>
[...]
</project>

4.优化依赖

当一个多模块项目越来越庞大,越来越复杂,就有可能出现重复的依赖声明。

重复依赖声明使得很难保证一个大项目中的版本一致性。这个时候需要在父POM中巩固版本和依赖声明。

找出所有被用于一个以上模块的依赖,然后将其向上移到父POMdependencyManagment片段。

<project>
...
   <dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring</artifactId>
            <version>2.0.7</version>
        </dependency>
        <dependency>
            <groupId>org.apache.velocity</groupId>
            <artifactId>velocity</artifactId>
            <version>1.5</version>
        </dependency>
    </dependencies> 
   </dependencyManagement> 
...
</project>

在这些依赖配置被上移之后,我们需要将子模块中的这些依赖的版本从子模块的POM中移除,否则它们会覆盖定义在父POM中的dependencyManagement

<project>
...
    <dependencies>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-annotations</artifactId>
        </dependency>
    </dependencies>
...
</project>

对于多个依赖,但是版本号重复的处理方式如下:

<project>
...
    <properties>
        <hibernate.annotations.version>3.3.0.ga</hibernate.annotations.version>
    </properties> 
    <dependencyManagement>
    ...
       <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-annotations</artifactId>
            <version>${hibernate.annotations.version}</version>
        </dependency> 
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-commons-annotations</artifactId>
            <version>${hibernate.annotation.version}</version>
        </dependency>
    ...
    </dependencyManagement>
...
</project>

5.插件优化

<project>
...
    <properties>
        <hibernate.annotations.version>3.3.0.ga</hibernate.annotations.version>
        <hsqldb.version>1.8.0.7</hsqldb.version>
    </properties>
    [...]
    <build>
        <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>
                <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>
    </build>
...
</project>