2

I have few Java classes that I want to use on different projects.

I don't want to move these classes in a dedicated project for now.

So I want to build a JAR with these classes, and be able to use it in my other projects, all with Gradle.

So here my JAR task (sources) and I publish it as an artifact :

task utilitiesJar(type: Jar) {
    baseName = 'utilities'
    version =  '0.0.1'
    includeEmptyDirs = false
    from sourceSets.main.allJava
    include "**\\common\\exceptions\\**"
    include "**\\common\\json\\**"
    include "**\\common\\logging\\**"
}

publishing {
    publications {
        utilities(MavenPublication) {
            artifact utilitiesJar
            groupId group
            artifactId utilitiesJar.baseName
            version utilitiesJar.version
        }
    }
    repositories {
        maven {
            url 'my_URL'
        }
    }
}

I get it back with an other project :

repositories {
    mavenCentral()

    maven {
        url 'my_URL'
    }
}

...
compile (...)
...

Seems like the JAR is correctly imported (I can see it in "External Libraries" of IntelliJ, with all its classes), but I can't use it. Maybe because the .class files are missing ? I'm beginner in Java, maybe I missed something.

How can I create a JAR with only some classes and then use it ?

FloFlow
  • 121
  • 1
  • 8
  • You can create a jar file from classes, use that Jar in gradle using local dependency. See the link https://stackoverflow.com/questions/20700053/how-to-add-local-jar-file-dependency-to-build-gradle-file. – Sambit May 15 '19 at 13:19
  • @Sambit that's not what he wants. – vilpe89 May 15 '19 at 13:23
  • After creating the jar, you can push it Artifactory or Nexus if you have so that jar file can be used by any project regardless of whether it is a gradle or maven project. – Sambit May 15 '19 at 13:26
  • Ok I completed my question, maybe it's more clean now – FloFlow May 15 '19 at 13:26
  • 1
    Have you inspected the JAR file to see if it contains the classes you expect? (by the way, this situation is exactly what sub-projects are for) – Slaw May 15 '19 at 14:11
  • Yes, the JAR contains all the classes (.java, not .class), and I can explore it in my IDE. Just can't use it... – FloFlow May 15 '19 at 16:16
  • 1
    You need the binaries if you want to use the JAR as a dependency, not the sources. You should be packaging the classes from the main source set's _output_ directory. You may also want to make your task depend on the `classes` task. – Slaw May 15 '19 at 18:08
  • Ok thanks that answer my question ! – FloFlow May 17 '19 at 15:26

1 Answers1

3

Ok so as said in comments, I have to include the builded .class files, I can't use external .java classes like this.

So my solution :

def utilitiesName = '...'
def utilitiesVersion = '0.0.1'

task utilitiesJar(type: Jar, dependsOn: classes) {
    baseName = utilitiesName
    version =  utilitiesVersion
    includeEmptyDirs = false
    from sourceSets.main.output
    include ("**\\common\\exceptions\\**\\*", "**\\common\\json\\**\\*", "**\\common\\logging\\**\\*")
}

task utilitiesSourcesJar(type: Jar, dependsOn: classes) {
    baseName = utilitiesName
    version =  utilitiesVersion
    classifier = 'sources'
    includeEmptyDirs = false
    from sourceSets.main.allJava
    include ("**\\common\\exceptions\\**\\*", "**\\common\\json\\**\\*", "**\\common\\logging\\**\\*")
}

publishing {
    publications {
        utilities(MavenPublication) {
            artifact utilitiesJar
            artifact utilitiesSourcesJar
            groupId group
            artifactId utilitiesName
            version utilitiesVersion
        }
    }
    repositories {
        maven {
            url 'myURL'
        }
    }
}

Now I can use it and see the classes in my IDE.

PS : doing in this way is pretty dirty. Create a sub-project / a module, it's just the way how to do it, that's finaly what I did.

FloFlow
  • 121
  • 1
  • 8