为了更好的学习pom,可以在myeclipse下编辑pom文件。
准备工作
1、安装myeclipse的maven插件
2、在windows->preference->myeclipse->maven4myeclipse->Installations 默认为Embedded
为了使用自己安装maven 可以添加项External,并将路径指定到本地。
3、如果更改了本地的setting.xml文件,需要在......maven4myeclipse->user settings下 update Settings并Reindex,否则
本地修改在myelipse里面不生效。
pom文件
超级pom:在lib下maven-model-builder-3.3.9.jar 下有一个pom文件,定义了默认的仓库,及工程
的默认目录等。
有效pom:超级pom与自己工程pom叠加后的pom,在myeclipse打开pom文件后,有一个窗口为Effective pom即为有效pom.
<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>MavenGroup</groupId>
<artifactId>MavenProject1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>MavenProject1</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>
<!-- 指定下载jar包的位置 工程依赖的jar包-->
<repositories>
<!-- 默认的中央仓库 -->
<repository>
<snapshots>
<enabled>false</enabled>
</snapshots>
<id>central</id>
<name>Maven Repository Switchboard</name>
<url>http://repo1.maven.org/maven2</url>
</repository>
</repositories>
<!-- 工程运行时plugin的仓库-->
<pluginRepositories>
<pluginRepository>
<releases>
<updatePolicy>never</updatePolicy>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
<id>central</id>
<name>Maven Plugin Repository</name>
<url>http://repo1.maven.org/maven2</url>
</pluginRepository>
</pluginRepositories>
<!-- maven 构建的一个过程 -->
<build>
<!-- 指定资源的目录开始 -->
<sourceDirectory>E:\workup\workspace\ThinkingInJava\MavenProject1\src\main\java</sourceDirectory>
<scriptSourceDirectory>E:\workup\workspace\ThinkingInJava\MavenProject1\src\main\scripts</scriptSourceDirectory>
<testSourceDirectory>E:\workup\workspace\ThinkingInJava\MavenProject1\src\test\java</testSourceDirectory>
<outputDirectory>E:\workup\workspace\ThinkingInJava\MavenProject1\target\classes</outputDirectory>
<testOutputDirectory>E:\workup\workspace\ThinkingInJava\MavenProject1\target\test-classes</testOutputDirectory>
<resources>
<resource>
<directory>E:\workup\workspace\ThinkingInJava\MavenProject1\src\main\resources</directory>
</resource>
</resources>
<testResources>
<testResource>
<directory>E:\workup\workspace\ThinkingInJava\MavenProject1\src\test\resources</directory>
</testResource>
</testResources>
<directory>E:\workup\workspace\ThinkingInJava\MavenProject1\target</directory>
<finalName>MavenProject1-0.0.1-SNAPSHOT</finalName>
<!-- 子工程需要用到的plugin的定义,子工程只需要引用artifactId -->
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.3</version>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2-beta-5</version>
</plugin>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.1</version>
</plugin>
<plugin>
<artifactId>maven-release-plugin</artifactId>
<version>2.0</version>
</plugin>
</plugins>
</pluginManagement>
<!-- 当前工程运行时需要用到的plugin -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>2.4.1</version>
<executions>
<execution>
<id>default-clean</id>
<!-- 执行的阶段 -->
<phase>clean</phase>
<!-- 执行的目标 -->
<goals>
<goal>clean</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.3.1</version>
<executions>
<execution>
<id>default-install</id>
<phase>install</phase>
<goals>
<goal>install</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.4.3</version>
<executions>
<execution>
<id>default-resources</id>
<phase>process-resources</phase>
<goals>
<goal>resources</goal>
</goals>
</execution>
<execution>
<id>default-testResources</id>
<phase>process-test-resources</phase>
<goals>
<goal>testResources</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.7.1</version>
<executions>
<execution>
<id>default-test</id>
<phase>test</phase>
<goals>
<goal>test</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<executions>
<execution>
<id>default-testCompile</id>
<phase>test-compile</phase>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
<execution>
<id>default-compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.1</version>
<executions>
<execution>
<id>default-jar</id>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.5</version>
<executions>
<execution>
<id>default-deploy</id>
<phase>deploy</phase>
<goals>
<goal>deploy</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>2.0.1</version>
<executions>
<execution>
<id>default-site</id>
<phase>site</phase>
<goals>
<goal>site</goal>
</goals>
<configuration>
<outputDirectory>E:\workup\workspace\ThinkingInJava\MavenProject1\target\site</outputDirectory>
<reportPlugins>
<reportPlugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-project-info-reports-plugin</artifactId>
</reportPlugin>
</reportPlugins>
</configuration>
</execution>
<execution>
<id>default-deploy</id>
<phase>site-deploy</phase>
<goals>
<goal>deploy</goal>
</goals>
<configuration>
<outputDirectory>E:\workup\workspace\ThinkingInJava\MavenProject1\target\site</outputDirectory>
<reportPlugins>
<reportPlugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-project-info-reports-plugin</artifactId>
</reportPlugin>
</reportPlugins>
</configuration>
</execution>
</executions>
<configuration>
<outputDirectory>E:\workup\workspace\ThinkingInJava\MavenProject1\target\site</outputDirectory>
<reportPlugins>
<reportPlugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-project-info-reports-plugin</artifactId>
</reportPlugin>
</reportPlugins>
</configuration>
</plugin>
</plugins>
</build>
<!-- 生成站点文件的地方 -->
<reporting>
<outputDirectory>E:\workup\workspace\ThinkingInJava\MavenProject1\target\site</outputDirectory>
</reporting>
</project>
跳过测试
可以在项目右键 run as configuration 执行时勾选skip test,或者使用plugin(具体用到时,在详细查)
pom里面profile元素
可以覆盖所有pom里面的元素,通常放到最后,一定有一个id. 在mvn install时通过profile的id来进行激活。
<profiles>
<profile>
<id>profileid</id>
<!-- 会覆盖pom文件中的该dependency元素 -->
<dependencies>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<!-- 设定该依赖包在什么范围下生效 -->
<scope>test</scope>
</dependencies>
</profile>
</profiles>
pom元素的分类
项目构建:build reportind
关系:dependenucies dependencyManagement <build> <pluginManagement></pluginManagement></build>
构建环境:profile