Gradle 将项目部署到 ear

Gradle deploy project to ear

我有以下结构的项目

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

我想部署到具有以下结构的 PRJ.ear 文件:

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

这是我的耳朵配置:

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"
    }
}

我的实际结果:

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

无法生成APP-INF/classes

要包含 .ear 文件,您应该通过向其添加 apply plugin: 'ear' 来修改 build.gradle,并按照 this guide 中的说明正确填充 ear 块.

此外,部署过程的魔力也得到了很好的解释 here,很快就会在 Gradle 中使用 wideploy 工具。 您可能还想查看 here 以找到有关此脚本的更多详细信息。

我将从一个观察开始:构建脚本中 ear 的两个实例指的是同一个任务。不需要引用 ear 两次,即 into 声明可以向上一级。

首先,将文件夹 APP-INF/src 添加为源集。这将导致已编译的 类 被添加到 EAR 的根目录中,因此您必须排除这些。然后你必须告诉 ear 任务将编译的 类 复制到 EAR 中的 APP-INF/classes 目录:

// 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
}