使用 open liberty-maven-plugin 时如何设置委派 parentLast?

How can I set a delegation parentLast when using open liberty-maven-plugin?

提问人:user3788903 提问时间:11/3/2023 最后编辑:user3788903 更新时间:11/9/2023 访问量:37

问:

我有两个应用程序需要将类加载器设置为 delegation=“parentLast”。

一方面,我有一个不需要在它们上开发的应用程序(称为 sites.ear),我只是按原样使用它,所以我不需要在我的 IDE 中集成这个 EAR。因此,我直接在我的服务器 .xml 上配置了这个应用程序。

另一方面,我有一个名为“博客”的应用程序,我正在开发。如果我在服务器.xml中设置以下行,它可以正常工作,但是如果我这样做,我不能使用liberty-maven-plugin(从IDE启动应用程序)。如果我尝试使用 IntelliJ 启动应用程序,那么它会部署两次代码,一次来自服务器 .xml,另一次来自插件 (mvn liberty:run)

    <application id="app-blog" location="C:\Code\app-blog\target\app-blog.ear" name="wem-app-blog" type="ear">
        <classloader commonLibraryRef="global" delegation="parentLast"></classloader>
    </application>    

这是我在我的应用博客中配置的pom.xml

<plugin>
  <groupId>io.openliberty.tools</groupId>
  <artifactId>liberty-maven-plugin</artifactId>
  <version>3.6</version>
  <configuration>
    <installDirectory>C:\wlp-webProfile8-21.0.0.12\wlp</installDirectory>
    <jvmOptionsFile>C:\wlp-webProfile8-21.0.0.12\wlp\usr\servers\defaultServer\jvm.arguments.txt</jvmOptionsFile>
  </configuration>
</plugin>

这是我的服务器.xml

<?xml version="1.0" encoding="UTF-8"?>
<server description="new server">

    <library id="global">
        <fileset dir="C:\sdk\libs-classpath" includes="*.jar"/>
    </library>

    <!-- Enable features -->
    <featureManager>
        <feature>webProfile-8.0</feature>
    </featureManager>

    <!-- To access this server from a remote client add a host attribute to the following element, e.g. host="*" -->
    <httpEndpoint id="defaultHttpEndpoint" host="*" httpPort="9080" httpsPort="9443"/>

    <!-- Automatically expand WAR files and EAR files -->
    <applicationManager autoExpand="true"/>

    <application id="sites" location="C:\sdk\websphere\sites.ear" name="sites" type="ear">
        <classloader commonLibraryRef="global" delegation="parentLast"></classloader>
    </application>
    
    <!-- Default SSL configuration enables trust for default certificates from the Java runtime --> 
    <ssl id="defaultSSLConfig" trustDefaultCerts="true" />
</server>

我还尝试以全局方式将类加载器配置为parentLast,但它不起作用。

基本上,我想使用集成到我的 IDE IntelliJ 的开放自由服务器,并且我想使用 IDE 启动和停止服务器。现在,我无法使用类加载器parentLast从IDE启动。

有没有办法与IDE集成,将类加载器部署到parentLast?

任何想法或建议都是值得赞赏的。

谢谢

WebSphere Maven-Plugin Open-Liberty 正在 类装入

评论

0赞 Scott Kurz 11/7/2023
如果您共享服务器 .xml 的相关部分,可能会有所帮助。如此处所述:openliberty.io/docs/latest/class-loader-library-config.html 通过“dropins”文件夹和“apps”文件夹部署应用时有不同的要求。(我想知道在 IDE 内部和外部部署是否会导致那里有所不同。此外,config 元素是 'classloader' 而不是 'classloading'。
0赞 Scott Kurz 11/7/2023
我想知道您是否在配置中将<classloader>元素的范围限定为应用程序,例如<webApplication location=“my.war” contextRoot=“ctx”> <classloader delegation=“parentLast”/> </webApplication> 虽然您可以将<classloader>定义为“顶级”元素,而不是application/webApplication/enterpriseApplication元素的子元素,但如果您这样做, 引用它的语法略有不同。无论如何,共享您的服务器 .xml 会有所帮助。
0赞 user3788903 11/8/2023
嗨,斯科特,非常感谢您的评论。我已经用我的服务器.xml更新了票证,我试图更好地解释我的情况。
0赞 Scott Kurz 11/9/2023
嗨,谢谢更新。我猜到问题和解决方案可能是什么,我在下面添加了答案。我承认我没有解决你问题的每一个方面,但我猜很有可能这就是你所需要的。如果您有更多问题并想通过聊天进行讨论,请访问我们的 Gitter 频道:app.gitter.im/#/room/... (您只需要一个 GitHub ID)。另外,由于您使用的是 IDEA,请查看我们的 Liberty Tools 插件 plugins.jetbrains.com/plugin/14856-liberty-tools 并让我们知道您的想法。

答:

1赞 Scott Kurz 11/9/2023 #1

溶液:

使用与应用程序存档的“基本名称”匹配的“位置”来配置应用程序,例如:

<application id="app-blog" location="app-blog.ear" name="wem-app-blog" type="ear">
  <classloader commonLibraryRef="global" delegation="parentLast"/>
</application>    

(注意:如果要部署工件以在文件名中包含版本,则可能需要调整此设置,可以在服务器 .xml 中添加版本或使用“stripVersion”配置参数)。

解释

Liberty Maven/Gradle 插件努力为通过插件部署的应用程序自动生成部署配置(直接通过 liberty-maven-plugin 的 'deploy' 目标,或作为 'run' 或 'dev' 目标的一部分进行部署)。

However, this can cause problems if you need to provide your own non-default configuration (for something like parent-last classloading) if your configuration does not align with the configuration generated by the Liberty Maven/Gradle plugins. The plugin can end up treating your config as if it were config for a different app, and think it needs to go ahead and generate its own config, and now you have two configs for one app, and only one has the correct attributes, customizations, etc.

NOTE: I would also try this on the latest liberty-maven-plugin version (v3.9) since we've had some fixes in this area over the past years.

评论

0赞 Scott Kurz 11/9/2023
I realize that by changing the app location from "C:\Code\app-blog\target\app-blog.ear" to the relative basename, I might be nudging you into a different deployment workflow or lifecycle than whatever you're using now, with the help of the IDE. Since I'm not really sure exactly what the flow that you've been using is, I'm not going to worry about that. Using basename should give a good user experience, whether using the Liberty Tools IDE tooling, using liberty-maven-plugin outside an IDE (perhaps using dev mode or 'run' goals), and as I said, pls see our Gitter if you have further questions.
0赞 user3788903 11/21/2023
Hi Scott, thank you very much for you help. What you suggested changing from the full path to the relative path made a difference. Futhermore, to have work properly, I should have added this configuration on my pom.xml "<appsDirectory>apps</appsDirectory>"
0赞 Scott Kurz 11/21/2023
Glad that helped ! As far as the <appsDirectory> config, noting the fine print in github.com/OpenLiberty/ci.maven/blob/main/docs/deploy.md, it does say that this will default to 'apps' if the app config is specified in the server.xml, which it is in your case. So it would seem that shouldn't be necessary and I'd think you wouldn't need it. Though it certainly wouldn't hurt to include it explicitly.