How to build a jar from some other subprojects compiled classes and ressources?

In one of my subprojects I want to build a jar which should include only some class files from some subproject. So I tried the following task:

task jarJadiceWorker(type: Jar) {
 archiveName = 'uvdmsWorker'
     dependsOn ':de.uvdms.jadice.common:compileJava'
 dependsOn ':de.uvdms.jadice5.server:compileJava'
    into ('/') {
    from project(':de.uvdms.jadice.common') {
   include '**/BGStampAnnotation.class'
      from project(':de.uvdms.jadice5.server') {
   include '**/PagesRotate*.class'
   include '**/SignatureAnno.class'
   include '**/StickyNoteAnno.class'
   include '**/Uvdms*.class'

But this is rejected by Gradle with this message:

Cannot convert the provided notation to a File or URI: project ':de.uvdms.jadice.common'.

I also tried with something like this:

from project(':de.uvdms.jadice5.server').sourceSets.main.output.classesDir { : gibt: java.lang.NullPointerException (no error message)

but this results in a NullPointerException, because Gradle does not find an output-Method.

So, who should I do this?

I was able to solve my problem with this:

task jarJadiceWorker(type: Jar) {
 archiveName = "uvdmsWorker-${version}.jar"
     dependsOn ':de.uvdms.jadice.common:compileJava'
 dependsOn ':de.uvdms.jadice5.server:compileJava'
      from fileTree(dir: '../de.uvdms.jadice.common/build/classes/main').matching
      include '**/*BGStampAnnotation.class'
   from fileTree(dir: '../de.uvdms.jadice5.server/build/classes/main').matching {
      include '**/*PagesRotate.class'
  include '**/*SignatureAnno.class'
  include '**/*StickyNoteAnno.class'
  include '**/*Uvdms*.class'
   into ('/')

Unfortunately, this includes all found subdirectories from those ‘…/build/classes/main’- directories as empty subfolders. Unschön! Is there a way to exclude them?

Then, I’m not happy with a low level API fiddling around with ‘build/classes/main’-Directories. I thought it would be possible to work with the project API.

Another strange thing I met: when make the ‘from’ declaration as a closure to 'into, then it works only with one ‘from’ declaration, not when useing two or more.

You can use

includeEmptyDirs = false

to get rid of empty directories:

Hi Marcin,

this is not what I need here. My goal is: have a special jar task which creates a jar from selected and compiled classes which reside in some other subprojects. These subprojects itself create their usual jars from all sources within them. But there is a need to create this special jar for some purposes, and only a few classes from those other subprojects are needed herein.


Hey carlo.if

One of the ways to approach this problem could be something like this (in root project):

def fooJar = task("fooJar", type: Jar) {
  baseName = "foo-jar"
  subprojects {
  //you can filter and only apply the configuration to the specific projects
  project.plugins.withId("java") {
       fooJar.from sourceSets.main.output.asFileTree.matching {
         include '**/SomeClass.class'
        include '**/SomeOther*'

It’s not ideal, but puts on the right tracks I think.

1 Like