how to copy all source jars using gradle

前端 未结 3 1863
梦如初夏
梦如初夏 2020-12-14 23:40

We have an old playframework 1.2.x version where we copy all the jars to project/lib so playframework can consume them. We would LOVE to copy all the source jars as well so

相关标签:
3条回答
  • 2020-12-15 00:08

    Here eskatos solution translated in Kotlin DSL:

    tasks {
        "copySourceJars"(Copy::class) {
            val sources = configurations.runtime.resolvedConfiguration.resolvedArtifacts.map {
                with(it.moduleVersion.id) { 
                    dependencies.create(group, name, version, classifier = "sources") 
                }
            }
            from(
                configurations.detachedConfiguration(*sources.toTypedArray())
                    .resolvedConfiguration.lenientConfiguration.getFiles(Specs.SATISFIES_ALL)
            )
            into(File("some-directory"))
        }
    }
    
    0 讨论(0)
  • 2020-12-15 00:19

    this is not that straight forward as expected. The following snippet copies the source jars for all dependencies (runtime + compile) of a java project into a specific folder:

        task copySourceJars(type:Copy){
            def deps = configurations.runtime.incoming.dependencies.collect{ dependency ->
                dependency.artifact { artifact ->
                        artifact.name = dependency.name
                        artifact.type = 'source'
                        artifact.extension = 'jar'
                        artifact.classifier = 'sources'
                    }
                dependency
            }
            from(configurations.detachedConfiguration(deps as Dependency[]).resolvedConfiguration.lenientConfiguration.getFiles(Specs.SATISFIES_ALL))
            into('sourceLibs')
        }
    

    The reason we use a lenientConfiguration here is, that we don't want to fail if a source artifact cannot be resolved. There might be a more elegant way, but I havn't looked deeper into that.

    hope it helps,

    René

    0 讨论(0)
  • 2020-12-15 00:26

    Rene answer will download sources jars of direct dependencies, not sources jars of all transitives dependencies.

    Here is a task that will do the trick:

    task copySourceJars( type: Copy ) {
      def sources = configurations.runtime.resolvedConfiguration.resolvedArtifacts.collect { artifact ->
        project.dependencies.create( [
          group: artifact.moduleVersion.id.group,
          name: artifact.moduleVersion.id.name,
          version: artifact.moduleVersion.id.version,
          classifier: 'sources'
        ] )
      }
      from configurations.detachedConfiguration( sources as Dependency[] )
        .resolvedConfiguration.lenientConfiguration.getFiles( Specs.SATISFIES_ALL )
      into file( 'some-directory/' )
    }
    

    One can then do the very same for javadocs jars by changing the classifier to javadoc.

    0 讨论(0)
提交回复
热议问题