maven-pom

来源:互联网 发布:js ul li 事件委托 编辑:程序博客网 时间:2024/05/22 14:15

maven的scope

compile:(默认的范围)编译,测试,运行

provided:编译,测试的时候有效,在运行时不会被加入 如:servlet api

runtime:在测试,运行时有效;如jdbc驱动包

test:在测试时有效

system:在编译,测试时有效(和provided相似),和系统相关联,可移埴性差

import: 用在dependencyManagent中。如:把a中的依赖导入b 中

dependencyManagent

一般定义在父模块中用于定义常用的依赖,maven并不会真正去加载,给子模块继承用。在子模块中定义时才会真正加载。例如:

<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/maven-v4_0_0.xsd">  <modelVersion>4.0.0</modelVersion>  <groupId>com.phl.test</groupId>  <artifactId>test</artifactId>  <packaging>pom</packaging>  <version>1.0-SNAPSHOT</version>  <name>test Maven Webapp</name>  <url>http://maven.apache.org</url>  <modules>    <module>loadresources</module>    <module>designmodel</module>    <module>javacase</module>  </modules>  <dependencyManagement>    <dependencies>      <dependency>        <groupId>junit</groupId>        <artifactId>junit</artifactId>        <version>3.8.1</version>        <scope>test</scope>      </dependency>      <dependency>        <groupId>org.springframework</groupId>        <artifactId>spring-context</artifactId>        <version>4.2.0.RELEASE</version>      </dependency>      <dependency>        <groupId>org.springframework</groupId>        <artifactId>spring-core</artifactId>        <version>4.2.0.RELEASE</version>      </dependency>    </dependencies>  </dependencyManagement>  <build>    <finalName>test</finalName>  </build></project>

build plugins

在build的时候执行插件.

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

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

在build里面列出要执行的插件列表

<build>    <finalName>${app.warFile}</finalName>    <resources>        <resource>            <directory>src/main/resources</directory>            <filtering>true</filtering>        </resource>        <resource>            <directory>src/main/resources-${profile.name}</directory>            <filtering>true</filtering>        </resource>    </resources>    <plugins>        <plugin>            <groupId>org.codehaus.mojo</groupId>            <artifactId>build-helper-maven-plugin</artifactId>            <version>1.9.1</version>            <executions>                <execution>                    <id>add-resource</id>                    <phase>generate-resources</phase>                    <goals>                        <goal>add-resource</goal>                    </goals>                    <configuration>                        <resources>                            <resource>                                <directory>src/main/resources-${profile.name}</directory>                            </resource>                        </resources>                    </configuration>                </execution>            </executions>        </plugin>        <plugin>            <groupId>org.apache.tomcat.maven</groupId>            <artifactId>tomcat7-maven-plugin</artifactId>            <version>2.2</version>            <configuration>                <path>/</path>                <uriEncoding>utf-8</uriEncoding>                <port>${app.port}</port>                <server>tomcat</server>                <systemProperties>                    <systemProperty>                        <name>JAVA_OPTS</name>                        <value>-Xms256m -Xmx768m -XX:+UseConcMarkSweepGC -XX:MaxPermSize=256m -XX:NewRatio=6                            -XX:+CMSClassUnloadingEnabled -XX:+CMSPermGenSweepingEnabled                        </value>                    </systemProperty>                </systemProperties>            </configuration>        </plugin>    </plugins></build>

build lifecycle & build phase & goal

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

  • validatevalidate the project is correct and all necessary information is available.
  • initializeinitialize build state, e.g. set properties or create directories.
  • generate-sourcesgenerate any source code for inclusion in compilation.
  • process-sourcesprocess the source code, for example to filter any values.
  • generate-resourcesgenerate resources for inclusion in the package.
  • process-resourcescopy and process the resources into the destination directory, ready for packaging.
  • compilecompile the source code of the project.
  • process-classespost-process the generated files from compilation, for example to do bytecode enhancement on Java classes.
  • generate-test-sourcesgenerate any test source code for inclusion in compilation.
  • process-test-sourcesprocess the test source code, for example to filter any values.
  • generate-test-resourcescreate resources for testing.
  • process-test-resourcescopy and process the resources into the test destination directory.
  • test-compilecompile the test source code into the test destination directory
  • process-test-classespost-process the generated files from test compilation, for example to do bytecode enhancement on Java classes. For Maven 2.0.5 and above.
  • testrun tests using a suitable unit testing framework. These tests should not require the code be packaged or deployed.
  • prepare-packageperform any operations necessary to prepare a package before the actual packaging. This often results in an unpacked, processed version of the package. (Maven 2.1 and above)
  • packagetake the compiled code and package it in its distributable format, such as a JAR.
  • pre-integration-testperform actions required before integration tests are executed. This may involve things such as setting up the required environment.
  • integration-testprocess and deploy the package if necessary into an environment where integration tests can be run.
  • post-integration-testperform actions required after integration tests have been executed. This may including cleaning up the environment.
  • verifyrun any checks to verify the package is valid and meets quality criteria.
  • installinstall the package into the local repository, for use as a dependency in other projects locally.
  • deploydone in an integration or release environment, copies the final package to the remote 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 clean dependency: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。



0 0