一.Maven相关问题

安装jar到本地仓库

mvn install:install-file -Dfile=jar路径 -DgroupId=组id -DartifactId=组件id

-Dversion=安装版本 -Dpackaging=jar

默认使用C:\Users\用户名\.m2中的setting文件,如果没有则使用maven安装地址conf目录下的setting文件,安装到配置文件中定义的路径

Maven设置镜像为本地文件

<mirror>
<id>central</id>
<mirrorOf>*</mirrorOf>
<name>central</name>
<url>file:///E:/maven-project/repo</url>

</mirror>

linux同样认此协议

<url>file:///root/.m2/repo</url>

Maven引入本地jar包

<dependency>
   <groupId>com.aspose</groupId>
   <artifactId>aspose-words</artifactId>
   <version>14.9.0</version>
   <scope>system</scope>
   <systemPath>${project.basedir}/src/main/webapp/WEB-INF/lib/itextpdf-5.5.9.jar</systemPath>
</dependency>

文件协议file///

打开本地或网络驱动器上的文件。

句法

file:///sDrives[|sFile]

sDrives

指定本地或网络驱动器。

sFile

可选。指定要打开的文件。如果省略sFile并且访问驱动器的帐户有权浏览目录,则会显示可访问文件和目录的列表。

文件协议和sDrives参数可以省略,只替换为驱动器号和文件位置的命令行表示。例如,要浏览 My Documents 目录,可以将文件协议指定为 file:///C|/My Documents/ 或 C:\My Documents\。此外,单个“\”相当于指定主本地驱动器上的根目录。在大多数计算机上,这是 C:\。

下面的示例演示了四种使用 File 协议的方法。

//Specifying a drive and a file name.

file:///C|/My Documents/ALetter.html

//Specifying only a drive and a path to browse the directory.

file:///C|/My Documents/

//Specifying a drive and a directory using the command line representation of the directory location.

C:\My Documents\

//Specifying only the directory on the local primary drive.

\My Documents\

file协议只能在本地访问

本地搭建http服务器开放端口后他人也可以通过http访问到你电脑中的文件,但是file协议做不到

file协议对应有一个类似http的远程访问,就是ftp协议,即文件传输协议。

file协议无法实现跨域

https://docs.microsoft.com/en-us/previous-versions//aa767731(v=vs.85)?redirectedfrom=MSDN

_remote.repositories

_remote.repositories的作用是当maven本地仓库缓存了jar/pom的情况下修改了maven的配置文件(settings.xml)后依然会去远程仓库获取。

也就是说,_remote.repositories文件,标示该资源的来源,如果你有这个_remote.repositories,那就会在访问本地的同时,必须确保远程上有才行(这里的远程是setting文件中配置的镜像或远程仓库,用id来进行关联),否则就会报错;

如果文件从私服下载,后因为网络问题连不上私服,可以把_remote.repositories删除,然后重新Reload All Maven Project,刷新下项目,重启idea

以org.slf4j:slf4j-api-1.5.6.jar为例,本地仓库中的_remote.repositories文件内容如下:

#NOTE: This is a Maven Resolver internal implementation file, its format can be changed without prior notice.

#Thu Jan 28 21:37:13 CST 2021

slf4j-api-1.5.6.jar>alimaven=

slf4j-api-1.5.6.jar>central=

slf4j-api-1.5.6.pom>alimaven=

slf4j-api-1.5.6.pom>central=

其中alimaven是我的maven配置文件中的私服id。

https://blog.csdn.net/lovepeacee/article/details/103094247

其他

xxx.sha1是校验文件,xxx.pom是管理描述文件

lastUpdated

Maven在下载仓库中找不到相应资源时,会生成一个.lastUpdated为后缀的文件

假设我们更换了私服地址,并且该私服不存在该资源,那么就会生成xxxx.lastUpdated

eclipse遇到的问题

一、应用场景

项目在内网构建,为了能使用maven2,只能使用本地仓库依赖策略。

但是,有时候明明本地仓库确确实实存在相应的依赖包,但是在eclipse环境下依然会报错,即:在pom.xml显示错误提示信息。

可以通过如下方式解决。

二、示例讲解

以依赖commons-io-2.0.1为例讲解:

在pom.xml中配置:

<dependency>

<groupId>commons-io</groupId>

<artifactId>commons-io</artifactId>

<version>2.0.1</version>

</dependency>

三、解决方案

1、

首先,从其他可以在外网下载相应依赖包的地方下载好相应的依赖包到本地仓库,再将该本地仓库拷贝到项目相应位置,便于引用。

2、安装Maven2并进行相应配置,关于Maven2具体安装和配置,略过。

注意:在Maven2的配置文件setting.xml一定要修改如下属性:

<offline>true</offline>

该属性配置默认为false,因为是在内网,无法到外网下载依赖包,所以一定要配置离线属性为true。

3、如果本地仓库确实存在该依赖包,且eclipse没有报错,那么在本地仓库中找到该依赖包所在的位置,相应目录下必定存在如下6个文件:

_maven.repositories

commons-io-2.0.1.jar

commons-io-2.0.1.jar.sha1

commons-io-2.0.1.pom

commons-io-2.0.1.pom.sha1

m2e-lastUpdated.properties

依赖配置正确且eclipse没有报错时依赖包目录下包含的文件

当然,如下载了源码的话,应该还包括如下2个文件:

commons-io-2.0.1-sources.jar

commons-io-2.0.1-sources.jar.sha1

所以,如果当本地仓库中确实存在相应的依赖jar包,但是在eclipse中依然报错的话,请进入到相应依赖包的目录位置,观察该目录下是否对应的6个文件。

通常情况下,当在eclipse中报错的话,在依赖包目录下还存在一个“xxx.lastUpdated”文件。

