maven实战学习笔记二(仓库、生命周期与插件)

来源:互联网 发布:seo标签优化 编辑:程序博客网 时间:2024/04/28 17:59
仓库
 
    仓库分类:本地仓库、远程仓库(中央仓库、私服、其他公共库)(远程仓库好比书店,本地仓库好比书房)
    默认情况下 在本地仓库找,找不到才到远程仓库找

    本地仓库
    默认在.m2/respository,可以编辑文件.m2/settings.xml,配置localRespository元素,指定仓库位置
    <settings>
        <localRepository>E:\maven_repertory</localRepository>
    </settings>
    默认.m2/settings.xml文件是不存在的,复制$M2_HOME/conf/settings.xml文件在编辑


    安装本地构件
        运行命令: mvn install 使用install插件的install目标可以将文件安装到本地仓库中,具体路径根据坐标计算获得

    中央仓库
    默认的中央仓库为:http://repol.maven.org/maven2
    <repository>
        <id>central</id>
        <url>http://repo1.maven.org/maven2/</url>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </repository>
    snapshots-enabled 为false  表示不从中央仓库下载快照的构建,包含这段配置的文件是所有maven项目都会继承的超级POM.

    私服(推荐 nexus)
    私服是一种特殊的远程仓库,私服代理广域网上的远程仓库,供局域网maven用户使用。
    好处:节省带宽、加速构建、部署第三方构建(如组织内部的私有构件、由于版权因素不能发布的构件,如Oracle JDBC)、提高稳定性增强控制、降低中央仓库负担

    远程仓库
    很多情况下,默认的远程仓库无法满足项目的需求,可能项目需要的构件存于另一个远程仓库。如JBOSS
    <repositories>
        ....
        <repository>
            <id>central</id>
            <url>http://repository.jboss.com/maven2</url>
            <snapshots>
                <enabled>false</enabled>
                <checksumPolicy>ignore</checksumPolicy>
                <updatePolicy>dalily</updatePolicy>
            </snapshots>
        </repository>
        ...
    </repositories>
    <updatePolicy>配置maven从远程仓库检查更新的频率,默认为dalily(每天),never,always(每次构件都检查),interval:X(每个X分钟)
    <checksumPolicy>配置maven的检查策略与文件策略。默认为warn(执行构件是输出警告信息),fail(构件失败),ignore(完全忽略)
    远程仓库认证
    配置认证信息必须在settings.xml文件中
    <servers>
        ...
        <server>
          <id>deploymentRepo</id>
          <username>repouser</username>
          <password>repopwd</password>
        </server>
        ...
    </servers>
    settings.xml中server元素的id必须与POM中需要认证的repository元素的id完全一致。
    
    部署至远程仓库
    <distributionManagement>
        <repository>
            <id>proj-releases</id>
            <name>proj-releases repository</name>
            <url>http://192.168.1.100/content/repositories/pro-releases</url>
        </repository>
        <snapshotRepository>
            <id>proj-snapshot</id>
            <name>proj-snapshot repository</name>
            <url>http://192.168.1.100/content/repositories/pro-snapshot</url>
        </snapshotRepository>
    </distributionManagement>
    <repository>元素表示发布版本仓库,<snapshotRepository>表示快照版本仓库
    通过mvn deploy命令发布构件



    镜像
    如果仓库X可以提供仓库Y的所有内容,那么就可以认为X是Y的一个镜像
    在settings.xml 中的mirror中配置
    <mirrors>
        <mirror>
          <id>maven.net.cn</id>
          <mirrorOf>central</mirrorOf>
          <name>one of the central mirros in china</name>
          <url>http://maven.net.cn/content/groups/public/</url>
        </mirror>
    </mirrors>
    <mirrorOf>值为central,表示该配置为中央仓库的镜像,对于任何中央仓库的请求都会转至该请求。
    镜像最常见的用法是结合私服
    <mirror>
          <id>internal-repository</id>
          <mirrorOf>*</mirrorOf>
          <name>one of the central mirros in china</name>
          <url>http://192.168.1.100/maven2</url>
    </mirror>
    <mirrorOf>*</mirrorOf>匹配所有远程仓库
    <mirrorOf>external:*</mirrorOf>配置所有不在本机的远程仓库
    <mirrorOf>rep1,rep2</mirrorOf>配置仓库rep1,rep2
    <mirrorOf>*,!rep1</mirrorOf>配置所有远程仓库,rep1除外

    
