使用Maven来管理项目-pom.xml详细解读(三)

来源:互联网 发布:淘宝宝贝评价没有了 编辑:程序博客网 时间:2024/05/01 14:24
<?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>    <parent> <!-- 指定该pom的父级pom  -->        <groupId>org.codehaus.mojo</groupId> <!-- 父级pom组织id -->        <artifactId>my-parent</artifactId>  <!-- 父级pom标识 -->        <version>2.0</version>  <!-- 父级pom版本 -->        <relativePath>../my-parent</relativePath>   <!-- 父级pom位置 -->    </parent>    <groupId>com.yourcompany.app</groupId>  <!-- 当前项目的组织id -->    <artifactId>app</artifactId>  <!-- 当前maven项目的唯一标识-->    <version>1.0-SNAPSHOT</version>  <!-- 当前maven项目版本号 -->    <packaging>pom</packaging>   <!-- 当前maven项目package 命令打包类型 eg.war、jar...-->    <!-- 项目有多个模块时使用,解决项目之间的依赖关系。eg:web项目->框架项目 前台项目 后台项目... -->    <modules>        <module>webFrame</module>     <!-- 模块的名称 -->    </modules>    <dependencies>              <!-- 管理项目依赖的jar包 -->        <dependency>            <groupId>junit</groupId>    <!-- 依赖的jar包组织名称 -->            <artifactId>junit</artifactId>  <!-- 依赖的jar包名称-->            <version>4.0</version>  <!-- 依赖的jar包版本号,写法:            1.0: "Soft" requirement on 1.0 (just a recommendation, if it matches all other ranges for the dependency)            [1.0]: "Hard" requirement on 1.0            (,1.0]: x <= 1.0            [1.2,1.3]: 1.2 <= x <= 1.3            [1.0,2.0): 1.0 <= x < 2.0            [1.5,): x >= 1.5            (,1.0],[1.2,): x <= 1.0 or x >= 1.2; multiple sets are comma-separated            (,1.1),(1.1,): this excludes 1.1 (for example if it is known not to work in combination with this library)            -->            <type>jar</type> <!-- 依赖的jar包类型-->            <scope>test</scope>  <!-- 依赖包使用区域 有:            compile -> this is the default scope, used if none is specified. Compile dependencies are available in all classpaths. Furthermore, those dependencies are propagated to dependent projects.            provided - this is much like compile, but indicates you expect the JDK or a container to provide it at runtime. It is only available on the compilation and test classpath, and is not transitive.            runtime - this scope indicates that the dependency is not required for compilation, but is for execution. It is in the runtime and test classpaths, but not the compile classpath.            test - this scope indicates that the dependency is not required for normal use of the application, and is only available for the test compilation and execution phases.            system - this scope is similar to provided except that you have to provide the JAR which contains it explicitly. The artifact is always available and is not looked up in a repository.            特别指明是system区域,管理本地的jar包,同时需要添加systemPath属性来指明依赖包的位置-->            <!--<systemPath>.../WEB-INF/lib/xxxxxx.jar</systemPath>-->            <optional>true</optional>            <exclusions>       <!-- 当依赖的这个包同时也依赖了其他包时,用来指明依赖该包所依赖的某一个包,不依赖该包所依赖的所有包使用*来匹配 -->                <exclusion>                    <groupId>org.apache.maven</groupId>                    <artifactId>maven-core</artifactId>                </exclusion>                <!-- 依赖的jar包类型-->            </exclusions>        </dependency>        <dependency>            <groupId>org.apache.maven</groupId>            <artifactId>maven-embedder</artifactId>            <version>3.1.0</version>            <exclusions>                <exclusion>   <!-- 不依赖该包(org.apache.maven<)所依赖的所有包使 -->                    <groupId>*</groupId>                    <artifactId>*</artifactId>                </exclusion>            </exclusions>        </dependency>    </dependencies>    <dependencyManagement>    <!-- 通常在父级pom中添加这个元素,用来管理一下共用的依赖,使得继承该pom的子pom使用,子pom中是需要指定groupId 和 artifactId,版本号会自动被补全 -->        <dependencies>            <dependency>                <groupId>org.apache.ant</groupId>                <artifactId>ant</artifactId>                <version>1.8.1</version>            </dependency>        </dependencies>    </dependencyManagement>    <!--    properties->pom中的元素就一个pom的属性,pom的顶级属性就是project,属性的访问格式:${属性名称},    env.X: (访问当前系统环境变量)Prefixing a variable with "env." will return the shell's environment variable. For example, ${env.PATH} contains the PATH environment variable.Note: While environment variables themselves are case-insensitive on Windows, lookup of properties is case-sensitive. In other words, while the Windows shell returns the same value for %PATH% and %Path%, Maven distinguishes between ${env.PATH} and ${env.Path}. As of Maven 2.1.0, the names of environment variables are normalized to all upper-case for the sake of reliability.    project.x:(访问pom中的属性<pom的元素>) A dot (.) notated path in the POM will contain the corresponding element's value. For example: <project><version>1.0</version></project> is accessible via ${project.version}.    settings.x: (访问配置文件中的属性)A dot (.) notated path in the settings.xml will contain the corresponding element's value. For example: <settings><offline>false</offline></settings> is accessible via ${settings.offline}.Java System Properties: All properties accessible via java.lang.System.getProperties() are available as POM properties, such as ${java.home}.    x: (访问使用properties定义的变量)Set within a <properties /> element in the POM. The value of <properties><someVar>value</someVar></properies> may be used as ${someVar}.    -->    <properties>   <!-- 用来定义属性 eg:定义了一个变量名为varName 值为varValue的属性 访问:${varName} -->        <varName>varValue</varName>    </properties>    <!--    repositories是一些附加在Maven仓库目录显示的制品的集合,要想成为Maven仓库的制品,pom文件不需是$BASE_REPO/groupId/artifactId/version/artifactId-version.pom. $BASE_REPO结构    repositories就是收集和存储成品的地方。    -->    <repositories>        <repository>   <!-- repository是Maven社区最有力的特性,默认的Maven仓库中心是 http://repo.maven.apache.org/maven2/. -->            <id>central</id>    <!-- 编译配置 -->            <name>Central Repository</name>    <!-- 编译配置 -->            <url>http://repo.maven.apache.org/maven2</url>    <!-- 编译配置 -->            <layout>default</layout>    <!-- 编译配置 -->            <snapshots>    <!-- 编译配置 -->                <enabled>false</enabled>    <!-- 编译配置 -->            </snapshots>        </repository>        <repository>            <releases>                <enabled>false</enabled>                <updatePolicy>always</updatePolicy>                <checksumPolicy>warn</checksumPolicy>            </releases>            <snapshots>                <enabled>true</enabled>                <updatePolicy>never</updatePolicy>                <checksumPolicy>fail</checksumPolicy>            </snapshots>            <id>codehausSnapshots</id>            <name>Codehaus Snapshots</name>            <url>http://snapshots.maven.codehaus.org/maven2</url>            <layout>default</layout>        </repository>    </repositories>    <distributionManagement>        <!-- 成品发布管理 -->    </distributionManagement>    <pluginRepositories>    <!-- 编译配置 -->        <pluginRepository>    <!-- 编译配置 -->            <id>central</id>    <!-- 编译配置 -->            <name>Central Repository</name>    <!-- 编译配置 -->            <url>http://repo.maven.apache.org/maven2</url>    <!-- 编译配置 -->            <layout>default</layout>    <!-- 编译配置 -->            <snapshots>    <!-- 编译配置 -->                <enabled>false</enabled>    <!-- 编译配置 -->            </snapshots>            <releases>    <!-- 编译配置 -->                <updatePolicy>never</updatePolicy>    <!-- 编译配置 -->            </releases>        </pluginRepository>    </pluginRepositories>    <!-- 编译配置 -->    <build>        <defaultGoal>install</defaultGoal>      <!-- 设置默认的goal或phase 当什么都没有指定是执行该目标或阶段-->        <directory>${project.basedir}/target</directory>    <!-- 编译后的文件目标位置 路径都是绝对路径-->        <outputDirectory>${project.build.directory}/classes</outputDirectory>     <!-- 编译后的文件输出位置  -->        <finalName>${project.artifactId}-${project.version}</finalName>    <!-- 编译打包的文件名称 -->        <testOutputDirectory>${project.build.directory}/test-classes</testOutputDirectory>    <!-- 测试编译后的文件输出位置  -->        <sourceDirectory>${project.basedir}/src/main/java</sourceDirectory>    <!-- 需要编译源文件存放位置 -->        <scriptSourceDirectory>src/main/scripts</scriptSourceDirectory>    <!-- 需要编译脚本源文件存放位置 -->        <testSourceDirectory>${project.basedir}/src/test/java</testSourceDirectory>    <!-- 编译的测试源文件存放位置  -->        <resources>     <!-- 指定了工程的资源存放位置,这些资源不会被编译但是她是捆绑在这个项目中的,如项目要求的配置文件configuration.xml等,这些文件需要在打包同时要打入包中 -->            <resource>                <directory>${project.basedir}/src/main/resources</directory>    <!-- 工程资源存放位置  -->                <targetPath>META-INF/plexus</targetPath>    <!-- 打包后资源的位置 -->                <filtering>false</filtering>                <includes>         <!-- 指定需要打包的资源 -->                    <include>configuration.xml</include>                </includes>                <excludes>    <!-- 指定不需要打包的资源 -->                    <exclude>**/*.properties</exclude>                </excludes>            </resource>        </resources>        <!--        resources:  is a list of resource elements that each describe what and where to include files associated with this project.        targetPath: Specifies the directory structure to place the set of resources from a build. Target path defaults to the base directory. A commonly specified target path for resources that will be packaged in a JAR is META-INF.        filtering:  is true or false, denoting if filtering is to be enabled for this resource. Note, that filter *.properties files do not have to be defined for filtering to occur - resources can also use properties that are by default defined in the POM (such as ${project.version}), passed into the command line using the "-D" flag (for example, "-Dname=value") or are explicitly defined by the properties element. Filter files were covered above.        directory:  This element's value defines where the resources are to be found. The default directory for a build is ${basedir}/src/main/resources.        includes:   A set of files patterns which specify the files to include as resources under that specified directory, using * as a wildcard.        excludes:   The same structure as includes, but specifies which files to ignore. In conflicts between include and exclude, exclude wins.        testResources: The testResources element block contains testResource elements. Their definitions are similar to resource elements, but are naturally used during test phases. The one difference is that the default (Super POM defined) test resource directory for a project is ${basedir}/src/test/resources. Test resources are not deployed.        -->        <testResources>            <testResource>                <directory>${project.basedir}/src/test/resources</directory>    <!-- 编译配置 -->            </testResource>        </testResources>        <filters>            <filter>filters/filter1.properties</filter>        </filters>        <plugins>   <!-- 指定使用的插件列表 -->            <plugin>    <!-- 指定一个插件  -->                <artifactId>maven-antrun-plugin</artifactId>                <version>1.1</version>                <!--                dependencies:   Dependencies are seen a lot within the POM, and are an element under all plugins element blocks. The dependencies have the same structure and function as under that base build. The major difference in this case is that instead of applying as dependencies of the project, they now apply as dependencies of the plugin that they are under. The power of this is to alter the dependency list of a plugin, perhaps by removing an unused runtime dependency via exclusions, or by altering the version of a required dpendency. See above under Dependencies for more information.                executions:     It is important to keep in mind that a plugin may have multiple goals. Each goal may have a separate configuration, possibly even binding a plugin's goal to a different phase altogether. executions configure the execution of a plugin's goals.                                For example, suppose you wanted to bind the antrun:run goal to the verify phase. We want the task to echo the build directory, as well as avoid passing on this configuration to its children (assuming it is a parent) by setting inherited to false. You would get an execution like this:                id:             Self explanatory. It specifies this execution block between all of the others. When the phase is run, it will be shown in the form: [plugin:goal execution: id]. In the case of this example: [antrun:run execution: echodir]                goals:          Like all pluralized POM elements, this contains a list of singular elements. In this case, a list of plugin goals which are being specified by this execution block.                phase:          This is the phase that the list of goals will execute in. This is a very powerful option, allowing one to bind any goal to any phase in the build lifecycle, altering the default behavior of Maven.                inherited:      Like the inherited element above, setting this false will supress Maven from passing this execution onto its children. This element is only meaningful to parent POMs.                configuration:  Same as above, but confines the configuration to this specific list of goals, rather than all goals under the plugin.-->                <executions>                    <execution>                        <id>echodir</id>                        <goals>                            <goal>run</goal>                        </goals>                        <phase>verify</phase>                        <inherited>false</inherited>                        <configuration>                            <tasks>                                <echo>Build Dir: ${project.build.directory}</echo>                            </tasks>                        </configuration>                    </execution>                    <!-- 以上execution解读: 绑定一个antrun:run的goal到verify阶段,任务是输出编译的目录,避免把这个配置传到子pom中配置inherited为false -->                </executions>            </plugin>        </plugins>        <pluginManagement>  <!-- 插件管理 -->            <!-- NOTE: These plugins will be removed from future versions of the super POM -->            <!-- They are kept for the moment as they are very unlikely to conflict with lifecycle mappings (MNG-4453) -->            <plugins>                <plugin>    <!-- 编译配置 -->                    <groupId>org.apache.maven.plugins</groupId>                    <artifactId>maven-jar-plugin</artifactId>                    <version>2.0</version>                    <extensions>false</extensions>  <!-- 是否加载插件的扩展,值:true|false 默认是false -->                    <inherited>true</inherited> <!-- 这个插件的configuration(配置)是否要施加到继承该pom的子pom上 值:true|false 默认true -->                    <configuration>   <!-- 对单个插件指定配置,不需要深入了解这个插件怎么工作的,换句话说就是这个插件期望的属性都可以在这里指定,                    如果该pom是一个父级pom,这个配置也会被继承,不管是在 build/plugins 还是在 pluginManagement中,如果子pom的插件中设置了该configuration,                      那么子pom中的插件配置值就是有效的,子pom中没有设置,父级中配置了父级的就是有效的-->                        <classifier>test</classifier> <!-- -->                    </configuration>                    <dependencies></dependencies>                    <executions></executions>                </plugin>                <plugin>                    <artifactId>maven-release-plugin</artifactId>                    <version>2.0</version>                </plugin>            </plugins>        </pluginManagement>    </build>    <reporting>    <!-- 包含了site阶段相应地元素,maven会生成一个定义和配置在reporting元素下的报告,比如:JavaDoc报告,像build中配置插件一样的,reporting掌握了相同的能力     明显的不同是build中插件的goal配置在executions块中,reporting是配置在reportSet元素中,稍微的不同是reporting下的configuration扮演了build插件的configuration     相反则不然,也就是build插件的配置(configuration)不会影响reporting插件 -->        <outputDirectory>${project.build.directory}/site</outputDirectory>    <!-- 文档输出位置 -->        <!-- 一个非常重要的是一个插件有很多goal,每个goal可能有分开的配置(configuration),reportSets配置一个report插件目标的执行,相同就是build的execution则不能绑定一个报告到其他阶段 -->        <plugins>            <plugin>                <artifactId>maven-project-info-reports-plugin</artifactId>                <version>2.0.1</version>                <reportSets>                    <reportSet>                        <id>sunlink</id>                        <reports>                            <report>javadoc</report>                        </reports>                        <inherited>true</inherited>                        <configuration>                            <links>                                <link>http://java.sun.com/j2se/1.5.0/docs/api/</link>                            </links>                        </configuration>                    </reportSet>                    <!--                    上述reportSet解读:假如你想配置javadoc:javadoc这个goal来连接到http://java.sun.com/j2se/1.5.0/docs/api/,仅仅只是javadoc(不是maven-javadoc-plugin:jar目标)                    同时想把这个配置传递给它的child,就设置inherited为true                     -->                </reportSets>            </plugin>        </plugins>    </reporting>    <profiles>        <!-- NOTE: The release profile will be removed from future versions of the super POM -->        <profile>            <id>test</id>            <activation>                <activeByDefault>false</activeByDefault>                <jdk>1.5</jdk>          <!-- jdk版本 -->                <os>       <!-- 操作系统 -->                    <name>Windows XP</name>                    <family>Windows</family>                    <arch>x86</arch>                    <version>5.1.2600</version>                </os>                <property>                    <name>sparrow-type</name>                    <value>African</value>                </property>                <file>  <!-- 给定的文件可能功能文件的existence来激活这个profile -->                    <exists>${basedir}/file2.properties</exists>                    <missing>${basedir}/file1.properties</missing>                </file>            </activation>            <!-- 备注:activation元素不是profile被激活的唯一方式,settings.xml文件中的activeProfile元素可能包含了这个profile的id,通过命令工具它们也可能被激活 -->            <!--<build>...</build>-->            <!--<modules>...</modules>-->            <!--<repositories>...</repositories>-->            <!--<pluginRepositories>...</pluginRepositories>-->            <!--<dependencies>...</dependencies>-->            <!--<reporting>...</reporting>-->            <!--<dependencyManagement>...</dependencyManagement>-->            <!--<distributionManagement>...</distributionManagement>-->        </profile>        <!--        profile 4.0的一个新特性就是工程根据环境来改变设置,这个环境就是那里将要被编译。profile包含了可选的activation和对pom作出的该组变化,如果这个profile可以被激活的话        如:为测试环境编译就可能需要指定一个不同的数据库,然后发布。或者依赖的包根据不同的jdk版本来从仓库中获取        -->        <profile>            <id>release-profile</id>            <activation>     <!-- activation是profile的关键,profile的能力就是根据确定的情况修改基础的pom的能力,这些情形可以在activation元素中指定,一个和多个指定的标准被遇到是activation就会发生,但正数第一个被遇见时,处理停止并且这个profile被标记为激活-->                <property>    <!-- 编译配置 -->                    <name>performRelease</name>    <!-- 编译配置 -->                    <value>true</value>    <!-- 编译配置 -->                </property>            </activation>            <build>                <plugins>                    <plugin>                        <inherited>true</inherited>                        <artifactId>maven-source-plugin</artifactId>                        <executions>                            <execution>                                <id>attach-sources</id>                                <goals>                                    <goal>jar</goal>                                </goals>                            </execution>                        </executions>                    </plugin>                    <plugin>                        <inherited>true</inherited>                        <artifactId>maven-javadoc-plugin</artifactId>                        <executions>                            <execution>                                <id>attach-javadocs</id>                                <goals>                                    <goal>jar</goal>                                </goals>                            </execution>                        </executions>                    </plugin>                    <plugin>                        <inherited>true</inherited>                        <artifactId>maven-deploy-plugin</artifactId>                        <configuration>                            <updateReleaseInfo>true</updateReleaseInfo>                        </configuration>                    </plugin>                </plugins>            </build>        </profile>    </profiles></project>
资料参考:http://maven.apache.org/pom.html

0 0