如果确实存在“xxx.lastUpdated”文件的话,请先删除该文件。并确认是否存在如下2个文件:

_maven.repositories

m2e-lastUpdated.properties

通常在eclipse中报错的话,是不会存在这2个文件的,到别的依赖正确的jar包目录下去拷贝这2个文件到当前目录下,并修改相应私服id

完成后,在eclipse中clean一下项目,并执行“Maven->Update

Project...”

https://zhidao.baidu.com/question/2144469377992920868.html

.pom文件配置

1.概述

pom中节点如下分布

<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>

<!-- 基本配置 -->

<groupId>...</groupId>

<artifactId>...</artifactId>

<version>...</version>

<packaging>...</packaging>

<!-- 依赖配置 -->

<dependencies>...</dependencies>

<parent>...</parent>

<dependencyManagement>...</dependencyManagement>

<modules>...</modules>

<properties>...</properties>

<!-- 构建配置 -->

<build>...</build>

<reporting>...</reporting>

<!-- 项目信息 -->

<name>...</name>

<description>...</description>

<url>...</url>

<inceptionYear>...</inceptionYear>

<licenses>...</licenses>

<organization>...</organization>

<developers>...</developers>

<contributors>...</contributors>

<!-- 环境设置 -->

<issueManagement>...</issueManagement>

<ciManagement>...</ciManagement>

<mailingLists>...</mailingLists>

<scm>...</scm>

<prerequisites>...</prerequisites>

<repositories>...</repositories>

<pluginRepositories>...</pluginRepositories>

<distributionManagement>...</distributionManagement>

<profiles>...</profiles>

</project>

2.基本配置

modelVersion:pom模型版本,maven2和3只能为4.0.0

groupId:组ID,maven用于定位

artifactId:在组中的唯一ID用于定位

version:项目版本

packaging:项目打包方式,有以下值:pom, jar, maven-plugin, ejb, war, ear, rar, par

3.依赖配置

3.1 parent

用于确定父项目的坐标。

<parent>

<groupId>com.learnPro</groupId>

<artifactId>SIP-parent</artifactId>

<relativePath></relativePath>

<version>0.0.1-SNAPSHOT</version>

</parent>

groupId:父项目的构件标识符

artifactId:父项目的唯一标识符

relativePath:Maven首先在当前项目的找父项目的pom,然后在文件系统的这个位置(relativePath),然后在本地仓库,再在远程仓库找。

version:父项目的版本

3.2 modules

有些maven项目会做成多模块的,这个标签用于指定当前项目所包含的所有模块。之后对这个项目进行的maven操作,会让所有子模块也进行相同操作。

<modules>

<module>com-a</>

<module>com-b</>

<module>com-c</>

</>

3.3 properties

用于定义pom常量

<properties>

<java.version>1.7</java.version>

</properties>

上面这个常量可以在pom文件的任意地方通过${java.version}来引用

3.4 dependencies

项目相关依赖配置,如果在父项目写的依赖,会被子项目引用,一般父项目会将子项目公用的依赖引入(将在之后详细讲解)

<dependencies>

<dependency>

<groupId>junit</groupId>

<artifactId>junit</artifactId>

<version>4.12</version>

</dependency>

</dependencies>

这边依赖和中央仓库中的一致,就可以引入对应的jar

scope

Dependency scope 是用来限制Dependency的作用范围的, 影响maven项目在各个生命周期时导入的package的状态。

compile

默认的scope,表示 dependency 都可以在生命周期中使用。而且,这些dependencies 会传递到依赖的项目中。

provided

跟compile相似,但是表明了dependency 由JDK或者容器提供,例如Servlet AP和一些Java EE APIs。这个scope 只能作用在编译和测试时,同时没有传递性。

使用这个时,不会将包打入本项目中,只是依赖过来。

使用默认或其他时,会将依赖的项目打成jar包,放入本项目的Lib里

runtime

表示dependency不作用在编译时,但会作用在运行和测试时

test

表示dependency作用在测试时,不作用在运行时。

system

跟provided 相似,但是在系统中要以外部JAR包的形式提供,maven不会在repository查找它。 例如:

<dependency>
<groupId>javax.sql</groupId>
<artifactId>jdbc-stdext</artifactId>
<version>2.0</version>
   <scope>system</scope>
<systemPath>${java.home}/lib/rt.jar</systemPath>
</dependency>

${project.basedir}

import (Maven 2.0.9 之后新增)

它只使用在<dependencyManagement>中,表示从其它的pom中导入dependency的配置

3.5 dependencyManagement

配置写法同dependencies

<dependencyManagement>

<dependencies>

.....

</dependencies>

</dependencyManagement>

在父模块中定义后,子模块不会直接使用对应依赖,但是在使用相同依赖的时候可以不加版本号:

父项目:

<dependencyManagement>

<dependencies>

<dependency>

<groupId>junit</groupId>

<artifactId>junit</artifactId>

<version>4.12</version>

<scope>test</scope>

</dependency>

</dependencies>

</dependencyManagement>

子项目:

<dependency>

<groupId>junit</groupId>

<artifactId>junit</artifactId>

</dependency>

这样的好处是,父项目统一了版本,而且子项目可以在需要的时候才引用对应的依赖

4.构建配置

4.1 build

用于配置项目构建相关信息

<build>

<!--该元素设置了项目源码目录,当构建项目的时候,构建系统会编译目录里的源码。该路径是相对于pom.xml的相对路径。 -->

<sourceDirectory />

<!--该元素设置了项目脚本源码目录,该目录和源码目录不同:绝大多数情况下,该目录下的内容 会被拷贝到输出目录(因为脚本是被解释的,而不是被编译的)。 -->

<scriptSourceDirectory />

