maven系列(一)

来源:互联网 发布:开源的java开发平台 编辑:程序博客网 时间:2024/06/01 08:33

maven介绍

maven,依地语意思是 知识的积累,maven的首要目标就是让开发者在短时间内掌握开发的全过程,它致力于以下几个目标:

构建过程更简单

maven确实隐藏了很多细节。(但是它的运行机制还是要掌握的。)

提供统一的构建系统

maven让一个工程通过 项目对象模型(project object model )和一系列公共的插件,提供一个统一的构建系统。

提供实战开发的准则

比如,说明、执行、报告单元测试是maven一般构建的一部分,它有一下几个原则:

  • 把测试代码放在一个分离的并行的代码树中
  • 使用测试用例的命名约定来定位和执行测试
  • 测试用例设置他们的环境

maven帮助管理项目工作流(比如版本管理,问题追踪)
maven也认为一个项目应该有标准的目录结构,以便更快理解其他类似结构的项目。


maven安装

maven下载地址,个人推荐二进制版本,解压即用。
maven的运行需要JDK的支持,确保已经配置了 JAVA_HOME,别忘了配置 M2_HOMEPATH,不再赘述。
注意:maven的版本和JDK的版本不是越高越好,和你所在项目线上环境的maven版本和JDK版本保持一致,不然可能会导致jar冲突等问题


maven的配置详解

MAVEN_OPTS

在环境变量中设置MAVEN_OPTS,这是jvm运行Maven的选项,比如可以设置成-Xms256m -Xmx512m

settings.xml

分为两种:

  • 全局settings.xml:$M2_HOME/conf/settings.xml。对本机上的所有项目有效。
  • 当前用户settings.xml:${user.home}/.m2/settings.xml。对当前用户的所有项目有效。

两者都存在的情况下,会进行一次merge,用户配置优先级更高。在默认的settings.xml配置文件中有所有配置项都有详细说明,可以很快根据自己需求进行配置。

<settings >  <localRepository/> ##本地仓库配置  <interactiveMode/>##是否交互,等待用户输入或者直接使用默认值  <usePluginRegistry/>##  <offline/>##是否尝试联网  <pluginGroups/>##自动加插件名前缀,比如做 mvn dependency:tree 实际上自动加上了org.apache.maven.plugins。  <servers/>## site的目标服务器  <mirrors/>##远程仓库的镜像  <proxies/>## 配置代理  <profiles/>## 个性配置,每个profile有自己的唯一id和激活条件  <activeProfiles/>##永久激活的个性配置</settings>

关于maven自带的属性,会在下一篇文章讲pom.xml配置时详细讲解。

超级pom.xml

项目的pom文件继承于超级pom文件。

<!-- START SNIPPET: superpom --><project>  <modelVersion>4.0.0</modelVersion><!-- jar包远程仓库 -->  <repositories>    <repository>      <id>central</id>      <name>Central Repository</name>      <url>https://repo.maven.apache.org/maven2</url>      <layout>default</layout>      <snapshots>        <enabled>false</enabled>      </snapshots>    </repository>  </repositories><!-- 插件包远程仓库 -->  <pluginRepositories>    <pluginRepository>      <id>central</id>      <name>Central Repository</name>      <url>https://repo.maven.apache.org/maven2</url>      <layout>default</layout>      <snapshots>        <enabled>false</enabled>      </snapshots>      <releases>        <updatePolicy>never</updatePolicy>      </releases>    </pluginRepository>  </pluginRepositories><!-- 项目目录结构配置 -->  <build>    <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>${project.basedir}/src/main/scripts</scriptSourceDirectory>    <testSourceDirectory>${project.basedir}/src/test/java</testSourceDirectory>    <resources>      <resource>        <directory>${project.basedir}/src/main/resources</directory>      </resource>    </resources>    <testResources>      <testResource>        <directory>${project.basedir}/src/test/resources</directory>      </testResource>    </testResources>    <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>          <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.8</version>        </plugin>        <plugin>          <artifactId>maven-release-plugin</artifactId>          <version>2.3.2</version>        </plugin>      </plugins>    </pluginManagement>  </build>  <reporting>    <outputDirectory>${project.build.directory}/site</outputDirectory>  </reporting>  <profiles>    <!-- NOTE: The release profile will be removed from future versions of the super POM -->    <profile>      <id>release-profile</id>      <activation>        <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><!-- END SNIPPET: superpom -->
0 0
原创粉丝点击