官术网_书友最值得收藏!

Apache Maven

Maven is an open-source software project management tool. It was born out of the need to simplify Ant-based builds and it favors convention over configuration. In more concrete terms, it offers out of the box support for typical workflows, such as building Java web applications, provided one follows its conventions. However, customizing the build process can prove challenging, and many of its detractors point this design choice out as the main motivation for not using Maven.

Note

More information on Maven can be found at https://maven.apache.org.

While Maven offers many features, they are beyond the scope of this chapter. We will focus on its support for building Java web applications.

The following POM (Project Object Model) file illustrates how a project can be declared with Maven (in a file called pom.xml):

<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.packtpub.rest-with-spring</groupId>
    <artifactId>rest-with-spring</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>war</packaging>

</project>

This pom.xml file defines a project with a group identifier, artifact identifier, and version. It also describes how the resulting application will be packaged (as a WAR file in this case).

Tip

Maven artifacts belong to a group (typically com.company.application), and must have a unique identifier (typically the application's name).

Developers are free to choose their versioning schemes. However, Maven works best with a version of the form x.y, where x is the major version number and y the minor version number.

Note

The SNAPSHOT version suffix tells Maven that this project is currently in development. It has implications for the way artifacts are handled by the dependency resolution, as explained in the following section.

Dependency management in Apache Maven

One build feature that makes development much easier is dependency management. It alleviates the need to manually download third-party libraries and package them with the application. Maven has robust dependency management based on artifact repositories.

Tip

The main repository for Maven is called the Central Repository and can be searched on at http://maven.org.

To declare dependencies, the sample POM file that was presented in the previous section can be amended in the following fashion:

// content omitted for readability purposes
    <version>1.0.0-SNAPSHOT</version>
    <packaging>war</packaging>
    <dependencies>
        <dependency>
 <groupId>org.hibernate</groupId>
 <artifactId>hibernate-core</artifactId>
 <version>4.1.9.Final</version>
 </dependency>
        <!-- Test Dependencies -->
        <dependency>
 <groupId>junit</groupId>
 <artifactId>junit</artifactId>
 <version>4.11</version>
 <scope>test</scope>
 </dependency>
    </dependencies>
</project>

The <dependencies> element in the amended POM file declares two dependencies:

  • Hibernate: This is a standard dependency that Maven will automatically fetch before compiling the project
  • JUnit: With this dependency, Maven will alter the classpath used to compile and execute the unit tests

Another build tool that has gathered a lot of popularity is Gradle and it is discussed in the next section

主站蜘蛛池模板: 鄯善县| 宝兴县| 辽宁省| 光泽县| 繁峙县| 攀枝花市| 三亚市| 铜鼓县| 塘沽区| 元阳县| 垦利县| 安宁市| 乌兰察布市| 乌兰县| 合肥市| 鄯善县| 和田市| 翁牛特旗| 蕲春县| 石棉县| 大足县| 丰台区| 绩溪县| 钟祥市| 霍城县| 墨江| 锦州市| 二连浩特市| 获嘉县| 南阳市| 贵港市| 聂拉木县| 青海省| 东兴市| 广州市| 苏尼特右旗| 兰坪| 揭东县| 高青县| 扬州市| 吉木萨尔县|