明确一下maven具体能帮助我们做些什么
- 项目构建
- 依赖管理
- 模块化开发
- 代码测试
- 项目信息管理
- 持续集成
maven的下载安装
1:windows 系统解压maven项目到指定目录 D:\sof\apache-maven-3.3.3-bin mven下载路径,
目录结构如下
2:解压完成之后需要进行简单的配置才可使用
环境变量配置 M2_HOME-> D:\sof\apache-maven-3.3.3-bin
环境变量path添加 ${M2_HOME}\bin
maven经过以上两步基本配置完成, cmd 控制台输入 mvn -version 查看maven版本信息
maven相关配置文件
settings.xml存在于两个地方:
1.安装的地方:$M2_HOME/conf/settings.xml (全局配置)
2.用户的目录:${user.home}/.m2/settings.xml(用户配置)
.m2的生成可使用mvn命令
setting相关配置
<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
本地仓库目录配置
<localRepository>C:\Users\x\.m2\repository</localRepository>
远程仓库认证信息配置
<servers>
<server>
<id>nexus2</id> 与仓库id对应
<username>admin</username>
<password>admin123</password>
</server>
</servers>
远程仓库镜像配置
<mirrors>
<mirror>
<id>external</id> 镜像id
<mirrorOf>external:*,central</mirrorOf>被镜像仓库id
<name>Human Readable Name for this Mirror.</name>
<url>http://xxxx:8081/nexus/content/groups/public/</url>
</mirror>
</mirrors>
仓库配置
<repositories>
<repository>
<id>admin</id> 仓库id
<name>Repositorynexus</name>
<url>http://xxxx:8081/nexus/content/groups/public/</url>
</repository>
</repositories>
自定义属相配置
<profiles>
<profile>
<id>dev</id> 配置ID
<properties>
<profiles.active>dev</profiles.active>
<finalName>web</finalName>
</properties>
<repositories>
<repository>
<id>admin</id>
<name>Repositorynexus</name>
<url>http://xxxx:8081/nexus/content/groups/public/</url>
</repository>
</repositories>
</profile>
<!--发布环境 -->
<profile>
<id>prod</id> 配置ID
<properties>
<profiles.active>dev</profiles.active>
<finalName>web</finalName>
</properties>
<repositories>
<repository>
<id>admin</id>
<name>Repositorynexus</name>
<url>http://xxxx:8081/nexus/content/groups/public/</url>
</repository>
</repositories>
</profile>
</profiles>
<activeProfiles>
<activeProfile>dev</activeProfile> 启用自定义属相配置 根据ID
</activeProfiles>
</settings>
仓库镜像配置
<mirrorOf>external:*</mirrorOf> 匹配所有不在本机上的远程仓库(使用localhost 和 file://协议的除外)
<mirrorOf>*</mirrorOf> 匹配所有远程仓库
<mirrorOf>repo1,repo2</mirrorOf> 对rpo1和repo2进行镜像,使用逗号分隔,rep1的为仓库ID
<mirrorOf>*,!repo2</mirrorOf> 匹配所有远程仓库,repo2除外,使用感叹号将仓库从匹配中排除
仓库分类
- 本地仓库
- 远程仓库(私服 中央仓库 其他公共仓库都输入远程仓库)
- 私服
- 中央仓库
(apache-maven-3.5.2\lib -> maven-model-builder-3.5.2.jar -> org\apache\maven\model\pom-4.0.0.xml(超级pom))
<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>
- 其他公共仓库
所有pom(项目对象模型都继承超级pom) 从超级pom中可看出maven约定项目的目录结构如下
<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->
<!-- START SNIPPET: superpom -->
<project>
<modelVersion>4.0.0</modelVersion>
<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 -->
超级pom中已经默认定义了项目的基本结构
<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>测试代码目录
${project.build.directory} = <directory>${project.basedir}/target</directory>
${project.basedir} = 项目目录
约定的项目目录结构
src
main
java
test
java
target
${project.artifactId}-${project.version}.jar/war...
class
test-class
pom