Is there a “right way” to create skinny wars and an ear file in Gradle

Using Gradle (and Java, obviously), I would like to create a skinny war (without bans of dependents in the war) and include all the dependencies in the ear file. I did some digging, but I can't find any obvious skinny war tutorials for Gradle.

In Maven 2.2.1 (yes, we are stuck in 2.2.1, do not ask me why), this was done by creating a separate ear and repeating all my dependencies on my subprojects in the ear .

I can imagine how I would duplicate the same hack in Gradle, but I hope that there will be a more reasonable way to do this. Does anyone have any ideas / examples / suggestions?

+3
source share
3 answers

1.0-M4 gradle .

http://repo.gradle.org/gradle/distributions/gradle-snapshots/gradle-1.0-milestone-4-20110610162713+0200-bin.zip

, http://repo.gradle.org/gradle/distributions/gradle-snapshots/gradle-1.0-milestone-4-20110610162713+0200-all.zip, .

/ear/earWithWar. .

, build.gradle . , /ear/earWithWar/war/build.gradle:

war{
    classpath = []
}

libs lib/subfolder , lib, , earlib . log4j.

"gradle ear" libs, lib/subfolder .

/

+4

war { classpath = [] }, , - WEB-INF/classes. , war WEB-INF/lib dir, stil include WEB-INF/classes - / .

dependencies { providedCompile fileTree(dir: 'lib', include: '**/*.jar') }

+1

I am going to try to assemble this assembly of snapshots, but here's how I hacked everything together from yesterday just for the sake of completeness. Feel free to improve my Gradle / Groovy. I am sure that it is not as elegant as it could be.

//Make sure the war and jars get built first
task ('ear', type:Jar, dependsOn: ":myWarProject:assemble" ){
    //This needs to be in the config block, or Gradle skips the task, assuming the file list for the jar/ear is empty...
    from{ "ear/src/main/application" }
}

ear.doFirst{
    //Set up the ear file name
    baseName = "myapp-" + rootVersion
    extension = "ear"

    //Gather up the jars required by all of the subprojects
    def allSubprojectDependencies = getAllProjectDependencies([
        "subproject1",
        "subproject2",
        "subproject3",
        "subproject4",
        "subproject5"
    ])
    from { allSubprojectDependencies }

    //grab the assembled war file
    from {
        subprojects.find{ it.name=="myWarFile" }.war.archivePath
    }

    //Other stuff required for our ear, such as security or eventing EJBs
    //Make sure you apply your "repositories" block to your root/ear project or "allProjects" if you do this...
    from { configurations.earConfig.files }

    //Create the classpath manifest
    manifestClassPath = allSubprojectDependencies.collect { it.name }.sort().join(' ')
    manifest { attributes( "Class-Path": manifestClassPath ) }
}

def getAllProjectDependencies (def projectNames){
    def allDependencies = []as Set
    projectNames.each{ projectName ->
        def subProject = subprojects.find{ subProject ->
            subProject.name.equals(projectName)
        }
        def subProjectDependencies = subProject.configurations.compile.files
        allDependencies.addAll subProjectDependencies
    }
    return allDependencies.unique{ a,b->
        if (a.equals(b)){
            return 0
        }
        return -1
    }
}

(Please note that all banks are at the root of the ear on purpose. Do not ask me why, but some people like it.)

0
source

All Articles