You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Gradle中使用本地Jar包避免下载公司插件

本地配置使用公司Gradle插件Jar包

我们公司的com.companyname.app插件托管在Maven Central,是所有项目/子模块的基础依赖。现有项目的典型配置如下:


现有项目配置示例

基础应用的settings.gradle

pluginManagement {
  repositories {
    mavenCentral() {
      content {
        excludeGroup 'com.companyname'
      }
    }
    maven {
      url 'https://repository.companyname.com/link/to/repository/maven-public/'
    }
  }
  plugins {
    id 'com.companyname.app' version '7.1.1'
  }
}

plugins {
  id 'com.github.node-gradle.node' version '3.5.1' apply false
}


dependencyResolutionManagement {
  repositories {
    mavenCentral() {
      content {
        excludeGroup 'com.companyname'
      }
    }
    maven {
      url 'https://repository.companyname.com/link/to/repository/maven-public/'
    }
    // Declare the Node.js download repository
    ivy {
      name = "Node.js"
      setUrl("https://nodejs.org/dist/")
      patternLayout {
        artifact("v[revision]/[artifact](-v[revision]-[classifier]).[ext]")
      }
      metadataSources {
        artifact()
      }
      content {
        includeModule("org.nodejs", "node")
      }
    }
  }
}

//Include all modules
rootProject.name = "companyname-productname"

def modules = []
file("modules").traverse(type: groovy.io.FileType.DIRECTORIES, maxDepth: 1) { it ->
  if (new File(it, "build.gradle").exists()) {
    modules.add(it)
  }
}

gradle.ext.appModules = modules

modules.each { dir ->
  include "modules:$dir.name"
  project(":modules:$dir.name").projectDir = dir
}

基础应用的build.gradle

plugins {
  id 'com.companyname.app'
}

companyname {
  title "CompanyName ProductName"
  description "CompanyName Enterprise Application"
}

allprojects {
  apply plugin: 'idea'
  apply plugin: 'eclipse'

  group = 'com.companyname.apps'
  version = '7.1.5'

  java {
    toolchain {
      languageVersion = JavaLanguageVersion.of(11)
    }
  }

  afterEvaluate {
    test {
      useJUnitPlatform()
      beforeTest { descriptor ->
        logger.lifecycle('Running: ' + descriptor)
      }
    }
  }

  configurations {
    runtimeClasspath.exclude group: "org.eclipse.birt.runtime.3_7_1", module: "org.apache.commons.codec"
  }
}

tasks.withType(PublishToMavenRepository).configureEach { it.enabled = false }

dependencies {
  gradle.appModules.each { dir ->
    implementation project(":modules:$dir.name")
  }
}

子模块的build.gradle

plugins {
    id 'com.companyname.app'
}

companyname {
    title "companyname :: companyname accounting"
    description "companyname accounting module"
}

dependencies {

    // module dependencies
    api "com.companyname.apps:module-1:5.0.0"
    api "com.companyname.apps:module-2:7.1.0"
    
}

问题场景

克隆新模块实例首次构建时,需要拉取插件相关Jar包,但偶尔会因服务器响应缓慢出现超时问题。已拥有所需的本地插件Jar包,但由于该Jar包是作为Gradle插件而非普通依赖使用,不清楚如何配置让项目优先使用本地Jar。


解决方法

方法1:使用本地文件系统仓库(推荐)

在settings.gradle的pluginManagement中添加本地目录作为优先级最高的仓库,让Gradle优先从本地加载插件:

pluginManagement {
  repositories {
    // 本地仓库优先级最高
    maven {
      url = uri('/path/to/your/local/jar/directory') // 替换为本地Jar所在目录
    }
    mavenCentral() {
      content {
        excludeGroup 'com.companyname'
      }
    }
    maven {
      url 'https://repository.companyname.com/link/to/repository/maven-public/'
    }
  }
  plugins {
    id 'com.companyname.app' version '7.1.1'
  }
}

需确保本地目录遵循Maven仓库结构:com/companyname/app/com.companyname.app.gradle.plugin/7.1.1/,目录内放置插件Jar和对应的POM文件(若没有POM,可从私有仓库下载对应版本的POM文件放入)。

方法2:直接引用本地Jar文件

若不想维护本地仓库结构,可在settings.gradle中通过解析策略直接指定本地Jar:

pluginManagement {
  resolutionStrategy {
    eachPlugin {
      if (requested.id.id == 'com.companyname.app') {
        useModule('com.companyname.apps:com.companyname.app.gradle.plugin:7.1.1')
        artifact file('/path/to/your/local/plugin.jar') // 替换为本地Jar路径
      }
    }
  }
  repositories {
    mavenCentral() {
      content {
        excludeGroup 'com.companyname'
      }
    }
    maven {
      url 'https://repository.companyname.com/link/to/repository/maven-public/'
    }
  }
  plugins {
    id 'com.companyname.app' version '7.1.1'
  }
}

注意:useModule中的坐标需与插件的Maven坐标完全匹配,版本号要和本地Jar一致。

方法3:临时修改插件应用方式(快速测试用)

在项目根目录和所有子模块的build.gradle中,先通过buildscript引入本地Jar,再手动应用插件:

buildscript {
  dependencies {
    classpath files('/path/to/your/local/plugin.jar')
  }
}

// 注释掉原插件声明
// plugins {
//   id 'com.companyname.app'
// }

// 直接应用插件
apply plugin: 'com.companyname.app'

// 其余配置保持不变
companyname {
  title "CompanyName ProductName"
  description "CompanyName Enterprise Application"
}
// ...

内容的提问来源于stack exchange,提问作者PVI-AXL

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.20 09:39:51