<!--该元素设置了项目单元测试使用的源码目录,当测试项目的时候,构建系统会编译目录里的源码。该路径是相对于pom.xml的相对路径。 -->

<testSourceDirectory />

<!--被编译过的应用程序class文件存放的目录。 -->

<outputDirectory />

<!--被编译过的测试class文件存放的目录。 -->

<testOutputDirectory />

<!--使用来自该项目的一系列构建扩展 -->

<extensions>

<!--描述使用到的构建扩展。 -->

<extension>

<groupId />

<artifactId />

<version />

</extension>

</extensions>

<!--当项目没有规定目标(Maven2 叫做阶段)时的默认值 -->

<defaultGoal />

<!--这个元素描述了项目相关的所有资源路径列表,例如和项目相关的属性文件,这些资源被包含在最终的打包文件里。 -->

<resources>

<!--这个元素描述了项目相关或测试相关的所有资源路径 -->

<resource>

<!--是否使用参数值代替参数名。参数值取自properties元素或者文件里配置的属性,文件在filters元素里列出。 -->

<filtering />

<!--描述存放资源的目录,该路径相对POM路径 -->

<directory />

<!--包含的模式列表,例如**/*.xml. -->

<includes />

<!--排除的模式列表,例如**/*.xml -->

<excludes />

<!-- 描述了资源的目标路径。该路径相对target/classes目录(例如${project.build.outputDirectory})。举个例子,如果你想资源在特定的包里(org.apache.maven.messages),你就必须该元素设置为org/apache/maven /messages。然而,如果你只是想把资源放到源码目录结构里,就不需要该配置。 -->

<targetPath />

</resource>

</resources>

<!--这个元素描述了单元测试相关的所有资源路径,例如和单元测试相关的属性文件。 -->

<testResources>

<!--这个元素描述了测试相关的所有资源路径,参见build/resources/resource元素的说明 -->

<testResource>

<targetPath />

<filtering />

<directory />

<includes />

<excludes />

</testResource>

</testResources>

<!--构建产生的所有文件存放的目录 -->

<directory />

<!--产生的构件的文件名,默认值是${artifactId}-${version}。 -->

<finalName />

<!--当filtering开关打开时,使用到的过滤器属性文件列表 -->

<filters />

<!--子项目可以引用的默认插件信息。该插件配置项直到被引用时才会被解析或绑定到生命周期。给定插件的任何本地配置都会覆盖这里的配置 -->

<pluginManagement>

<!--使用的插件列表 。 -->

<plugins>

<!--plugin元素包含描述插件所需要的信息。 -->

<plugin>

<groupId />

<artifactId />

<version />

<!--是否从该插件下载Maven扩展(例如打包和类型处理器),由于性能原因,只有在真需要下载时,该元素才被设置成enabled。 -->

<extensions />

<!--在构建生命周期中执行一组目标的配置。每个目标可能有不同的配置。 -->

<executions>

<!--execution元素包含了插件执行需要的信息 -->

<execution>

<!--执行目标的标识符,用于标识构建过程中的目标,或者匹配继承过程中需要合并的执行目标 -->

<id />

<!--绑定了目标的构建生命周期阶段,如果省略,目标会被绑定到源数据里配置的默认阶段 -->

<phase />

<!--配置的执行目标 -->

<goals />

<!--配置是否被传播到子POM -->

<inherited />

<!--作为DOM对象的配置 -->

<configuration />

</execution>

</executions>

<!--项目引入插件所需要的额外依赖 -->

<dependencies>

<!--参见dependencies/dependency元素 -->

<dependency>

......

</dependency>

</dependencies>

<!--任何配置是否被传播到子项目 -->

<inherited />

<!--作为DOM对象的配置 -->

<configuration />

</plugin>

</plugins>

</pluginManagement>

<!--使用的插件列表 -->

<plugins>

<!--参见build/pluginManagement/plugins/plugin元素 -->

<plugin>

<groupId />

<artifactId />

<version />

<extensions />

<executions>

<execution>

<id />

<phase />

<goals />

<inherited />

<configuration />

</execution>

</executions>

<dependencies>

<!--参见dependencies/dependency元素 -->

<dependency>

......

</dependency>

</dependencies>

<goals />

<inherited />

<configuration />

</plugin>

</plugins>

</build>

4.2 reporting

该元素描述使用报表插件产生报表的规范。当用户执行“mvn site”,这些报表就会运行。 在页面导航栏能看到所有报表的链接。

<reporting>

<!--true,则,网站不包括默认的报表。这包括“项目信息”菜单中的报表。-->

<excludeDefaults/>

<!--所有产生的报表存放到哪里。默认值是${project.build.directory}/site。-->

<outputDirectory/>

<!--使用的报表插件和他们的配置。-->

<plugins>

<!--plugin元素包含描述报表插件需要的信息-->

<plugin>

<!--报表插件在仓库里的group ID-->

<groupId/>

<!--报表插件在仓库里的artifact ID-->

<artifactId/>

<!--被使用的报表插件的版本(或版本范围)-->

<version/>

<!--任何配置是否被传播到子项目-->

<inherited/>

<!--报表插件的配置-->

<configuration/>

<!--一组报表的多重规范,每个规范可能有不同的配置。一个规范(报表集)对应一个执行目标 。例如,有1,2,3,4,5,6,7,8,9个报表。1,2,5构成A报表集,对应一个执行目标。2,5,8构成B报表集,对应另一个执行目标-->

<reportSets>

<!--表示报表的一个集合,以及产生该集合的配置-->

<reportSet>

<!--报表集合的唯一标识符,POM继承时用到-->

<id/>

<!--产生报表集合时,被使用的报表的配置-->

<configuration/>

<!--配置是否被继承到子POMs-->

<inherited/>

<!--这个集合里使用到哪些报表-->