生命周期和插件
    maven拥有3套相互独立的生命周期,分别为clean,default,site.clean生命周期目的是清理项目,default生命周期是构件项目,而site生命周期是建立项目站点。
    每个生命周期包含一些阶段(phase),这些阶段都是有序的,且后面的阶段依赖前面的阶段。用户和maven最直接的交互方式是调用这些生命周期阶段。

    clean生命周期
    pre-clean:执行一些清理前需要完成的工作
    clean:清理上一次构件生成的文件
    post-clean:执行一些清理后需要完成的工作

    default生命周期
    validate/initialize/generate-sources/process-sources/generate-resources/process-resources/compile/process-classes/generate-test-resources/
    process-test-sources/generate-test-resources/process-test-resources/test-compile/test/prepare-package/package/pre-integration-test/
    integration-test/post-integration-test/verify/install/deploy

    compile:编译项目
    test:使用单元测试框架测试项目
    package:打包好发布格式
    install:安装到本地仓库
    deploy:部署到远程仓库

    site生命周期
    pre-site:执行生成站点之前需要完成工作
    site:生成项目站点文档
    post-site:执行生成站点之后需要完成的工作
    site-deploy:将生成的站点发布到服务器上
    
    从命令行执行maven任务就是调用maven生命周期阶段,需要注意的是各个生命周期是相互独立的,而一个生命周期的阶段是有前后依赖的

    插件目标
    maven的核心仅仅定义了抽象的生命周期,具体的任务由插件完成,maven会在需要的时候下载并使用插件。

    插件绑定
    maven的生命周期与插件相互绑定,用以完成实际的构件任务。
    
    内置绑定
        
    自定义绑定
    除了内置绑定外,用户还能选择某个插件目标绑定到生命周期的某个阶段上
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-source-plugin</artifactId>
                <version>2.1.1</version>
                <executions>
                    <execution>
                        <id>attach-sources</id>
                        <phase>verify</phase>
                        <goals>
                            <goal>jar-no-fork</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
    在POM的build元素下的plugins子元素中声明插件的使用。<execution>子元素用来配置一个执行任务。<phase>元素将插件绑定到指定生命周期上,通过goals指定要执行的插件目标。

    配置插件参数
    命令行输入插件参数 mvn install -Dmaven.test.skip=true
    POM中插件全局配置
    <build>
        <plugins>
            ...
            <!-- 编译时采用JDK1.5 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.1</version>
                <configuration>
                    <source>1.5</source>
                    <target>1.5</target>
                </configuration>
            </plugin>
            ...
        </plugins>
    </build>
    POM中插件任务配置
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-antrun-plugin</artifactId>
                <version>1.3</version>
                <executions>
                    <execution>
                        <id>ant-validate</id>
                        <phase>validate</phase>
                        <goals>
                            <!-- 用来在maven中执行ant任务-->
                            <goal>run</goal>
                        </goals>
                        <configuration>
                            <tasks>
                                <echo>I'm bound to validate phase</echo>
                            </tasks>
                        </configuration>
                    </execution>
                    <execution>
                        <id>ant-verify</id>
                        <phase>verify</phase>
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <configuration>
                            <tasks>
                                <echo>I'm bound to verify phase</echo>
                            </tasks>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

    <configuration>元素位于<execution>元素下表示这是特定任务配置

获取插件信息
    在线插件信息
        主要的Maven插件都来自Apache(http://maven.apache.org/plugins/index.html)和Codehaus(http://mojo.codehaus.org/plugins.html)
    使用maven-help-plugin查看插件信息
    如:$ mvn help:describe -Dplugin=org.apache.maven.plugins:maven-compiler-plugin:2.1
    这里执行的是maven-help-plugin的discribe目标
    在描述插件的时候可以省略版本信息
    $ mvn help:describe -Dplugin=org.apache.maven.plugins:maven-compiler-plugin
    也可以使用插件前缀(Goal Prefix)替换坐标
    $ mvn help:describe -Dplugin=compiler
    仅仅查看插件目标信息
    $ mvn help:describe -Dplugin=compiler -Dgoal=compile
    查看插件详细信息
    $ mvn help:describe -Dplugin=compiler -Ddetail

    插件仓库
    maven会区别对待依赖的远程仓库与插件的远程仓库,配置插件的远程仓库使用<pluginRepositories><pluginRepository>元素
    <pluginRepositories>
        <pluginRepository>
            <id>central</id>
            <url>http://repo1.maven.org.maven2</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
            <releases>
                <updatePolicy>never</updatePolicy>
            </releases>
        </pluginRepository>
    </pluginRepositories>

原创粉丝点击