Skip to main content

maven安装与配置

maven安装

maven官网下载 https://maven.apache.org/download.cgi

下载apache-maven-3.9.7-bin.zip

环境变量配置

M2_HOME=D:\apache-maven-3.6.3

PATH添加 %M2_HOME%\bin

环境变量验证

mvn -v

返回结果

Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f)
Maven home: D:\apache-maven-3.6.3\bin\..
Java version: 1.8.0_202, vendor: Oracle Corporation, runtime: C:\Program Files\Java\jdk1.8.0_202\jre
Default locale: zh_CN, platform encoding: GBK
OS name: "windows 10", version: "10.0", arch: "amd64", family: "windows"

配置镜像

没有特殊需求可以配置成阿里源

新建文件名为settings.xml

内容如下

<?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>D:\Maven\repository</localRepository>
<mirrors>
<mirror>
<id>alimaven</id>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<mirrorOf>central</mirrorOf>
</mirror>
</mirrors>
</settings>

将settings.xml复制到C:\Users\[用户名]\.m2目录

含义localRepository指定仓库jar存储路径

mirrors配置镜像

若公司内有nexus仓库私服,则按按公司地址进行配置,可能需要指定用户名密码

本地安装jar

maven install deploy

mvn install 安装到本地

mvn deploy 安装到私有仓库

以oracle为例

https://www.oracle.com/technetwork/database/application-development/jdbc/downloads/index.html

下载19c的ojdbc8.jar

https://download.oracle.com/otn-pub/otn_software/jdbc/1924/ojdbc8.jar

maven install 本地安装

安装命令

mvn install:install-file -Dfile =demo.jar DgroupId=<groupId> -DartifactId=<artifactId> -Dversion=<version> -Dpackaging=jar

安装示例

安装oracle ojbc8

mvn install:install-file -Dfile=D:\MvnProject\ojdbc8.jar  -DgroupId=com.oracle -DartifactId=ojdbc8 -Dversion=19.24.0.0 -Dpackaging=jar

pom引用示例

<dependencies>        ...
<!-- ojdbc8.jar example -->
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc8</artifactId>
<version>19.24.0.0</version>
</dependency>

...

</dependencies>

maven deploy 远程安装

安装命令

mvn deploy:deploy-file -Dmaven.test.skip=true -Dfile=demo.jar -DgroupId=<groupId> -DartifactId=<artifactId> -Dversion=<version> -Dpackaging=jar -DrepositoryId=<repositoryId> -Durl=<repositoryUrl>

安装示例

release仓库

mvn deploy:deploy-file -Dmaven.test.skip=true -Dfile=D:\MvnProject\ojdbc8.jar -DgroupId=com.oracle -DartifactId=ojdbc8 -Dversion=19.24.0.0 -Dpackaging=jar -DrepositoryId=nexus-releases -Durl=http://your_nexus_server/repository/maven-releases/

对应pom

<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc8</artifactId>
<version>19.24.0.0</version>
</dependency>

snapshot仓库

mvn deploy:deploy-file -Dmaven.test.skip=true -Dfile=D:\MvnProject\ojdbc8.jar -DgroupId=com.oracle -DartifactId=ojdbc8 -Dversion=1.0.0-SNAPSHOT -Dpackaging=jar -DrepositoryId=nexus-snapshots -Durl=http://your_nexus_server/repository/maven-snapshots/

对应pom

<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc8</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>

jar和pom同步上传

mvn deploy:deploy-file -DgroupId=com.htcf  -DartifactId=frame-starter -Dversion=2.2.0-SNAPSHOT  -Dfile=E:\jar\frame-starter-2.2.0-SNAPSHOT.jar  -DpomFile=frame-starter-2.2.0-SNAPSHOT.pom  -DrepositoryId=user-thirdparty  -Durl=http://68.174.xx.xx:8081/nexus/content/repositories/snapshots/

参数定义

-Dmaven.test.skip=true                  //跳过编译、测试
-Dfile=D:\MvnProject\ojdbc8.jar //jar包文件地址,绝对路径
-DgroupId=com.oracle //gruopId--pom坐标,自定义
-DartifactId=ojdbc8 //artifactId--pom坐标,自定义
-Dversion //版本号
-Dpackaging //打包方式 jar/war/pom
-DrepositoryId //远程库服务器ID 对应setting.xml里的<repository>标签对应的id
-Durl //远程库服务器地址 对应setting.xml里的<repository>标签对应的url

