简体   繁体   English

使用 Gradle 构建 EAR 文件:如果 webmodules 是 SNAPSHOT 依赖项,如何配置它们?

[英]Building an EAR file with Gradle: How to configure the webmodules if they are SNAPSHOT dependencies?

I create an EAR file from external dependencies using Gradle like this:我使用 Gradle 从外部依赖项创建一个 EAR 文件,如下所示:

...
dependencies {
...
    deploy group: 'xx.xxx.xxx.xxx', name: 'war-name', version: '0.3.0', ext: 'war'
...
}

ear {
    deploymentDescriptor {
        webModule("war-name-0.3.0.war", "/war-name")
    }
}
...

The ear Configuration achieves the goal to not have the version number in the path of the HTTP-Endpoints. ear 配置实现了在 HTTP-Endpoints 的路径中没有版本号的目标。 This solution is suggested in Cannot set web-uri with gradle ear plugin to inclue war from maven repository .此解决方案在Cannot set web-uri with gradle ear plugin to include war from maven 存储库中建议。

So far, everything works just fine.到目前为止,一切正常。 However, if I do the same for a WAR file with a SNAPSHOT version, the configuration of the webModule does not work anymore, since the filename of the war file contains the timestamp of the SNAPSHOT, rather than being called exactly like the version:但是,如果我对具有 SNAPSHOT 版本的 WAR 文件执行相同操作,则 webModule 的配置将不再起作用,因为 WAR 文件的文件名包含 SNAPSHOT 的时间戳,而不是像版本一样被调用:

...
dependencies {
...
    deploy group: 'xx.xxx.xxx.xxx', name: 'war-name', version: '0.3.0-SNAPSHOT', ext: 'war'
...
}

ear {
    deploymentDescriptor {
        // This fails, because the war file does not have this exact name, but instead contains 
        // the timestamp of the SNAPSHOT's buildtime.
        webModule("war-name-0.3.0-SNAPSHOT.war", "/war-name")
    }
}
...

If I do the above, the resulting application.xml contains the following entries:如果我执行上述操作,生成的application.xml包含以下条目:

  <module>
    <web>
      <web-uri>war-name-0.3.0-20200714.134805-4.war</web-uri>
      <context-root>war-name-0.3.0-20200714.134805-4</context-root>
    </web>
  </module>
  <module>
    <web>
      <web-uri>war-name-0.3.0-SNAPSHOT.war</web-uri>
      <context-root>/war-name</context-root>
    </web>
  </module>

What I am trying to achieve is to have it generated like this (basically a mix of the two module declarations above):我想要实现的是让它像这样生成(基本上是上面两个模块声明的混合):

  <module>
    <web>
      <web-uri>war-name-0.3.0-20200714.134805-4.war</web-uri>
      <context-root>/war-name</context-root>
    </web>
  </module>

I guess I need to find out the used timestamp and then do it like this:我想我需要找出使用的时间戳,然后这样做:


ear {
    deploymentDescriptor {
        webModule("war-name-" + TIMESTAMP_OF_SNAPSHOT + ".war", "/war-name")
    }
}
...

However, I am a bit lost on how I could find the timestamp of the exact SNAPSHOT-Version which is packed into the EAR.但是,我对如何找到打包到 EAR 中的确切 SNAPSHOT-Version 的时间戳有点迷茫。 Any ideas?有任何想法吗? Thanks for the help!谢谢您的帮助!

I actually found a solution myself by now.我现在实际上自己找到了解决方案。 What you can do is finding the used War-File by searching the configuration:您可以做的是通过搜索配置找到使用过的 War-File:

ear {
    // Since we use SNAPSHOT-Versions sometimes (e.g. for integration testing),
    // We cannot just use the version of the war files when specifying the webModules.
    // Instead, we have to find out the exact filename of the used war-file (which will have an
    // arbitrary timestamp in the case of SNAPSHOT Versions).
    final def actualWarFileUsed = configurations.getByName("deploy")
            .filter{ final dep -> dep.name.contains("war-name")}
            .getSingleFile()
            .name

    deploymentDescriptor {
        webModule(actualWarFileUsed, "/war-name")
    }
}

Maybe this helps someone with the same problem.也许这可以帮助有同样问题的人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM