Search code examples
javaweb-applicationsgradleweblogicear

Gradle deploy project to ear


I have project with below structure

--MyPrj.ear
  --APP-INF
    --src
    --lib
  --META-INF
    --application.xml
    --weblogic-application.xml
  --WEB_MAIN
    --assets
    --WEB-INF
      --conf
      --web.xml
      --weblogic.xml

I want to deploy to PRJ.ear file with below structure:

--MyPrj.ear
  --APP-INF
    --classes
    --lib
  --META-INF
    --application.xml
    --weblogic-application.xml
  --WEB_MAIN
    --assets
    --WEB-INF
      --conf
      --web.xml
      --weblogic.xml

And here is my ear configuration:

ear {
    baseName 'PRJ'
    appDirName 'APP-INF/src'
    libDirName 'APP-INF/lib'

    ear{
        into("META-INF"){
            from("META-INF") {
                exclude 'application.xml'
            }
        }
        into("WEB_MAIN") {
            from ("WEB_MAIN")
        }
    }

    deploymentDescriptor {
        webModule 'WEB_MAIN', '/'
        applicationName = "PRJ"
    }
}

My actual result:

--MyPrj.ear
  --APP-INF
    --lib
  --com
  --META-INF
    --application.xml
    --weblogic-application.xml
  --WEB_MAIN
    --assets
    --WEB-INF
      --conf
      --web.xml
      --weblogic.xml

Cannot generate APP-INF/classes


Solution

  • I'll start with an observation: Both instances of ear in your build script refer to the same task. There is no need to refer to ear twice, i.e. the into declarations can go one level up.

    First, add the folder APP-INF/src as a source set. This will result in the compiled classes being added to the root of the EAR, so you have to exclude these. Then you have to tell the ear task to copy the compiled classes to the APP-INF/classes directory in the EAR:

    // Make sure the source files are compiled.
    sourceSets {
        main {
            java {
                srcDir 'APP-INF/src'
            }
        }
    }
    
    ear {
        baseName 'PRJ'
        libDirName 'APP-INF/lib'
    
        into("META-INF") {
            from("META-INF") {
                exclude 'application.xml'
            }
        }
        into("WEB_MAIN") {
            from("WEB_MAIN")
        }
    
        deploymentDescriptor {
            webModule 'WEB_MAIN', '/'
            applicationName = "PRJ"
        }
    
        // Exclude the compiled classes from the root of the EAR.
        // Replace "com/javathinker/so/" with your package name.
        eachFile { copyDetails ->
            if (copyDetails.path.startsWith('com/javathinker/so/')) {
                copyDetails.exclude()
            }
        }
    
        // Copy the compiled classes to the desired directory.
        into('APP-INF/classes') {
            from(compileJava.outputs)
        }
    
        // Remove empty directories to keep the EAR clean.
        includeEmptyDirs false
    }