简体   繁体   中英

Gradle with maven-publish: how to autoversion and publish non-gradle jars in a directory to Artifactory

As a short-term option for migrating from ant to gradle, we want to use gradle only at the start and end of our ant build rather than running ant from within gradle.

Basically we want to use gradle to fetch dependencies to a directory, then run the ant build which places all specified jars in a directory, then use gradle to publish artifacts from that directory to Artifactory.

This means that gradle will not actually build the artifacts -- it will instead find them in a directory, but we would still like them versioned (since they will go to artifactory)

So we are looking to use Gradle to dynamically version and publish all files in a directory.

Reviewing the gradle documentation, it appears Gradle and the artifactory-publish plugin are focused on more static definitions of 'what modules to publish'. I'm having difficulty getting Gradle to properly publish a dynamic count of files from a directory.

I've tried the following (condensed), but it does not work so far in my testing (because the MavenPublication name seems to require static text) -- and the artifactory-publish bit does not currently loop over the proper set of MavenPublications:

apply plugin: 'java'
apply plugin: 'maven-publish'
apply plugin: 'artifactory-publish'

version = new Version( 1, 0, System.env.BUILD_NUMBER )   // set by Jenkins

ext.filesToPublish=['foo.jar', 'bar.ear']

publishing {
   publications {
      def i=0;
      for ( s in filesToPublish ) {
         ++i
         def vname="artifact" + i;
         $vname(MavenPublication) {
            artifact s
            artifactId vname
         }
      }
   }
}

artifactory {
   contextUrl = 'http://.../artifactory'
   publish {
      repository {
         repoKey='...'
         username='...'
         password='...'
      }
      defaults {
         publications('artifact0')
         publications('artifact1')
         publications('artifact2')
      }
   }
   resolve {
      repository {
         repoKey='...'
      }
   }
}

I am very interested if anyone has figured out a way to dynamically publish all files found a directory to Artifactory.

Is there a better approach, such as some way to define components like the Java plugin does?

I also tried dynamically creating MavenPublication objects, but couldnt get that working. Would that be a better approach?

Thanks!

EDIT: With additional research and a better understanding of Groovy, I was able accomplish this as follows -- note the use of double-quotes around the variable expansion -- "$fbase" as compared to before:

apply plugin: 'java'
apply plugin: 'maven-publish'
apply plugin: 'artifactory-publish'

version = new Version( 1, 0, System.env.BUILD_NUMBER )   // set by Jenkins

ext.filesToPublish = []
ext.dir= new File("pub")
ext.dir.eachFile( groovy.io.FileType.FILES ) { file ->
   ext.filesToPublish << file
}

publishing {
   publications {
      for ( f in filesToPublish ) {
         def fbase=f.getName().split("\\.")[0]
         "$fbase"(MavenPublication) {
            artifact f
            artifactId fbase
         }
      }
   }
}


artifactory {
   contextUrl = 'http://.../artifactory'
   publish {
      repository {
         repoKey='...'
         username='...'
         password='...'
      }
      defaults {
         for ( f in filesToPublish ) {
           def fbase=f.getName().split("\\.")[0]
           publications( fbase )
         }
      }
   }
   resolve {
      repository {
         repoKey='...'
      }
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM