构建gradle插件

时间:2018-05-26 14:37:57

标签: java gradle groovy gradle-plugin

我正在开发一个gradle插件以及一个项目:

plugin_consumer
  |
  + build.gradle
  + src/

plugin
  |
  + build.gradle 
  + libs/
  + src/

构建后所有jar都移动到plugin/libsplugin consumer模块使用插件,如下所示:

buildscript {
  repositories {
  // ...
  }
  dependencies {
    classpath fileTree(dir:'../plugin/libs', include: ['*.jar'])
  }
} 

apply plugin: 'my.custom.plugin'

我测试过,这个设置运行正常,但插件.jar正在构建时没有依赖关系,一旦我向项目添加了一些依赖项,消费者buildcript就会因NoClassDefFoundError异常而崩溃。问题是,当我构建一个胖jar 时,脚本不再找到插件:

// from plugin/build.gradle 
// Configurations to build jar with dependencies
configurations {
  jar.archiveName = 'plugin.jar'
}
jar {
  archiveName = 'plugin.jar'
  baseName = project.name + '-all'
  from { configurations.compile.collect {
    println it
    it.isDirectory() ? it : zipTree(it)
  } }
}

plugin_consumer项目上运行命令会引发错误:

> Failed to apply plugin [id 'my.custom.plugin']
   > Plugin with id 'my.custom.plugin' not found.

1 个答案:

答案 0 :(得分:0)

我还没有发现问题,但找到了解决方案。不知何故,包装所有源的jar指令搞砸了插件jar文件。以下配置适用于插件方面:

//...
jar {
  from {
    configurations.compileWithDependencies.collect { it.isDirectory() ? it : zipTree(it) }
  }
}

configurations {
  compileWithDependencies
}

dependencies {
  compile gradleApi()
  compileWithDependencies 'my.dependency1'
  compileWithDependencies 'my.dependency1'
  testCompile 'test.dependency'

  configurations.compile.extendsFrom(configurations.compileWithDependencies)
}

从它的外观来看,我猜不应该将gradleApi()个依赖项编译到插件jar中。