Gradle project dependency does not reference SNAPSHOT jar

903 views Asked by At

I am trying to create a fat jar file in a multi-project Gradle build, something like the following:

root
  +-- project1
  +-- project2

project1 provides the basic functionality, which is then used by project2 to create an executable jar. The executable JAR needs to contain all of the code from the dependencies so that it can be run standalone.

For external dependencies this works fine. In project2 I create a new configuration:

apply plugin: 'maven'
apply plugin: 'java'

configurations {
  // configuration for JARs that need to be included in the final packaging
  includeInJar
}

and then add the dependencies:

dependencies {
  includeInJar 'com.fasterxml.jackson.core:jackson-databind:2.2.3'
  ...

  configurations.compile.extendsFrom(configurations.includeInJar)
}

The packaging then looks like this:

jar {
  manifest {
    attributes "Main-Class": "com.acme.project1.MyTest"
  }
  // import all dependencies into the Jar so that it can be run easily
  from {
    configurations.includeInJar.collect {
      it.isDirectory() ? it : zipTree(it)
    }
  }
}

The jar is correctly built with all of the files from the external dependencies. The problem comes with the project dependency to project1:

includeInJar project(':project1')

When this is present, I get an error when it tries to assemble the JAR that it can't find the jar (e.g. project1-0.4.0.jar) in the project1 build/libs directory as it does not exist. The message is correct, as project1 builds a SNAPSHOT jar (e.g. project1-0.4.0-SNAPSHOT.jar).

The question is, why does the configuration refer to the non-SNAPSHOT jar when the project is building SNAPSHOT jars? What can I change so that it finds the correct jar?

2

There are 2 answers

1
couettos On

As a comment :

In my opinion, fatjar is not a great pattern. Maybe Gradle application plugin would fit your need ?

0
Paul On

I found the answer to my own question.

The problem was that we have some additional scripting at the project level which is apparantly making a change to the version at the end of the configuration phase.

When the fat jar configuration is assembled, the 'plain' version is used. This is then changed to the -SNAPSHOT version before the jars are built.

Moving the from { ... } code into the build phase by wrapping it in doFirst { ... } is enough to fix the problem, although the real fix is obviously to avoid changing the version in the middle of the in the first place.