Maven使用

    xiaoxiao2021-12-14  18

    Maven使用介绍

    创建project

    先去官方网站下载一个最新版本http://maven.apache.org/download.cgi.下载后解压,使用之前最好先将maven的bin目录设置到path环境变量里面。

    maven无非也就是用来build一个project的,直接先上一个例子,在命令行下输入下面的命令:

    mvn archetype:generate DarchetypeGroupId=org.apache.maven.archetypes-DgroupId=com.mycompany.app -DartifactId=myapp

    mvn就是maven的命令行程序,archetype:generate中的archetype是plugin的名字,generate是goal的名字,命令行后面的是一些参数。关于archetype和goal以及后面的参数,后面再细说。

    如果是第一次运行,这个过程会有点慢,maven需要下载一些依赖项,中间如果有输入提示信息,直接回车使用默认值就可以了。这条命令执行完后,会在你的当前目录下生成一个名为myapp的目录:

    注意这个目录结构,src/main/java 和 src/test/java 是不能改动,不然maven会无法找到源文件。下面是maven一个标准的目录结构:

    src/main/java

    Application/Library sources

    src/main/resources

    Application/Library resources

    src/main/filters

    Resource filter files

    src/main/assembly

    Assembly descriptors

    src/main/config

    Configuration files

    src/main/scripts

    Application/Library scripts

    src/main/webapp

    Web application sources

    src/test/java

    Test sources

    src/test/resources

    Test resources

    src/test/filters

    Test resource filter files

    src/site

    Site

    另外maven还生成了一个重要的文件pom.xml,maven就是通过这个文件来来管理整个project,可以理解位类似于eclipse的.project文件。默认生成的pom.xml文件的内容如下:

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    /* 1-1 */

    <projectxmlns="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.0http://maven.apache.org/xsd/maven-4.0.0.xsd">

        <modelVersion>4.0.0</modelVersion>

        <groupId>com.mycompany.app</groupId>

        <artifactId>my-app</artifactId>

        <version>1.1.0.1</version>

        <packaging>jar</packaging>

        <name>myapp</name>

        <url>http://maven.apache.org</url>

        <properties>

            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

        </properties>

        <dependencies>

            <dependency>

                <groupId>junit</groupId>

                <artifactId>junit</artifactId>

                <version>3.8.1</version>

                <scope>test</scope>

            </dependency>

        </dependencies>

    </project>

    解释一下这个xml文件的内容:

    ·        modelVersion: 这个XML文件所使用的POM格式的版本

    ·        groupId: 相当于这个project的所有者或者机构的一个标识,一般是com.company.xxx这种格式

    ·        artifactId:  这个project最后所生成的文档(jar、war)的名字,比如对于junit这个开源的project,它的artifactId就是junit

    ·        packaging: 这个project的打包的类型,一般是war、jar等值

    ·        version: project的版本

    ·        name: project的名字,生成文档等内容的时候会用的这个名字

    这个project创建好后和普通的project没有什么不同,我们直接往里面放源代码进行开发就可以了,如果有目录想修改的也完全可以。

     

    POM & archetype

    archetype就是一个project的模板,上面我们生成的project就是用默认的archetype生成的。如果使用不同的archetype,生成的project结构会有所不同。 一个archetype指明了

    ·        1) 项目的目录结构以及什么目录是放source code,哪些是放test source code,哪些目录是放resource的。

    ·        2) 一个默认的pom.xml文件,这个默认的pom.xml文件中的groupId,artifactId,version用占位符表示,在创建project的时候通过参数传进来。

    pom.xml文件的POM全称是Project Object Model,这个文件对于maven的使用者来说是一个和maven交互的渠道,pom.xml包含了一个maven project的配置,一个project该如何编译打包,project有哪些依赖项等等。

    仔细看一下我们前面创建project的命令:mvn archetype:generateDarchetypeGroupId=org.apache.maven.archetypes -DgroupId=com.mycompany.app-DartifactId=myapp

    1) archetype:generate, 这是一个maven的plugin,用来从一个archetype创建一个project,关于plugin后面再说。 2) DarchetypeGroupId,这个就是指的archetype的groupid,也就是说是用的哪个archetype,或者说用哪个项目模板。 3) 后面的两个参数,用来放大pom.xml文件里面,作为当前创建的project的描述信息。

    Project创建好了,看如何去编译,直接进入的project的目录,在命令行下:

    mvn compile

    编译完后maven会创建一个target目录去保存编译结果。 我们需要编译成一个什么样的内容,以及要输出到什么地方等等,都是可以在pom.xml文件里面配置的,但是因为我们目前并没有指定这些内容,所以maven会使用默认值。

    我们还可以用maven执行test:

    mvn test

    第一次执行时,maven会去下载一些依赖项。另外要注意的时,如果我们更改了默认的目录结构,maven会因为找bu到test而无法去执行test。如果只需要编译test可以执行:

    mvn test-compile

    要把项目打包,执行:

    mvn package

    mvn会根据pom.xml里面的packaging选项打包成相应的文件。

     

    repository & dependency

    maven里面有一个repository的概念,当我们的项目依赖于某个jar时,maven会去repository里面去找。repository分两种,一种是远程的,一种是本地的。如果有几个project都用到junit,我们可以把junit放在repository里面,几个project可以公用,节约存储空间而且方便管理,这个repository的位置可以在pom.xml里面设置。

    本地的默认的路径是安装用户的目录下的 .m2\repository 文件夹。如果一个依赖项在本地的repository里面没有,那么maven会去他自己的远程的repository http://repo.maven.apache.org/maven2 去下载后放到本地的repository里面。

    也就是说,我们如果我们的project需要要引用一个依赖项,我们只需要在pom.xml文件中进行配置,maven会自动帮我们去引用。 我们之前的创建project里面需要写单元测试,引用到了junit,看pom中的配置:

    1

    2

    3

    4

    5

    6

    7

    8

    <dependencies>

        <dependency>

            <groupId>junit</groupId>

            <artifactId>junit</artifactId>

            <version>3.8.1</version>

            <scope>test</scope>

        </dependency>

    </dependencies>

    每一个需要为每一个 dependency 指明groupId,artifactId,version。scope很简单,意思是说我们需要怎么引用,比如我们上面的例子里面设置的是test,意思是说只在test里面引用junit。 但是我们如何知道groupId,artifactId和version呢? 比如我现在想引用log4j,这个几个值怎么填? 可以去http://mirrors.ibiblio.org/maven2/上去查找。比如log4j,我们就在上面这个地址加上log4j,也就是http://mirrors.ibiblio.org/maven2/junit/。进去后会有一个maven-metadata.xml,打开就可以知道这些值了然后添加这个dependency了。

    如果要把一个project安装到本地的repository里面,可以执行下面的命令:

    mvn install

     

    到这里就说完了创建,编译,测试,打包以及安装,大部分的项目也就是做这些事情。

    再介绍几个其它命令:

    mvn site : 为你的project创建一个站点 mvn clean: 清除target目录下的所有文件 mvn eclipse:eclipse :为project生成eclipse的工程文件和classpath文件

     

    build lifecycle & build phase &goal

    maven有一套build的生命周期,是按照一套顺序走下来的,这一套顺序就叫一个生命周期(lifecycle)。maven内置三种生命周期:default, clean 和 site。一个生命周期分为多个build phase,下面是default生命周期全部的build phase:

    ·        validate:validatethe project is correct and all necessary information is available.

    ·        initialize:initializebuild state, e.g. set properties or create directories.

    ·        generate-sources:generateany source code for inclusion in compilation.

    ·        process-sources:processthe source code, for example to filter any values.

    ·        generate-resources:generateresources for inclusion in the package.

    ·        process-resources:copyand process the resources into the destination directory, ready for packaging.

    ·        compile:compilethe source code of the project.

    ·        process-classes:post-processthe generated files from compilation, for example to do bytecode enhancement onJava classes.

    ·        generate-test-sources:generateany test source code for inclusion in compilation.

    ·        process-test-sources:processthe test source code, for example to filter any values.

    ·        generate-test-resources:createresources for testing.

    ·        process-test-resources:copyand process the resources into the test destination directory.

    ·        test-compile:compilethe test source code into the test destination directory

    ·        process-test-classes:post-processthe generated files from test compilation, for example to do bytecodeenhancement on Java classes. For Maven 2.0.5 and above.

    ·        test:runtests using a suitable unit testing framework. These tests should not requirethe code be packaged or deployed.

    ·        prepare-package:performany operations necessary to prepare a package before the actual packaging. Thisoften results in an unpacked, processed version of the package. (Maven 2.1 andabove)

    ·        package:takethe compiled code and package it in its distributable format, such as a JAR.

    ·        pre-integration-test:performactions required before integration tests are executed. This may involve thingssuch as setting up the required environment.

    ·        integration-test:processand deploy the package if necessary into an environment where integration testscan be run.

    ·        post-integration-test:performactions required after integration tests have been executed. This may includingcleaning up the environment.

    ·        verify:runany checks to verify the package is valid and meets quality criteria.

    ·        install:installthe package into the local repository, for use as a dependency in otherprojects locally.

    ·        deploy:donein an integration or release environment, copies the final package to theremote repository for sharing with other developers and projects.

    这些build phase是按照顺序执行的,如果执行后面的build phase,前面的build phase 也会被执行。例如如果执行:

    mvn deploy

    前面的install、verify一直到validate这些build phase都会执行。

    每一个build phase是由goal组成的,一个goal其实就是一个任务,一个goal可以关联到一个build phase也可以不关联到任何build phase 。 不关联到任何phase的goal是可以独立执行的,例如:

    mvn cleandependency:copy-dependencies package

    上面的命令会导致先执行clean这个phase,然后拷贝依赖项,最后打包。注意,这里clean这个goal是clean这个lifecycle里面的一个goal,所以可以看到不同lifecycle的build phase和goal是可以混合在一起执行的。 如果一个goal被绑定到多个phase上,那么goal就会被执行多次。

    phase的顺序是已经固定的,如果一个phase没有绑定到任何goal,那么phase就不会被执行。 一个goal可以通过两种方式绑定到一个phase,一个是指定packaging,另一个就是plugin。

     

    packaging&plugin

    plugin就是用来向maven提供goal的。一个plugin里面可以有多个goal,这就是为什么我们在指明goal时,前面会用一个冒号与plugin的名字。

    一个plugin自己可以指定自己的goal绑定到哪个lifecycle的哪一个Phase上,另外也可以配置一个goal绑定到哪个phase上。可以在pom.xml里面配置。 看两个配置:

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    <plugin>

       <groupId>org.codehaus.modello</groupId>

       <artifactId>modello-maven-plugin</artifactId>

       <version>1.4</version>

       <executions>

         <execution>

           <configuration>

             <models>

               <model>src/main/mdo/maven.mdo</model>

             </models>

             <version>4.0.0</version>

           </configuration>

           <goals>

             <goal>java</goal>

           </goals>

         </execution>

       </executions>

     </plugin>

    这个就在当前的lifecycle里面添加了一个名字叫java的goal,这goal会根据自己的配置去绑定到一个phase,在phase执行的时候这个goal会执行。并且在这个配置里面,可以指定多个execution来让这个goal执行多次。

    看另一个示例配置:

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    <plugin>

       <groupId>com.mycompany.example</groupId>

       <artifactId>display-maven-plugin</artifactId>

       <version>1.0</version>

       <executions>

         <execution>

           <phase>process-test-resources</phase>

           <goals>

             <goal>time</goal>

           </goals>

         </execution>

       </executions>

     </plugin>

    这个名为time的goal把自己绑定到了process-test-resource这个phase上。

    在默认情况下,并不是所有的phase都绑定了goal,比如clean这个lifecycle是有三个phase的,但是只有其中的一个名为clean的phase默认绑定了一个clean:clean goal,其它两个phase默认没有绑定任何goal。

    之前已经提到过packaging,在pom.xml可以指定packaging,每种packaging都设定了一组phase和goal之间的绑定关系。在default lifecycle下,当packaging为 ejb/ejb3/jar/par/rar/war 其中之一的值的时候,只有以下的phase绑定了goal,具体如下:

    process-resources

    resources:resources

    compile

    compiler:compile

    process-test-resources

    resources:testResources

    test-compile

    compiler:testCompile

    test

    surefire:test

    package

    jar:jar

    install

    install:install

    deploy

    deploy:deploy

     

    总结

    首先搞清楚maven的project的目录结构,然后理解maven的lifecycle,lifecycle是由build phase组成,每一个build phase会绑定到goal。goal是由plugin提供的。 每一种packaging的值都表明了一定的phase和goal之间的绑定关系。

    另外一个很重要的就是dependency,我们要在项目中引用一个依赖,只需要在pom.xml指定依赖的名字和版本,maven会自动去远程的repository下载,然后放到本地的repository里面,这样以后所有的project都可以共用

     

    转载请注明原文地址: https://ju.6miu.com/read-964531.html

    最新回复(0)