gradlegroovyjbossear

Gradle Ear Plugin: Adding modules without version specifier


I am trying to assemble an ear with Gradle, which application.xml contains some non-ejb modules. The dependent project archives are generated with the {basename}-{version}.{extension} standard and thus look something like, e.g. subproject-0.2.6.rar. However, since some of these projects need to be marked as java or connector module, I have issues addressing the correct files.

This is what the build script looks like:

apply plugin: 'ear'

dependencies {
    deploy project(':EJB-Project')
    deploy project(':Commons')
    deploy project(':UpdateManager')

    earlib 'net.sf:jasperreports:unknown'
    earlib 'org.apache.axis:axis:1.4'
}

ear {
    deploymentDescriptor {
        applicationName = 'EnterpriseProject'

        module('Commons.jar', 'java')
        module('UpdateManager.rar', 'connector')
    }

    // Remove the version postfix from archived files
    rename(/(.*)-${version}(.)/, /$1$2/)
}

The jboss-deployment-structure.xml has the archives in the sub-deployment nodes declared without the version specifier, too.

<jboss-deployment-structure xmlns="urn:jboss:deployment-structure:1.2">
       ...
    <sub-deployment name="EJB-Project.jar">
        <dependencies />
        <exclusions />
    </sub-deployment>
    <sub-deployment name="Commons.jar">
        <dependencies />
        <exclusions />
    </sub-deployment>
        ...

The rename method in the ear task successfully renames the libraries from the deploy configuration, but Gradle adds the dependencies, which are not explicitly added via module(String path, String identifier) automatically with their original name as ejb module:

<module>
    <ejb>EJB-Project-4.5.2.jar</ejb>
</module>

I would love to solve this in a clean way - e.g., referencing the project itself in the module section, rather than specifying the file name (and its extension) and generating the deployment xml for JBoss, but I'm somehow stuck. I want all version strings to be removed, or keep all of them.


Solution

  • I solved something similar like this in gradle 6.8:

    I started from here:

    plugins {
        id 'ear'
    }
    
    dependencies {
        deploy      'org.cpp.pp:dmReports:5.04.01-2'
        deploy      project(':reporting-transpoLib')
    }
    
    ear {
        deploymentDescriptor {
            module('dmReports-5.04.01-2.jar', 'java')
            module("reporting-transpoLib-${project.version}.jar", 'java')
        }
    }
    

    which produced an ear containing jars with versions and deployment descriptor with versions.

    But I had also a second war specifying in it's deployment descriptor a dependency to dmReports.

    For each new version of dmReports, deployed via this ear, I had to updated also the war deployment descriptor (dependency section)- which was not ok.

    I needed to remove the dmReports version from the ear and from war deployment descriptor.

    So I changed to this the ear:

    plugins {
        id 'ear'
    }
    
    configurations {
        dmReports
        reportingTranspoLib
    }
    
    dependencies {
        dmReports          'org.cpp.pp:dmReports:5.04.01-2'
        reportingTranspoLib project(':reporting-transpoLib')
    }
    
    // copyToLib task copies the dependencies and rename them (remove version) in a separate 
    // folder: ${buildDir}/lib. This folder will be used as input for deploy configuration
    
    task copyToLib {
        dependsOn(':reporting-transpoLib:build')
    
        doFirst {
            def  configNames = [ 'dmReports', 'reportingTranspoLib']
            configNames.each { configName ->
                configurations.getByName(configName).resolvedConfiguration.resolvedArtifacts.each { artifact ->
                    project.copy {
                        from artifact.file
                        into "${buildDir}/lib"
                        rename { "${artifact.name}.${artifact.extension}" }
                    }
                }
            }
        }
    }
    
    // the output of copyToLib task (folder ${buildDir/lib}) is taken and used by configuration deploy
    
    dependencies {
        deploy files("${buildDir}/lib")
    }
    
    
    ear {
        dependsOn(copyToLib)
    //    from(copyToLib)
        deploymentDescriptor {
            module('dmReports.jar', 'java')
            module("reporting-transpoLib.jar", 'java')
        }
    }
    

    So I obtained an ear containing jars without versions and deployment descriptor without versions. Was very usefull.