Question

I have a multi-project build and in each of the projects I have several packages in the main src tree which need to be packaged up separately from the rest of the src as individual EJB artifacts:

com/foo/ejb/ejb1
com/foo/ejb/ejb2
...

There can be an arbitrary number of these EJBs in each project (0 or several). My question is, how do I create a task that can, per project, Jar each of these as separate artifacts after the regular compile/jar? I.e. So I end up with following artifacts for each project:

project.jar (usual Java classes) 
ejb1.jar (MyEjb1Home/Remote/Bean.class, META_INF/[descriptors])
ejb2.jar (MyEjb2Home/Remote/Bean.class, META_INF/[descriptors])
...

The task(s) should be something I can share to each project (so probably defined in root project) and that will automatically find EJB src and generate a jar for each, without having to explicitly define the EJBs in the subprojects (but perhaps a property set "containsEjbs" to narrow down subprojects that have EJBs).

I'm thinking along the lines of using a filetree to grab the src package (com.foo.ejb.*) and then iterating and Jar-ing - but its how to do this Jar-ing in the context of Tasks which is what I'm having trouble with.

Thanks for any help, Chris.

Was it helpful?

Solution 2

I ended up doing the following. In [root]/build.gradle:

afterEvaluate { project ->
    if (project.hasProperty('containsEjbs'))
    {
        def basePath = '/com/foo/ejb'
        def classesDir = project.sourceSets.main.classesDir
        def ejbRootClassesDir = file(classesDir.getPath() + basePath)
        def srcFileDirs = project.sourceSets.main.java.srcDirs.collect { file(it.getPath() + basePath) }.findAll { it.exists() && it.isDirectory() }
        def ejbDirs = srcFileDirs.collect { it.listFiles() }.flatten()
        def ejbs = ejbDirs.findAll { it.listFiles().findAll { f -> f.name == 'META-INF'} }
        ejbs.each { file ->
            task "jarEjbFor_$file.name" (type: Jar, dependsOn:classes) {
                baseName = "$file.name"
                classifier = 'ejb'
                from fileTree(file).include('META-INF/**')
                into ("$basePath/$file.name")
                {
                    from fileTree( ejbRootClassesDir.getPath() + "/$file.name" )
                }
            }
        }
    }

So if any projects have a property 'containsEjbs=true', then a tasks is added for each ejb package found under '/com/foo/ejb' in each respective (sub)project. We store META-INF with descriptors in same source tree as classes, so there may be tweaks needed for your set up.

OTHER TIPS

You can simply define a new Task of Type: Jar

task('ejb1', type: Jar) {
  //describe jar contents here as a CopySpec
}

This task should automatically be run in the 'assemble' build phase. Full description of features available is at http://www.gradle.org/0.9-rc-1/docs/userguide/userguide_single.html#artifact_management

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