Single Source Project creating and upload Mutiple Jars with Different Main-Class to Maven Repository


(ted) #1

I want to make two jar files from single source code and build.gradle. Two jar files have different Main-class attribute in manifest file.
And I want to upload jar archives to private maven repository.
How should I do ?

I made build.gradle like below. buildA and buildB will make two jar files, jarA-0.1.jar and jarB-0.2.jar. After executing buildA and buildB, however, uploadArchives doesn’t work well.

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

sourceSets.main.java.srcDirs = ['src']

sourceCompatibility = '1.8'
targetCompatibility = '1.8'

compileJava.options.encoding = 'UTF-8'
compileTestJava.options.encoding = 'UTF-8'

// Normal jar task will make dummy.jar
jar.baseName = 'dummy'
group = 'com.example'

// Not to upload dummy.jar to maven repository
def jarFilesList = file("build/libs").listFiles(new FilenameFilter() {
    @Override
    boolean accept(File dir, String name) {
        return name.indexOf("dummy.jar") == -1
    }
})

repositories {
    mavenCentral()
}

dependencies {
    // dependencies info
}

task buildA(type: Jar, dependsOn: jar) {  // make build/libs/jarA-0.1.jar
    buildA.baseName = "jarA"
    version = "0.1"
    manifest {
        attributes 'Main-Class': "com.example.classA"
    }
    from configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
}

task buildB(type: Jar, dependsOn: jar) {  // make build/libs/jarB-0.2.jar
    buildB.baseName = "jarB"
    version = "0.2"
    manifest {
        attributes 'Main-Class': "com.example.classB"
    }
    from configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
}

artifacts {
    jarFilesList.each { f ->
        archives f
    }
}

uploadArchives {
    repositories {
        mavenDeployer {
            repository(url: 'http://private.repository.example.com/repository/')
            jarFilesList.each { f ->
                def id = f.name.replaceAll("-.+", "")  // "buildA" or "buildB"

                // get version information from jar file names
                def versionString = f.name.find(/-[0-9.]+/)
                def versionStringLength = versionString.size()
                def version = f.name.find(/-[0-9.]+/).substring(1, versionStringLength - 1)

                addFilter(id) { artifact, file ->
                    artifact.name == id
                }
                pom(id).artifactId = id
                pom(id).version = version
            }
        }
    }
}
gradle_dir
|--.gradle
|  |--(some files)
|--build.gradle
|--gradle
|  |--(some files)
|--gradlew
|--gradlew.bat
|--settings.gradle
|--src
|  |--com
|  |  |--example
|  |  |  |--classA.java
|  |  |  |--classB.java

(Chris Doré) #2

I think there’s a couple issues.

  1. jarFilesList is empty when running the build after a clean, since no files will exist in build/libs.
  2. When configuring the artifacts you’re using a File reference. Doing this does not configure a task dependency on the corresponding Jar task.

You should be able to resolve both of these by using references to the Jar task’s you’ve created instead of basing you configuration logic and data on files in the filesystem.