Maven概述

Maven下载地址maven.apache.org/

Maven安装及配置

将下载的压缩包解压,并配置环境变量css

  1. 新建环境变量M2_HOME,变量值为:Maven解压路径;
  2. 在环境变量PATH中,在结尾处新增%M2_HOME%\bin;
  3. 在DOS窗口中,执行mvn -v命令查看是否配置成功

代理配置

在~/.m2/setting.xml中进行配置,配置以下:java

<proxies>
    <proxy>
        <!-- 惟一标识 -->
        <id></id>
        <!-- 是否激活 boolean -->
        <active></active>
        <!-- 代理协议,一般使用HTTP -->
        <protocol></protocol>
        <!-- 主机名 -->
        <host></host>
        <!-- 端口 -->
        <port></port>
        <!-- 认证用户名 -->
        <username></username>
        <!-- 认证密码 -->
        <password></password>
        <!-- 设置不须要代理的域名 -->
        <nonProxyHosts></nonProxyHosts>
    </proxy>
</proxies>
复制代码

Maven仓库

中央仓库地址: repo1.maven.org/maven2
设置本地仓库地址web

在setting.xml中设置
<localRepository></localRepository>
复制代码

远程仓库配置请参考代码 远程仓库认证信息配置以下:apache

在setting.xml中配置
<servers>
    <server>
        <!-- pom文件中远程仓库的ID -->
        <id></id>
        <!-- 用户名 -->
        <username></username>
        <!-- 密码 -->
        <password></password>
    </server>
</servers>
复制代码

Maven镜像

如需配置镜像服务,则在setting.xml中配置以下app

<mirrors>
    <mirror>
        <!-- 镜像地址惟一标识 -->
        <id></id>
        <!-- 镜像描述 -->
        <name></name>
        <!-- 镜像地址 -->
        <url></url>
        <!-- 镜像代理地址,经过ID表示,*表示全部 -->
        <mirrorOf></mirrorOf>
    </mirror>
</mirros>
复制代码

Maven元素

<project>
    <!-- 固定值 -->
    <modelVersion>4.0.0</modelVersion>
    <!-- 所属项目,必选 -->
	<groupId>com.xxx</groupId>
	<!-- 模块名称,必选 -->
	<artifactId>xxx</artifactId>
	<!-- 版本,SNAPSHOT表示快照,必选 -->
	<version>0.0.1-SNAPSHOT</version>
	<!-- 打包方式,默认为jar,可选 -->
	<packaging>jar</packaging>
    
	<name></name>
	<url>http://maven.apache.org</url>
        <!-- 若是须要引用父maven,则使用以下标签 -->
	<parent>
            <groupId></groupId>
            <artifactId></artifactId>
	    <version></version>
	    <!-- 父类pom文件相对位置,默认为../pom.xml,即在上一层目录中 -->
	    <relativePath></relativePath>
	</parent>
        <!-- 属性 -->
	<properties>
	    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>
	<dependencies>
	    <dependency>
            <!-- 依赖的jar包groupId -->
	        <groupId></groupId>
		<!-- 依赖的jar包artifactId -->
		<artifactId></artifactId>
		<!-- 依赖的jar包版本号 -->
		<version></version>
		<!-- 依赖的范围 -->
		<scope></scope>
		<!-- 标记依赖是否可选,true表示该依赖只对当前项目有效,不会传递 -->
		<optional></optional>
		<!-- 排除传递依赖 -->
		<exclusions>
		    <exclusion>
		        <groupId></groupId>
			<artifactId></artifactId>
		    </exclusion>
		</exclusions>
	    </dependency>
	</dependencies>
        <!-- 远程仓库配置 -->
	<repositories>
	    <repository>
                <!-- 远程仓库惟一标识 -->
		<id></id>
		<!-- 远程仓库描述 -->
		<name></name>
		<!-- 远程仓库地址 -->
		<url></url>
		<!-- 远程仓库发布版本下载支持 -->
		<releases>
		    <enabled>true</enabled>
		</releases>
		<!-- 远程仓库快照版本下载支持 -->
		<snapshots>
		    <enabled>false</enabled>
		</snapshots>
	    </repository>
	</repositories>
	<!-- 远程插件仓库配置 -->
	<pluginRepositories>
	    <pluginRepository>
		<id></id>
		<name></name>
		<url></url>
	    </pluginRepository>
	</pluginRepositories>
	<build>
	    <plugins>
		<plugin>
                    <groupId>org.apache.maven.plugins</groupId>
		    <artifactId>maven-compiler-plugin</artifactId>
		    <version>3.5.1</version>
		    <configuration>
		        <source>1.7</source>
			<target>1.7</target>
			<encoding>UTF-8</encoding>
		    </configuration>
	        </plugin>
	    </plugins>
        </build>