<reports/>

</reportSet>

</reportSets>

</plugin>

</plugins>

</reporting>

5.项目信息

name:给用户提供更为友好的项目名

description:项目描述,maven文档中保存

url:主页的URL,maven文档中保存

inceptionYear:项目创建年份,4位数字。当产生版权信息时需要使用这个值

licenses:该元素描述了项目所有License列表。 应该只列出该项目的license列表,不要列出依赖项目的license列表。如果列出多个license,用户可以选择它们中的一个而不是接受所有license。(如下)

<license>

<!--license用于法律上的名称-->

<name>...</name>

<!--官方的license正文页面的URL-->

<url>....</url>

<!--项目分发的主要方式:repo,可以从Maven库下载 manual, 用户必须手动下载和安装依赖-->

<distribution>repo</distribution>

<!--关于license的补充信息-->

<comments>....</comments>

</license>

organization:1.name组织名2.url组织主页url

developers:项目开发人员列表(如下)

contributors:项目其他贡献者列表,同developers

<developers>

<!--某个开发者信息 -->

<developer>

<!--开发者的唯一标识符 -->

<id>....</id>

<!--开发者的全名 -->

<name>...</name>

<!--开发者的email -->

<email>...</email>

<!--开发者的主页 -->

<url>

...

<url />

<!--开发者在项目中的角色 -->

<roles>

<role>Java Dev</role>

<role>Web UI</role>

</roles>

<!--开发者所属组织 -->

<organization>sun</organization>

<!--开发者所属组织的URL -->

<organizationUrl>...</organizationUrl>

<!--开发者属性,如即时消息如何处理等 -->

<properties>

<!-- 和主标签中的properties一样,可以随意定义子标签 -->

</properties>

<!--开发者所在时区, -11到12范围内的整数。 -->

<timezone>-5</timezone>

</developer>

</developers>

6.环境设置

6.1 issueManagement

目的问题管理系统(Bugzilla, Jira, Scarab)的名称和URL

<issueManagement>

<system>Bugzilla</system>

<url>http://127.0.0.1/bugzilla/</url>

</issueManagement>

system:系统类型

url:路径

6.2 ciManagement

项目的持续集成信息

<ciManagement>

<system>continuum</system>

<url>http://127.0.0.1:8080/continuum</url>

<notifiers>

<notifier>

<type>mail</type>

<sendOnError>true</sendOnError>

<sendOnFailure>true</sendOnFailure>

<sendOnSuccess>false</sendOnSuccess>

<sendOnWarning>false</sendOnWarning>

<address>continuum@127.0.0.1</address>

<configuration></configuration>

</notifier>

</notifiers>

</ciManagement>

system:持续集成系统的名字

url:持续集成系统的URL

notifiers:构建完成时,需要通知的开发者/用户的配置项。包括被通知者信息和通知条件(错误,失败,成功,警告)

type:通知方式

sendOnError:错误时是否通知

sendOnFailure:失败时是否通知

sendOnSuccess:成功时是否通知

sendOnWarning:警告时是否通知

address:通知发送到的地址

configuration:扩展项

6.3 mailingLists

项目相关邮件列表信息

<mailingLists>

<mailingList>

<name>User List</name>

<subscribe>user-subscribe@127.0.0.1</subscribe>

<unsubscribe>user-unsubscribe@127.0.0.1</unsubscribe>

<post>user@127.0.0.1</post>

<archive>http://127.0.0.1/user/</archive>

<otherArchives>

<otherArchive>http://base.google.com/base/1/127.0.0.1</otherArchive>

</otherArchives>

</mailingList>

.....

</mailingLists>

subscribe, unsubscribe: 订阅邮件(取消订阅)的地址或链接,如果是邮件地址,创建文档时,mailto:链接会被自动创建

archive:浏览邮件信息的URL

post:接收邮件的地址

6.4 scm

允许你配置你的代码库,供Maven web站点和其它插件使用

<scm>

<connection>scm:svn:http://127.0.0.1/svn/my-project</connection>

<developerConnection>scm:svn:https://127.0.0.1/svn/my-project</developerConnection>

<tag>HEAD</tag>

<url>http://127.0.0.1/websvn/my-project</url>

</scm>

connection, developerConnection:这两个表示我们如何连接到maven的版本库。connection只提供读,developerConnection将提供写的请求

写法如:scm:[provider]:[provider_specific]

如果连接到CVS仓库,可以配置如下:scm:cvs:pserver:127.0.0.1:/cvs/root:my-project

tag:项目标签,默认HEAD

url:共有仓库路径

6.5 prerequisites

项目构建的前提

<prerequisites>

<maven>2.0.6</maven>

</prerequisites>

6.6 repositoriespluginRepositories

依赖和扩展的远程仓库列表,同setting.xml配置中介绍的。

<repositories>
    <repository>
        <releases>//发行版
            <enabled>false</enabled>
            <updatePolicy>always</updatePolicy>
            <checksumPolicy>warn</checksumPolicy>
        </releases>
        <snapshots>//快照版
            <enabled>true</enabled>
            <updatePolicy>never</updatePolicy>
            <checksumPolicy>fail</checksumPolicy>
        </snapshots>
        <id>codehausSnapshots</id>
        <name>Codehaus Snapshots</name>
        <url>http://snapshots.maven.codehaus.org/maven2</url>
        <layout>default</layout>
    </repository>
</repositories>

<pluginRepositories>
    ...
</pluginRepositories>

releases, snapshots:这是各种构件的策略,release或者snapshot。这两个集合,POM就可以根据独立仓库任意类型的依赖改变策略。如:一个人可能只激活下载snapshot用来开发。

enable:true或者false,决定仓库是否对于各自的类型激活(release或者snapshot)。

