Question

Building a Java/Groovy project, various tasks like compileJava, compileGroovy, test, etc requires various jar artifacts which Gradle provides them if you have properly defined what you need for each area of that task.

I'm making those available to the build script (build.gradle) and everything is working fine.


One of my other project that I'm working on, requires not only jar rtifacts but also an .xml file as an artifact for doing JIBX / XSLT transformation/processing.

My simple question: - Gradle build process know how to fetch artifacts from Artifactory (as I have mentioned those Artifactory repositories in init.d/common.gradle file) and during the build it feeds the compile/test etc tasks with those jars, now if I have this .xml artifact uploaded to Artifactory as well, then:

a. How can I get the .xml artifact available to me in build.gradle so that I can perform some operation on it; for ex: Copy that .xml file to a x/y/z folder in a resultant project's jar/war file. Those jar files I can access via project.configurations.compile.each or .find or something like that but I'm not sure if I can access the .xml file the same way. The following code works fine for unjaring a jar file in build/tmpJibx/ folder i.e. if I need httpunit-1.1.1.jar during my build, then the following function when called, will create/unjar this jar in build/tmpJibx/httpunit folder.

// Unpack jar
def unpackJarItem( jarName ) {
   println 'unpacking: ' + jarName
   def dirName = "$buildDir/tmpJibx/$jarName"
   new File( dirName ).mkdirs()
   project.configurations.compile.find {
      def nameJar = it.name
      def iPos = nameJar.lastIndexOf( '-' )
      if( iPos > 0 ) {
         nameJar = nameJar.substring( 0, iPos )
         if( nameJar == jarName ) {
            def srcJar = it.toString()
            ant {
               unjar( src: "$srcJar", dest: "$dirName" )
            }
         }
      }
   }
}

Gradle maintains artifacts in its cache at user's ~(home directory inside ~/.gradle) or C:\Users\.gradle under ...\caches..\artifactory..\filestore.........*

All Im trying to achieve is: If I can do something like below:

copy {
      into "build/war/WEB-INF/conf"
      from "......<THIS_PATH_IS_WHAT_Im_LOOKING_FOR>:
      include "thatxmlartifactfile.xml"
}

I tried defining the entry under dependencies { ... } section, like below, but I'm not sure if Gradle will automatically have access to it somehow as Gradle is so great.

dependencies {
   compile 'groupid:artifactid:x.x.x'
   compile group: 'xxxx', artifac...: 'yyyy', version: 'x.x.x'
   //for ex:
   compile 'httpunit:httpunit:1.1.1'

   jibxAnt 'groupidnameofxmlfile:artifactidnameofxmlfile:versionnumberofxml@xml"
   ...

   ...
   ....

}

It seems like I have to first copy that .xml from where ever Gradle know it's available to some location n then from that location to my target folder.

   // Add libraries for acceptance tests
   project.configurations.acceptanceTestCompile.each { File f ->
      if( f.isFile() ) {
         def nameJar = f.getName()
         def jarName = f.getName()
         def fileInc = true
         def iPos = nameJar.lastIndexOf( '-' )
         if( iPos > -1 ) {
            jarName = nameJar.substring( 0, iPos )

            // Here I can say that one of the file/entry will be that .xml file
            // Now I have that in jarName variable and I can play with it, right?
            // i.e. if jarName == name of that xml file, then 
            copy {
                  into "some/folder/location"
                  from jarName
            }
         }
      }
   }
Was it helpful?

Solution

The easiest solution is to commit the XML file to source control. If you put it under src/main/webapp/WEB-INF/conf/thatxmlartifactfile.xml, it will get included in the War automatically.

If you need to get the file from Artifactory, you can do so as follows:

configurations {
    jibx
}

dependencies {
    jibx "some.group:someArtifact:1.0@xml"

war {
    from { configurations.jibx.singleFile }
}

PS: It's often possible, and also preferable, to add files directly to the final archive, rather than going through intermediate copy steps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top