云效上传示例

进入云效制品库https://packages.aliyun.com/

下载seetings.xml替换m2目录的seeting.xml

release

mvn deploy:deploy-file -Dmaven.test.skip=true -Dfile=D:\MvnProject\ojdbc8.jar -DgroupId=com.oracle -DartifactId=ojdbc8 -Dversion=19.24.0.0 -Dpackaging=jar -DrepositoryId=isesol -Durl=https://packages.aliyun.com/6556f38db322becc90a55fe6/maven/isesol

snapshot

mvn deploy:deploy-file -Dmaven.test.skip=true -Dfile=D:\MvnProject\ojdbc8.jar -DgroupId=com.oracle -DartifactId=ojdbc8 -Dversion=1.0.0-SNAPSHOT -Dpackaging=jar -DrepositoryId=isesol -Durl=https://packages.aliyun.com/6556f38db322becc90a55fe6/maven/isesol

pom引入本地jar

项目新建lib目录,将jar复制到lib目录

pom引入如下

 <dependency>
<groupId>gnu.io</groupId>
<artifactId>RXTXcomm</artifactId>
<scope>system</scope>
<systemPath>${project.basedir}/lib/RXTXcomm.jar</systemPath>
<version>2.0</version>
</dependency>

打包需要包含lib目录

   <!--打包后的lib目录 包含lib下的war包 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<webResources>
<resource>
<directory>${project.basedir}/lib</directory>
<targetPath>WEB-INF/lib/</targetPath>
<includes>
<include>**/*.jar</include>
</includes>
</resource>
</webResources>
</configuration>
</plugin>

maven指定seetings.xml执行命令

可以使用 -s 或 --settings 选项来指定

假设你有一个自定义的 settings.xml 文件位于 /path/to/custom-settings.xml,你可以使用以下命令来指定它:

mvn -s /path/to/custom-settings.xml clean install

或者使用 --settings 选项:

mvn --settings /path/to/custom-settings.xml clean install

maven上传到私有仓库配置

1.配置 settings.xml

需要在 settings.xml 文件中配置私有仓库的服务器信息,包括用户名和密码。假设私有仓库是 Nexus 或 Artifactory。

    <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">
<servers>
<server>
<id>my-private-repo</id>
<username>your_username</username>
<password>your_password</password>
</server>
</servers>

<mirrors>
<mirror>
<id>my-private-repo</id>
<mirrorOf>*</mirrorOf>
<url>http://localhost:8081/repository/maven-releases/</url>
</mirror>
</mirrors>

<profiles>
<profile>
<id>release</id>
<repositories>
<repository>
<id>my-private-repo</id>
<url>http://localhost:8081/repository/maven-releases/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>my-private-repo</id>
<url>http://localhost:8081/repository/maven-releases/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>

<activeProfiles>
<activeProfile>release</activeProfile>
</activeProfiles>
</settings>

2.pom.xml配置

在项目的 pom.xml 文件中配置部署信息。

    <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>com.example</groupId>
<artifactId>my-project</artifactId>
<version>1.0.0</version>
<distributionManagement>
<repository>
<id>my-private-repo</id>
<url>http://localhost:8081/repository/maven-releases/</url>
</repository>
<snapshotRepository>
<id>my-private-repo</id>
<url>http://localhost:8081/repository/maven-snapshots/</url>
</snapshotRepository>
</distributionManagement>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<version>3.0.0-M1</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.2.1</version>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.4.1</version>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

3.上传到私有仓库

使用以下命令将项目上传到私有仓库:

mvn deploy

或者,如果你只想生成并部署 JAR 包:

mvn clean package deploy

本地仓库同步到私有仓库工具

可以使用migrate-local-repo-tool.jar

下载地址:https://agent-install.oss-cn-hangzhou.aliyuncs.com/migrate-local-repo-tool.jar?spm=a2c40.rdc_maven_repo.0.0.9a383054RvNKLP&file=migrate-local-repo-tool.jar

执行命令

java -jar migrate-local-repo-tool.jar -cd "%homepath%/.m2/repository/" -t "https://packages.aliyun.com/6556f38db322becc90a55fe6/maven/isesol" -u username -p password