updatePolicy: 这个元素决定更新频率。maven将比较本地pom的时间戳(存储在仓库的maven数据文件中)和远程的.有以下选择: always, daily (默认), interval:X (x是代表分钟的整型),never.

checksumPolicy:当Maven向仓库部署文件的时候,它也部署了相应的校验和文件。可选的为:ignore,fail,warn,或者不正确的校验和。

layout:在上面描述仓库的时候,提到他们有统一的布局。Maven 2有它仓库默认布局。然而,Maven 1.x有不同布局。使用这个元素来表明它是default还是legacy。

6.7 distributionManagement

它管理的分布在整个构建过程生成的工件和支持文件

<distributionManagement>

...

<downloadUrl>http://mojo.codehaus.org/my-project</downloadUrl>

<status>deployed</status>

</distributionManagement>

downloadUrl: 其他pom可以通过此url的仓库抓取组件

status:给出该构件在远程仓库的状态

none: 默认

converted: 将被早期Maven 2 POM转换过来

partner: 这个项目会从合作者仓库同步过来

deployed: 从Maven 2或3实例部署

verified: 被核实时正确的和最终的

repository

指定Maven pom从远程下载控件到当前项目的位置和方式,如果snapshotRepository没有被定义则使用repository相关的配置

<distributionManagement>

<repository>

<uniqueVersion>false</uniqueVersion>

<id>corp1</id>

<name>Corporate Repository</name>

<url>scp://repo/maven2</url>

<layout>default</layout>

</repository>

<snapshotRepository>

<uniqueVersion>true</uniqueVersion>

<id>propSnap</id>

<name>Propellors Snapshots</name>

<url>sftp://propellers.net/maven</url>

<layout>legacy</layout>

</snapshotRepository>

...

</distributionManagement>

id, name:仓库的唯一标识

uniqueVersion:true或false,指明控件部署的时候是否获取独立的版本号。

url:repository元素的核心。指定位置和部署协议发布控件到仓库。

layout:布局,default或legacy

site

多分布存储库,distributionManagement负责定义如何部署项目的网站和文档。

<distributionManagement>

...

<site>

<id>mojo.website</id>

<name>Mojo Website</name>

<url>scp://beaver.codehaus.org/home/projects/mojo/public_html/</url>

</site>

...

</distributionManagement>

id, name, url: 这些元素与distributionManagement repository中的相同

relocation

重新部署-项目不是静态的,是活的。他们需要被搬到更合适的地方。如:当你的下个成功的开源项目移到Apache下,重命名为org.apache:my-project:1.0对你项目更有好处。

<distributionManagement>

...

<relocation>

<groupId>org.apache</groupId>

<artifactId>my-project</artifactId>

<version>1.0</version>

<message>We have moved the Project underApache</message>

</relocation>

...

</distributionManagement>

6.8 profiles

配置重点

profile可以让我们定义一系列的配置信息(插件等),然后指定其激活条件

profile的相关配置可以参考–setting文件配置-profiles

.setting文件配置

1.文件概览

<?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/>

<interactiveMode/>

<offline/>

<pluginGroups/>

<servers/>

<mirrors/>

<proxies/>

<profiles/>

<activeProfiles/>

</settings>

通过配置文件中的注释,我们可以看到,有两种配置此文件的方法

1.用户级别

${user.home}/.m2/settings.xml

可以通过指令 -s /path/to/user/settings.xml

2.全局级别

${maven.home}/conf/settings.xml.

可以通过指令 -gs /path/to/global/settings.xml

2.localRepository

localRepository用于构建系统的本地仓库的路径。

默认的值是${user.home}/.m2/repository。

Default: ${user.home}/.m2/repository

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

3.interactiveMode

interactiveMode 用于决定maven是否在需要输出的时候提示你,默认true。如果是false,它将使用合理的默认值,或者基于一些设置。

4.offline

决定maven是否在构建的时候进行网络传输。

默认false,表示联网状态,true为取消联网。

在某些情况下设置为true是很有用的,比如jar无法从网上下载等

5.pluginGroups

pluginGroups 插件组

<pluginGroups>

<pluginGroup>org.mortbay.jetty</pluginGroup>

</pluginGroups>

这样Maven可以使用简单的命令执行org.morbay.jetty:jetty-maven-plugin:run

mvn jetty run

我们同样可以在pom文件中看到相似的配置,只是在这配置了就起到全局的作用,而不用每个项目中pom配置jetty

6.proxies

此项用于设置http代理

有时候由于安全问题,需要配置http代理,通过代理服务才能正常访问外部仓库下载资源可以ping repo1.maven.org来访问中央仓库

telnet 218.14.227.197 3128 来查看代理地址以及端口是否畅通

<proxies>

<proxy>

<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.net|some.host.com</nonProxyHosts>

</proxy>

</proxies>

id:proxy的唯一标识,用来区别proxy元素。

active:表示是否激活代理,如果配置多个,默认是第一个生效

username,password:提供连接代理服务器时的认证。

host,port:主机地址,端口号

nonProxyHosts:用来表示哪些主机名不需要代理,可以用|来分

割多个,此外也支持通配符,

如:*.goole.com表示所有以goole.com结尾的都不需要通过代理

7.servers

这是一个认证配置的列表,根据系统中使用的server-id控制。认证配置在maven连接到远程服务时使用。

<servers>

<!--使用登录方式-->

<server>

<id>deploymentRepo</id>

<username>repouser</username>

<password>repopwd</password>

</server>

<!-- 使用秘钥认证 -->

<server>

<id>siteServer</id>

<privateKey>/path/to/private/key</privateKey>

<passphrase>可空</passphrase>

</server>

</servers>

8.mirrors

指定镜像仓库位置用于从远程仓库下载资源

repository

