Maven
Java 使用「Maven 制品库」存放依赖包,使用 Maven 或 Gradle 等构建工具进行依赖管理和安装。本文以 Gradle 为例,而 Maven 老项目可使用命令一键升级到 Gradle:
gradle init --type pom
Gradle bin
Gradle 构建时会先下载 bin,官方下载链接在海外,内地用户访问可能很慢,建议切换为腾讯云镜像,即修改项目中的 gradle/wrapper/gradle-wrapper.properties
:
# 腾讯云镜像
distributionUrl=https\://mirrors.cloud.tencent.com/gradle/gradle-6.8.1-bin.zip
# 默认海外
# distributionUrl=https\://services.gradle.org/distributions/gradle-6.8.1-bin.zip
公共制品库
Maven 公共制品库在海外,CODING 持续集成已内置国内镜像加速,无需配置。如本地开发需要加速,按照以下代码修改 ~/.gradle/init.gradle
:
def repoConfig = {
all { ArtifactRepository repo ->
if (repo instanceof MavenArtifactRepository) {
def url = repo.url.toString()
if (url.contains('repo1.maven.org/maven2')
|| url.contains('jcenter.bintray.com')
|| url.contains('maven.google.com')
|| url.contains('plugins.gradle.org/m2')
|| url.contains('repo.spring.io/libs-milestone')
|| url.contains('repo.spring.io/plugins-release')
|| url.contains('repo.grails.org/grails/core')
|| url.contains('repository.apache.org/snapshots')
) {
println "gradle init: [buildscript.repositories] (${repo.name}: ${repo.url}) removed"
remove repo
}
}
}
// 当前所代理的外部仓库:
// https://repo1.maven.org/maven2/
// https://dl.google.com/dl/android/maven2
// https://jcenter.bintray.com/
// https://oss.sonatype.org/content/repositories/snapshots/
// https://repo.spring.io/plugins-release/
// https://repo.clojars.org/
// https://jitpack.io/
// https://developer.huawei.com/repo/
// http://maven.qingting.fm/
// http://spantus.sourceforge.net/maven/global/
// https://dl.bintray.com/thelasterstar/maven/
// https://dl.bintray.com/sergei-ivanov/maven/
// https://download.oracle.com/maven/
// https://maven.repository.redhat.com/ga
// https://repo.maven.apache.org/maven2/
maven { url 'https://mirrors.cloud.tencent.com/nexus/repository/maven-public/' }
}
allprojects {
buildscript {
repositories repoConfig
}
repositories repoConfig
}
私有制品库
使用 Maven 私有制品库需先获得用户名/密码,参考文档:《搭建团队级制品库》。
gradle.properties
将制品库地址和用户名/密码添加到项目的 build.gradle
同一级目录下的 gradle.properties
文件中:
codingArtifactsMavenUrl=https://codes-farm-maven.pkg.coding.net/repository/share/build/
codingArtifactsUsername=无需填写
codingArtifactsPassword=无需填写
build.gradle
repositories {
maven {
url codingArtifactsMavenUrl
credentials {
username = codingArtifactsUsername
password = codingArtifactsPassword
}
}
}
dependencies {
implementation 'com.tencent:cloudpay:1.6'
implementation '[GROUP_ID]:[ARTIFACT_ID]:[VERSION]'
}
本地构建
把用户名/密码作为参数传入构建命令:
./gradlew build -Dorg.gradle.project.codingArtifactsUsername=foo -Dorg.gradle.project.codingArtifactsPassword=bar
持续集成构建
把用户名/密码填入环境变量:
pipeline {
agent any
stages {
stage('检出') {
steps {
checkout([$class: 'GitSCM', branches: [[name: env.GIT_BUILD_REF]],
userRemoteConfigs: [[url: env.GIT_REPO_URL, credentialsId: env.CREDENTIALS_ID]]])
}
}
stage('编译') {
steps {
sh "./gradlew build -Dorg.gradle.project.codingArtifactsUsername=$CODING_ARTIFACTS_USERNAME -Dorg.gradle.project.codingArtifactsPassword=$CODING_ARTIFACTS_PASSWORD"
}
}
}
}
问题反馈 >
2023-08-22最近更新
感谢反馈有用
感谢反馈没用
在阅读中是否遇到以下问题?*
您希望我们如何改进?*
如果您希望得到回复,请留下您的邮箱地址。