</project>
复制代码

Maven资源过滤及构建

便可以使用Maven中的属性值替换配置文件中的值webapp

Maven属性

  • 内置属性,示例
    ${basedir}:项目根目录
    ${version}:项目版本号
  • pom属性,示例
    ${project.build.sourceDirectory}:项目主源码目录
    ${project.artifactId}:项目的artifactId
  • 自定义属性
  • Java系统属性,经过mvn help:system查看全部的Java系统属性
  • 环境变量属性,以env.开头的属性引用
  • setting属性,经过settings.开头引用xml元素的值

Maven资源过滤

在pom.xml中配置以下maven

<!-- 资源文件过滤 -->
<build>
    <resources>
        <resource>
            <!-- 资源路径 -->
            <directory></directory>
            <filtering>true</filtering>
            <!-- 排除的资源文件 -->
            <excludes>
	        <exclude>src/main/filter</exclude>
	    </excludes>
        </resource>
    </resources>
</build>
<!-- 针对不一样环境的不一样配置 -->
<profiles>
    <profile>
        <id>dev</id>
        <!-- 配置的属性值 -->
        <properties></properties>
        <!-- 默认激活 -->
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <!-- 针对单个profile资源过滤 -->
        <build>
            <filters>
	        <filter>src/main/filter/dev_filter.properties</filter>
	    </filters>
        </build>
    </profile>
</profiles>
复制代码

Maven聚合

新建项目,之包含pom.xml,其中<packaging>元素设置为pom,在聚合中,最主要的元素为ui

<modules>
    <!-- 聚合的模块名称 -->
    <module></module>
        ...
    <module></module>
</modules>
复制代码

Maven继承

继承与聚合同样,<packaging>元素设置为pom,子pom若是须要继承父pom中的元素,则在子pom中配置<parent>元素,具体参考配置,可继承的元素以下:url

  • groupId 项目组ID
  • version 版本信息
  • description 项目描述
  • organization 项目组织信息
  • inceptionYear 创始年份
  • url 项目Url地址
  • developers 开发者信息
  • contributors 贡献者信息
  • distributionManagement 部署配置
  • issueManagement 缺陷跟踪系统信息
  • ciManagement 持续集成系统信息
  • scm 版本控制系统信息
  • mailingLists 邮件列表信息
  • properties 自定义属性
  • dependencies 依赖配置
  • dependencyManagement 依赖管理配置
  • repositories 仓库配置
  • build 项目构建信息
  • reporting 项目报告输出配置

在父模块中,元素<dependencyManagement>既能让子模块继承到父模块的依赖配置,又能保证子模块依赖使用的灵活性,由于该元素下的依赖声明不会引入实际的依赖,不过它可以约束dependencies的依赖使用,即子类要重写父依赖的groupId和artifactId元素spa

Maven经常使用插件

编译插件

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <!-- 支持的java版本 -->
                <source>1.7</source>
                <target>1.7</target>
                <encoding>UTF-8</encoding>
            </configuration>
        </plugin>
        <plugin>
	    <groupId>org.apache.maven.plugins</groupId>
	    <artifactId>maven-resources-plugin</artifactId>
	    <version>2.6</version>
	    <configuration>
	        <encoding>UTF-8</encoding>
	    </configuration>
        </plugin>
        <!-- 对web资源进行过滤 -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.1-beta-1</version>
            <configuration>
                <webResources>
                    <resource>
                        <filtering>true</filtering>
                        <directory>src/main/webapp</directory>
                        <!-- 过滤的目录 -->
                        <includes>
                            <include>* */*.css</include>
                            <include>* */*.js</include>
                        </includes>
                    </resource>
                </webResources>
            </configuration>
        </plugin>
    </plugins>
</build>
复制代码

Maven经常使用命令

mvn dependency:list 查看当前项目已解析依赖
mvn dependency:tree 查看当前项目依赖树
mvn dependency:analyze 分析当前项目依赖树\

Maven私服全局配置

在setting.xml配置镜像,同时配置私服地址以下

<profiles>
    <profile>
        <id>nexus</id>
        <!-- 以下配置请参考Maven元素远程仓库配置 -->
        <repository>
        </repository>
    </profile>
</profiles>
<!-- 激活的私服ID -->
<activeProfiles>
    <activeProfile>nexus</activeProfile>
</activeprofiles>
复制代码
相关文章
相关标签/搜索