repository就是个仓库.maven里有两种仓库,本地仓库和远程仓库。远程仓库相当于公共的仓库,大家都能看到。本地仓库是你本地的缓存副本,只有你看的到,主要起缓存作用。当你向仓库请求插件或依赖的时候,会先检查本地仓库里是否有。如果有则直接返回,否则会向远程仓库请求,并被缓存到本地仓库。远程仓库可以在工程的pom.xml文件里指定。本地仓库默认在你本地的用户目录下的.m2/repository目录下。

//目标仓库

<repository>
    <id>central</id>
    <name>Maven Repository Switchboard</name>
    <url>**[http://repo1.maven.org/maven2</url>

<snapshots>
        <enabled>false</enabled>
    </snapshots>
</repository>

mirror

mirror就是镜像,主要提供一个方便地切换远程仓库地址的途径。

比如,上班的时候在公司,用电信的网络,连的是电信的仓库。回到家后,是网通的网络,我想连网通的仓库,就可以通过mirror配置,统一把我工程里的仓库地址都改成联通的,而不用到具体工程pom配置文件里一个个地改地址。比如给上面id为central的远程仓库做个镜像。

<mirror>
    <id>mirrorId</id>
    <mirrorOf>central</mirrorOf>
    <name>myMirror.</name>
    <url>http://my.repository.com/repo/path</url>
</mirror>

<mirrorOf>的值为central,表示该配置为中央仓库的镜像,任何对于中央仓库的请求,都会转至该镜像

以后向central这个仓库发的请求都会发到http://my.repository.com/repo/path而不是http://repo1.maven.org/maven2,简单而言, mirror可以拦截对远程仓库的请求 , 改变对目标仓库的下载地址

<mirrorOf>central</mirrorOf>里是要替代的仓库的id。如果填*,就会替代所有仓库。所有的仓库都被镜像到这个url

私服

关于镜像的一个更为常见的用法是结合私服,由于私服可以代理任何外部的公共仓库(包括中央仓库),因此对于组织内部的maven用户来说,使用一个私服地址就等于使用了所有需要的外部仓库,这样可以将配置集中到私服,从而简化maven本身的配置,在这种情况下,任何需要的构件都可以从私服获得,私服就是所有仓库的镜像,这时,可以把<mirrorOf>的值改为*,标识该配置是所有maven仓库的镜像,任何对于远程仓库的请求都会转到http://my.repository.com/repo/path,如果该镜像仓库需要认证,则配置一个id为mirrorId的<server>即可

如果想有些仓库使用镜像,有些仓库不想被镜像,可以在pom文件中配置,

<repositories>
    <repository>
        <id>maven-aliyun</id>
        <name>maven-aliyun</name>
        <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
    </repository>

</repositories>

也可以在setting文件中配置

<profiles>
   <profile>
      <id>maven-aliyun</id>
      <repositories>
         <repository>
            <id>maven-aliyun</id>
            <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
            <releases>
               <enabled>true</enabled>
            </releases>
            <snapshots>
               <enabled>true</enabled>
            </snapshots>
         </repository>
      </repositories>
   </profile>
</profiles>
<activeProfiles>
   <activeProfile>maven-aliyun</activeProfile>
</activeProfiles>

mirror中的mirrorOf不要配置为*或者maven-aliyun

高级配置

为满足一些复杂的需求,maven还支持更高级的镜像配置

<mirrorOf>*</mirrorOf>:匹配所有远程仓库
<mirrorOf>external:*</mirrorOf>:匹配所有远程仓库,使用localhost的除外,使用file://协议的除外,也就是说,匹配所有不在本机上的远程仓库
<mirrorOf>repo1,repo2</mirrorOf>:匹配仓库repo1和repo2,使用逗号分隔多个远程仓库
<mirrorOf>*,!repo1</mirrorOf>:匹配所有远程仓库,repo1除外,使用感叹号将仓库从匹配中排除

当您使用高级语法并配置多个镜像时,声明顺序很重要。当 Maven 查找某个存储库的镜像时,它首先检查<mirrorOf>与存储库标识符完全匹配的镜像。如果没有找到直接匹配,Maven 会根据上述规则(如果有)选择第一个匹配的镜像声明。因此,您可以通过更改定义的顺序来影响匹配顺序settings.xml

如果maven的setting.xml中配置了镜像mirror , 而镜像mirror配置里mirrorOf的规则,匹配到目标仓库时 ,maven认为目标仓库被镜像了, 不会再去被镜像仓库下载依赖jar包, 而是直接去镜像仓库下载。即maven里面设置的repository会被setting.xml里面的mirror所屏蔽。

镜像仓库:
<mirror>
    <id>aliyun-maven</id>
    <name>aliyun maven</name>
    <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
    <mirrorOf>repotest</mirrorOf>
</mirror>
目标仓库:
<repositories>
    <repository>
        <id>repotest</id>
        <url>https://repo.maven.apache.org/maven2/</url>
    </repository>
</repositories>

镜像配置的规则 <mirrorOf>repotest</mirrorOf> 匹配到目标仓库 <id>repotest</id>,所以maven认为目标仓库repotest被镜像了, 不再去https://repo.maven.apache.org/maven2/地址下载jar包,而是去镜像仓库http://maven.aliyun.com/nexus/content/groups/public/下载jar包。

需要注意的是,由于镜像仓库完全屏蔽了被镜像仓库,当镜像仓库不稳定或者停止服务的时候,maven仍将无法访问被镜像仓库,因此无法下载构件

mirror的mirrorOf不能和任何mirror的id一致,因为id在setting中唯一,mirrorOf要和库的id一致,所以势必不能和mirror的id一致

默认仓库

maven下面的pom.xml的中央仓库,缺省了模板仓库id为central,url为maven中央仓库url:https://repo.maven.apache.org/maven2/。默认包含的主 Maven 中央存储库的 ID 是central

# 默认中央仓库
<repositories>
    <repository>
        <id>central</id>
        <url>https://repo.maven.apache.org/maven2/</url>
    </repository>
</repositories>

配置多个镜像时的使用顺序

精确匹配 > 通配符匹配(*)

配置优先级

需要注意的是:局部配置优先于全局配置。

配置优先级从高到低:pom.xml> user settings > global settings

如果这些文件同时存在,在应用配置时,会合并它们的内容,如果有重复的配置,优先级高的配置会覆盖优先级低的。

Setting.xml中repository的配置与pom.xml中repository的配置有什么不同?

Setting.xml中配置repository与pom.xml中配置repository的作用是相同的,都是为了指定多个存储库的使用(you can specify the use of multiple repositories)。但在pom.xml中配置只对当前项目与子项目有用,而在setting.xml中配置为全局性配置,用于所用的项目。

参考:

mirrorof的官方文档解:Maven – Guide to Mirror Settings

maven的setting配置文件中mirror和repository的区别 - 简书

mirror镜像和repository远程仓库、mirrorof - 简书

9.profiles

配置重点

1. settings.xml中时意味着该profile是全局的,所以只能配置范围宽泛一点配置信息,比如远程仓库等。而一些比较细致一点的需要定义在项目的pom.xml中。

2.profile可以让我们定义一系列的配置信息,然后指定其激活条件。

根据每个profile对应不同的激活条件和配置信息,从而达到不同环境使用不同配置。

例子:通过profile定义jdk1.5以上使用一套配置,jdk1.5以下使用另外一套配置;或者通过操作系统来使用不同的配置信息。

3.settings.xml中的信息有repositories、pluginRepositories和properties。定义在properties的值可以在pom.xml中使用。

activation

<profiles>

//可定义多个profile,使用activeProfiles激活

<profile>

<id>test</id>

<activation>

<activeByDefault>false</activeByDefault>

<jdk>1.5</jdk>

<os>

<name>Windows XP</name>

<family>Windows</family>

<arch>x86</arch>

<version>5.1.2600</version>

</os>

<property>

<name>mavenVersion</name>

<value>2.0.3</value>

</property>

<file>

<exists>${basedir}/file2.properties</exists>

<missing>${basedir}/file1.properties</missing>

</file>

</activation>

</profile>

</profiles>

jdk:检测到对应jdk版本就激活

os:针对不同操作系统

property:当maven检测到property(pom中如${name}这样的)profile将被激活

file:如果存在文件,激活,不存在文件激活

通过以下命令查看哪些profile将生效

mvn help:active-profiles

properites

Maven的属性是值占位符,如果X是一个属性的话,在POM中可以使用${X}来进行任意地方的访问。他们来自于五种不同的风格,所有都可以从settings.xml文件中访问到。

1.env.x:“env.”前缀会返回当前的环境变量。如${env.PATH}就是使用了$path环境变量(windosws中的%PATH%)。

2.project.x:一个点“.”分割的路径,在POM中就是相关的元素的值。例如:<project><version>1.0</version></project>就可以通过${project.version}来访问。

3.settings.x:一个点“.”分割的路径,在settings.xml中就是相对应的元素的值,例如:<settings><offline>false</offline></settings>就可以通过${settings.offline}来访问。

4.Java系统属性:通过java.lang.System.getProperties()来访问的属性都可以像POM中的属性一样访问,例如:${java.home}

5.x:被<properties/>或者外部文件定义的属性,值可以这样访问${someVar}

<profiles>

<profile>

...

<properties>

<user.install>${user.home}/our-project</user.install>

</properties>

...

</profile>

</profiles>

上面这个profile如果被激活,那么在pom中${user.install}就可以被访问了。

实战:

<profiles>
   <profile>
      <id>dev</id>
      <activation>
         <activeByDefault>true</activeByDefault>
      </activation>
      <properties>
         <p.jdbc.url>172.11.2.112:3306/yh2_qr</p.jdbc.url>
         <p.jdbc.username>root</p.jdbc.username>
         <p.jdbc.password>root</p.jdbc.password>
      </properties>
   </profile>
   <profile>
      <id>uat</id>
      <properties>
         <p.jdbc.url>172.11.2.131:3306/yh2_qr</p.jdbc.url>
         <p.jdbc.username>root</p.jdbc.username>
         <p.jdbc.password>Cheryfs5t6y89kL</p.jdbc.password>
      </properties>
   </profile>
</profiles>

jdbc.url=jdbc:mysql://${p.jdbc.url}?characterEncoding=utf-8&allowMultiQueries=true
jdbc.username=${p.jdbc.username}
jdbc.password=${p.jdbc.password}

repositories

repositories是远程项目集合maven用来移植到本地仓库用于构建系统。如果来自本地仓库,Maven调用它的插件和依赖关系。不同的远程仓库可能包含不同的项目,当profile被激活,他们就会需找匹配的release或者snapshot构件。

<profiles>

<profile>

...

<repositories>

<repository>

<id>codehausSnapshots</id>

<name>Codehaus Snapshots</name>

<releases>

//发行版

<enabled>false</enabled>

<updatePolicy>always</updatePolicy>

<checksumPolicy>warn</checksumPolicy>

</releases>

<snapshots>

//快照版

<enabled>true</enabled>

<updatePolicy>never</updatePolicy>

<checksumPolicy>fail</checksumPolicy>

</snapshots>

<url>http://snapshots.maven.codehaus.org/maven2</url>

<layout>default</layout>

</repository>

</repositories>

<pluginRepositories>

...

</pluginRepositories>

...

</profile>

</profiles>

1.releases,snapshots:这是各种构件的策略,release或者snapshot。这两个集合,POM就可以根据独立仓库任意类型的依赖改变策略。如:一个人可能只激活下载snapshot用来开发。

2.enable:true或者false,决定仓库是否对于各自的类型激活(release或者snapshot)。

3.updatePolicy:这个元素决定更新频率。maven将比较本地pom的时间戳(存储在仓库的maven数据文件中)和远程的.有以下选择: always, daily (默认), interval:X (x是代表分钟的整型),never.

4.checksumPolicy:当Maven向仓库部署文件的时候,它也部署了相应的校验和文件。可选的为:ignore,fail,warn,或者不正确的校验和。

5.layout:在上面描述仓库的时候,提到他们有统一的布局。Maven 2有它仓库默认布局。然而,Maven 1.x有不同布局。使用这个元素来表明它是default还是legacy。

10.activeProfiles

<activeProfiles>

<activeProfile>alwaysActiveProfile</activeProfile>

<activeProfile>anotherAlwaysActiveProfile</activeProfile>

</activeProfiles>

每个activeProfile元素对应一个profile id的值,任何profile id被定义到activeProfile的profile将被激活。

参考:

Pom:Maven实战(三)--Pom.xml详解_oDeviloo的博客-CSDN博客

Setting:Maven实战(二)--setting.xml详解_oDeviloo的博客-CSDN博客

整合:https://blog.csdn.net/qq877507054/article/details/79138294?spm=1001.2014.3001.5501

Maven之pom.xml与setting.xml配置文件详解相关推荐

  1. Hibernate3 入门之Api,配置文件详解

    1 api详解[多练] 1.1 体系结构 PO:persistent object ,用于与数据库交互数据.--dao层  (JavaBean + hbm ) BO:Business object 业 ...

  2. Maven学习—setting.xml pom.xml 配置文件详解

    一.Maven最重要的两个配置文件 Maven 仓库及相关设置配置文件 setting.xml配置文件可以设置本地仓库目录.远程仓库地址.JDK版本等等 Maven构建Java工程的pom配置文件 p ...

  3. sqlMapConfig.xml配置文件详解

    sqlMapConfig.xml配置文件详解:  Xml代码 Xml代码   <? xml version="1.0" encoding="UTF-8"  ...

  4. ibatis之——sqlMapConfig.xml配置文件详解

    转载请注明出处:http://blog.csdn.net/l1028386804/article/details/47104893 sqlMapConfig.xml配置文件详解: <?xml v ...

  5. AndroidManifest.xml配置文件详解

    AndroidManifest.xml配置文件详解 . AndroidManifest.xml配置文件对于Android应用开发来说是非常重要的基础知识,本文旨在总结该配置文件中重点的用法,以便日后查 ...

  6. java spring bean配置文件_Spring基于xml文件配置Bean过程详解

    这篇文章主要介绍了spring基于xml文件配置Bean过程详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 通过全类名来配置: class:be ...

  7. hbase-site.xml 配置文件详解

    目录 1 版本信息 2 hbase-site.xml 配置文件详解 1 版本信息 2 hbase-site.xml 配置文件详解 <configuration  xmlns:xi="h ...

  8. Spring XML配置文件详解

    ​ spring配置文件是用于指导Spring工厂进行Bean生产.依赖关系注入(装配)及Bean实例分发的"图纸".Spring框架的配置文件是基于xml的,Spring强大的功 ...

  9. XML for Analysis(XMLA)开发详解-(4)XMLA over TCP/IP访问Analysis Services 2005/2008 Olap的释疑

    目录 1.综述(简要回顾一下Olap开发接口) 2.XMLA概念(基本来自联机手册,介绍一些概念,如果熟悉概念可以略过) 3.XMLA配置(针对各个版本的SSAS.IIS的配置说明) 4.XMLA o ...

最新文章

  1. 占用栅格地图算法的理解
  2. 【记录】我在团队合作中遇到过的胎神(扑街仔)级别前端小伙伴 之 莫名其妙配置0.0.0.0这种IP访问
  3. memset函数使用详解
  4. mysql数据库基本操作练习
  5. 安装Rabbitmq脚本
  6. Python进阶之路:namedtuple
  7. Android社会化分享详解
  8. 我一个弱女子在欧洲大街上拉屎了,因为便秘惹的祸
  9. background-color:#e5eecc; border:solid 1px #c3c3c3;
  10. 软考2022上半年真题笔记
  11. es java 模糊查询_java使用elasticsearch进行模糊查询
  12. Mac版pages快捷键大全
  13. APP 接口拦截与参数破解
  14. python变量四则运算_python_第一次作业 四则运算
  15. 笔记本显示器仅计算机,笔记本可以当屏幕用 笔记本屏幕当显示器用
  16. Java线程并发协作与任务定时调度
  17. 无盘服务器内存问题,无盘服务器内存占用过高
  18. Cadence Allegro使用过程中出现的常见问题-原理图和PCB
  19. TR069协议向导——一个帮助你了解TR069协议的简明教程(一)
  20. ActiveX控件开发

热门文章

  1. Ubuntu安装Burp Suite--史上最详细教程
  2. 计算机院校都看重保研er的哪些能力?
  3. Unity连接SQLServer数据库报错找不到System.Data.dll
  4. Mac 简单免费的外网映射(其他系统类似)
  5. 两数相除之分析问题解决问题
  6. RTMP推流方案总结
  7. nedc和epa续航里程什么意思_5分钟看懂综合工况续航、等速续航和NEDC续航指标
  8. mt6735通用recovery_Mtk arm64通用root ,使用无需bl或者rec,安卓7以下使用最佳!
  9. Ubuntu16.04中命令及配置
  10. 抖音下播倒计时自动化软件,送礼物自动加时长