Maven


Maven

1.Maven 配置文件(阿里云镜像配置)

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

  <proxies></proxies>

  <localRepository>/Users/anubis/maven_repository</localRepository>

  <servers></servers>

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

  <profiles>
  	<profile>
  	  <id>aliyun</id>
  	  <repositories>
  	  	<repository>
  	  	  <id>central</id>
  	  	  <url>https://maven.aliyun.com/repository/central</url>
  	  	  <releases>
  	  	    <enabled>true</enabled>
  	  	  </releases>
  	  	  <snapshots>
  	  	    <enabled>true</enabled>
  	  	  </snapshots>
  	  	</repository>
  	  </repositories>
  	</profile> 
  </profiles>

  <activeProfiles>
    <activeProfile>aliyun</activeProfile>
  </activeProfiles>
</settings>

2.Maven 配置文件(共公仓库与第三方仓库混合配置)

  • 注:混合配置共公仓库和第三方仓库,需要在setting.xml增加新的mirror和profile,并激活新的activeProfile。

mirror

mirrorOf代表了一个镜像的替代位置,其中,

  • *: 匹配所有,所有内容都从镜像拉取
  • external:*: 除了本地缓存的所有从镜像仓库拉取
  • repo,repo1: repo或者repo1,这里的repo指的仓库ID
  • *,!repo1: 除了repo1的所有仓库
<?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">
	<pluginGroups/>
	<proxies/>
	<localRepository>/Users/anubis/maven_repository_zj</localRepository>
	<!-- 镜像 -->
	<mirrors>
		<mirror>
			<id>maven-R1</id>
			<name>maven-R1</name>
			<url>http://xx.xx.xx.xx:xxxx/nexus/content/groups/public</url>
			<mirrorOf>central</mirrorOf>        
	    </mirror>
		<mirror>
			<id>maven-R2</id>
			<mirrorOf>maven-R2</mirrorOf>
			<name>maven-R2</name>
			<url>http://xx.xx.xx.xx:xxxx/nexus/content/groups/public/</url>
		</mirror>
 		<mirror>
			<id>alimaven</id>
			<mirrorOf>central</mirrorOf>
			<name>aliyun maven</name>
			<url>https://maven.aliyun.com/repository/public</url>
		</mirror>
	</mirrors>

	<servers>
		<server>
    		<id>maven-R1</id>
    		<username>admin</username>
    		<password>admin123</password>
    	</server>
    	<server>
    		<id>maven-R2</id>
    		<username>admin</username>
    		<password>admin456</password>
    	</server>
	</servers>


	<profiles>
		<!--新增:私有maven仓库配置-->
		<profile>
            <id>ali</id>
            <repositories>
                <repository>
                    <id>ali</id>
					<url>https://maven.aliyun.com/repository/public</url>
					<snapshots>
						<enabled>true</enabled>
						<updatePolicy>always</updatePolicy>
						<checksumPolicy>warn</checksumPolicy>
					</snapshots>
				</repository>
            </repositories>
			<pluginRepositories>
                <pluginRepository>
                    <id>aliyun</id>
                    <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
                    <releases>
                        <enabled>true</enabled>
                    </releases>
                    <snapshots>
                        <enabled>true</enabled>
                    </snapshots>
                </pluginRepository>
            </pluginRepositories>
        </profile>

		<profile>
            <id>maven-R1</id>
            <repositories>
                <repository>
                    <id>maven-R1</id>
					<url>http://xx.xx.xx.xx:xxxx/nexus/content/groups/public</url>
					<snapshots>
						<enabled>true</enabled>
						<updatePolicy>always</updatePolicy>
						<checksumPolicy>warn</checksumPolicy>
					</snapshots>
				</repository>
            </repositories>
        </profile>

		<profile>
			<id>maven-R2</id>
			<repositories>
				<repository>
					<id>maven-R2</id>
					<url>http://xx.xx.xx.xx:xxxx/nexus/content/groups/public/</url>
					<snapshots>
						<enabled>true</enabled>
						<updatePolicy>always</updatePolicy>
						<checksumPolicy>warn</checksumPolicy>
					</snapshots>
				</repository>
			</repositories>
		</profile>
	</profiles>
	<!--激活配置 -->
	<activeProfiles>
		<activeProfile>ali</activeProfile>
		<activeProfile>maven-R1</activeProfile>
		<activeProfile>maven-R2</activeProfile>
	</activeProfiles>
</settings>

3.清理本地maven仓库

# 创建脚本
vi maven_clean.sh
# 赋权
chmod +777 ./maven_clean.sh
#!/bin/bash
# 定义 Maven 仓库路径
repo_path="/Users/anubis/maven_repository"  # 请根据实际情况修改路径
# 删除所有以 .lastUpdated 结尾的文件
find $repo_path -name "*.lastUpdated" -type f -delete
# 删除空文件夹
find $repo_path -type d -empty -delete

4.Maven依赖上传

  • 注:Nexus 私有仓库的public仓库一般不能直接上传,需要上传到其他新建的仓库,并注意区分仓库 release 和 snapshot 版本库。

本地仓库

mvn install:install-file -Dfile=/路径/xxx-x.x.x.jar -DgroupId=xxx -DartifactId=xxx -Dversion=x.x.x -Dpackaging=jar

远程仓库

mvn deploy:deploy-file -DgroupId=xxx -DartifactId=xxx -Dversion=xxx -Dpackaging=jar -Dfile=xxx的路径 -Durl=http://账号:密码@xx.xx.xx.xx:xxxx/nexus/content/repositories/snapshots

将本地仓库依赖批量上传到远程私有仓库

#!/bin/bash
# copy and run this script to the root of the repository directory containing files
# this script attempts to exclude uploading itself explicitly so the script name is important
# Get command line params
while getopts ":r:u:p:" opt; do
   case $opt in
   	r) REPO_URL="$OPTARG"
   	;;
   	u) USERNAME="$OPTARG"
   	;;
   	p) PASSWORD="$OPTARG"
   	;;
   esac
done

find . -type f -not -path './mvnimport\.sh*' -not -path '*/\.*' -not -path '*/\^archetype\-catalog\.xml*' -not -path '*/\^maven\-metadata\-local*\.xml' -not -path '*/\^maven\-metadata\-deployment*\.xml' | sed "s|^\./||" | xargs -I '{}' curl -u "$USERNAME:$PASSWORD" -X PUT -v -T {} ${REPO_URL}/{} ;
# -u maven私服用户名
# -p maven私服用密码
# http://私服ip:端口/repository/目标仓库/
./mavenimport.sh -u admin -p admin123 -r http://xx.xx.xx.xx:xxxx/repository/test/

(脚本原文链接)


文章作者: Anubis
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 Anubis !
评论
  目录