Create custom artifact using dependencies


(Avishay Balderman) #1

Hi
I need to create a custom artifact (a zip file) that should contain a collection of txt files and xml descriptor. My project structure is Maven based. The txt files and the xml descriptor are under src/main
I manage to do this using the task below:

task srcZip(type: Zip) {
  archiveName project.name + '_' + project.version + '.zip'
  destinationDir file('./target')
  include '**/**/*'
  from 'src/main'

}

The project may have a dependency in one or more artifacts and I want to represent those dependencies using

dependencies {

}

I am not using java plugin - the project has nothing with java.
The dependencies are zip files that contains the same type of txt files that were mentioned above.
Each dependency zip should be unzipped and the txt files should be merged into the artifact zip.
I am using
> apply plugin: 'maven-publish'
in order to deploy artifacts to maven repo.

I need a direction/example… Do I have to develop a custom plugin here?

thanks

Avishay


(Avishay Balderman) #2

Below is the code that creates a custom library (zip) and install it to local maven repo.

//
// A ‘Library’ packager
// Package ‘source’ code into zip and install it as Maven artifcat into local Maven repo
//
apply plugin: ‘maven-publish’

defaultTasks ‘customBuild’

group 'com.something’
version ‘1.0’

task srcZip(type: Zip) {
archiveName project.name + ‘_’ + project.version + ‘.zip’
destinationDir file(’./target’)
include '//*'
from ‘src/main’
}

task customClean(type: Delete) {
delete file(’./target’)
}

task customBuild(dependsOn: [‘customClean’, ‘publish’])
publish.mustRunAfter customClean

publishing {
publications {
zip(MavenPublication) {
artifact srcZip
}
}
repositories {
mavenLocal()
}
}

It works fine and it installs the artifact zip at the local Maven repo.
From another gradle build script (the ‘app’ script) I need to:

  1. Consume the artifact[s] (I would like to declare them as dependencies) I just created using the script above.
  2. Each dependency should be unpacked and the source code should be pushed into the final artifact

So the ‘app’ script should be able to declare dependencies (anyway to do it without java plugin?)
It should create a zip with its own sources and add the unpacked dependencies.

I hope I am more clear now with what I need help with.

thanks

Avishay


(Avishay Balderman) #3

Below is the solution for the ‘app’ script

//
// An 'Application' packager
// Package 'source' code into zip as well as the 'source' code of the dependencies 
//
apply plugin: 'java'

defaultTasks 'make'

version '1.0'

repositories {
     mavenLocal()
}

dependencies {
    runtime 'com.something:lib:1.0'
}

task customClean(type: Delete) {
    delete file('./target')
}

def getArtifactName() {
    return project.name + '_' + project.version + '.zip'
}

task createArtifact(type: Zip) {
    archiveName getArtifactName()
    destinationDir file('./target')
    project.configurations.runtime.each {
        def zipFile = file(it)
        from zipTree(zipFile)
    }
    include '**/**/*'
    from 'src/main'
}

task make(dependsOn: ['customClean','createArtifact'])
createArtifact.mustRunAfter customClean

It works.
Is there a cleaner way to implement it?