🤖 AI文章摘要 qwen-turbo-latest
加载中...

Maven简介

  maven是一个项目管理和理解工具, 基于项目对象模型(POM, project object model)提供项目构建、文档、报告、依赖管理等功能。

maven的配置读取

maven会自动根据mvn命令定位MAVEN_HOME的路径值, 查找配置文件顺序是先: 用户配置->全局配置

  • 全局settings.xml: $MAVEN_HOME/conf/settings.xml
  • 用户settings.xml: $HOME/.m2/settings.xml

settings.xml文件结构

settings.xml文件的xml约束

  <?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.2.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.2.0 https://maven.apache.org/xsd/settings-1.2.0.xsd">
    <!--> config your serttings here <-->
</settings>
  

本地仓库路径配置, 默认使用用户路径${user.home}/.m2/repository

  <localRepository>/path/to/local/repo</localRepository>
  

交互模式(未理解)

This will determine whether maven prompts you when it needs input. If set to false, maven will use a sensible default value, perhaps based on some other setting, for the parameter in question. Default: true

  <interactiveMode>true</interactiveMode>
  

未理解

Determines whether maven should attempt to connect to the network when executing a build. This will have an effect on artifact downloads, artifact deployment, and others. Default: false

  <offline>false</offline>
  

This is a list of additional group identifiers that will be searched when resolving plugins by their prefix, i.e. when invoking a command line like “mvn prefix:goal”. Maven will automatically add the group identifiers “org.apache.maven.plugins” and “org.codehaus.mojo” if these are not already contained in the list.

  <pluginGroups>com.your.plugins</pluginGroups>
  

未理解

This is a list of proxies which can be used on this machine to connect to the network. Unless otherwise specified (by system property or command-line switch), the first proxy specification in this list marked as active will be used.

  <proxies>
    <proxy>
    <!-->Specification for one proxy, to be used in connecting to the network.<-->
        <id>optional</id>
        <active>true</active>
        <protocol>http</protocol>
        <username>proxyuser</username>
        <password>proxypass</password>
        <host>proxy.host.net</host>
        <port>80</port>
        <nonProxyHosts>local.netsome.host.com</nonProxyHosts>
    </proxy>
</proxies>
  

This is a list of authentication profiles, keyed by the server-id used within the system. Authentication profiles can be used whenever maven must make a connection to a remote server.
Specifies the authentication information to use when connecting to a particular server, identified by a unique name within the system (referred to by the ‘id’ attribute below).
NOTE: You should either specify username/password OR privateKey/passphrase, since these pairings are
used together.

  <servers>
    <server>
      <id>deploymentRepo</id>
      <username>repouser</username>
      <password>repopwd</password>
    </server>
    <server>
      <id>siteServer</id>
      <privateKey>/path/to/private/key</privateKey>
      <passphrase>optional; leave empty if not used.</passphrase>
    </server>
</servers>
  

This is a list of mirrors to be used in downloading artifacts from remote repositories. It works like this: a POM may declare a repository to use in resolving certain artifacts. However, this repository may have problems with heavy traffic at times, so people have mirrored it to several places. That repository definition will have a unique id, so we can create a mirror reference for that repository, to be used as an alternate download site. The mirror site will be the preferred server for that repository
mirror Specifies a repository mirror site to use instead of a given repository. The repository that this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used for inheritance and direct lookup purposes, and must be unique across the set of mirrors.

  <mirrors>
    <mirror>
        <id>aliyunmaven</id>
        <mirrorOf>*</mirrorOf>
        <name>阿里云公共仓库</name>
        <url>https://maven.aliyun.com/repository/public</url>
    </mirror>
</mirrors>
  

This is a list of profiles which can be activated in a variety of ways, and which can modify the build process. Profiles provided in the settings.xml are intended to provide local machine-specific paths and repository locations which allow the build to work in the local environment. For example, if you have an integration testing plugin - like cactus - that needs to know where your Tomcat instance is installed, you can provide a variable here such that the variable is dereferenced during the build process to configure the cactus plugin. As noted above, profiles can be activated in a variety of ways. One way - the activeProfiles section of this document (settings.xml) - will be discussed later. Another way essentially relies on the detection of a system property, either matching a particular value for the property, or merely testing its existence. Profiles can also be activated by JDK version prefix, where a value of ‘1.4’ might activate a profile when the build is executed on a JDK version of ‘1.4.2_07’. Finally, the list of active profiles can be specified directly from the command line. NOTE: For profiles defined in the settings.xml, you are restricted to specifying only artifact repositories, plugin repositories, and free-form properties to be used as configuration variables for plugins in the POM.

  <profiles>
    <profile>
        <id>env-dev</id>
        <activation>
        <property>
            <name>target-env</name>
            <value>dev</value>
            </property>
            </activation>
        <properties>
            <tomcatPath>/path/to/tomcat/instance</tomcatPath>
        </properties>
    </profile>
</profiles>
<activeProfiles>
    <activeProfile>alwaysActiveProfile</activeProfile>
    <activeProfile>anotherAlwaysActiveProfile</activeProfile>
</activeProfiles>
  

Complie任意位置都可使用, 会被编译到产品中. Test只能在test中使用, 不会编译到产品中. Provider任何地方都可以使用不会被编译到产品中.

mvn命令行工具

  • mvn dependency:resolve -Dclassifier=sources下载源码

maven项目结构

maven标准目录结构

  ├── pom.xml
└── src
    ├── main
    │   ├── java
    │   └── resources
    └── test
        ├── java
        └── resource
  

pom.xml文件结构

  <?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <!--> config your settings here <-->
</project>
  

当前项目坐标

  <groupId>com.rubitcat</groupId>
<artifactId>encloud-server</artifactId>
<version>1.0</version>
  

当前项目属性

  <properties>
    <project.build.sourceEncoding>UTF-8<project.build.sourceEncoding>
    <maven.compiler.source>8</maven.compiler.source>
    <maven.compiler.target>8</maven.compiler.target>
</properties>
  

项目插件

  <build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>3.3.2</version>
        </plugin>
    </plugins>
</build>
  

项目依赖

  <dependencies>
    <dependency>
        <groupId></groupId>
        <artifactId></artifactId>
        <version></version>
        <scope></scope>
    </dependency>
</dependencies>
  

项目经验

  • 项目划分哪些子系统(模块)
  • 模块间如何进行业务通信
  • 我负责哪个模块
  • 我所负责的模块包含哪些技术点
  • 开发过程中碰到过什么问题,以及其解决思路
  • 碰到的问题对项目开发构成哪些影响,以及对后续开发的的影响