使用 Gradle 将依赖项添加到运行时映像

2024-01-04

我不知道如何添加依赖项。我的模块需要 Log4j。我向模块信息添加了要求。我还添加了 gradle 依赖项。我可以运行项目,但无法创建自定义运行时映像。

plugins {
    id 'java'
    id 'application'
    id 'org.openjfx.javafxplugin' version '0.0.5'
}

group 'eu.sample'
version '2.0'


repositories {
    mavenCentral()
}

javafx {
    modules = [ 'javafx.controls', 'javafx.fxml' ]
}

mainClassName = "$moduleName/eu.sample.app.Main"

def lin_java_home = hasProperty('org.gradle.java.home') ? getProperty('org.gradle.java.home') : System.getenv('JAVA_HOME')
def lin_fx_jmods = hasProperty('linux.fx.mods') ? getProperty('linux.fx.mods') : System.getenv('PATH_TO_FX_MODS_LIN')

def win_java_home = hasProperty('windows.java.home') ? getProperty('windows.java.home') : System.getenv('JAVA_HOME_WIN')
def win_fx_jmods = hasProperty('windows.fx.mods') ? getProperty('windows.fx.mods') : System.getenv('PATH_TO_FX_MODS_WIN')

dependencies {
    compile group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.11.1'
}

task jlink(type: Exec) {
    dependsOn 'clean'
    dependsOn 'jar'

    workingDir 'build'

    if (lin_java_home == null) {
        throw new RuntimeException("java_home is not defined.")
    }
    if (lin_fx_jmods == null) {
        throw new RuntimeException("fx_jmods is not defined.")
    }
    commandLine "${lin_java_home}/bin/jlink", '--module-path', "libs${File.pathSeparatorChar}${lin_fx_jmods}",
            '--add-modules', "${moduleName}", '--output', "${moduleName}", '--strip-debug',
            '--compress', '2', '--no-header-files', '--no-man-pages'
}

task jlinkWin(type: Exec) {
    dependsOn 'clean'
    dependsOn 'jar'


    workingDir 'build'

    if (win_java_home == null) {
        throw new RuntimeException("java_home is not defined.")
    }
    if (win_fx_jmods == null) {
        throw new RuntimeException("fx_jmods is not defined.")
    }
    commandLine "${lin_java_home}/bin/jlink", '--module-path', 
            "${win_java_home}/jmods${File.pathSeparatorChar}libs${File.pathSeparatorChar}${win_fx_jmods}",
            '--add-modules', "${moduleName}", '--output', "${moduleName}", '--strip-debug',
            '--compress', '2', '--no-header-files', '--no-man-pages'
}

当我触发任务 jlink 时,我得到:

错误:找不到模块 org.apache.logging.log4j,需要 应用程序

我检查了构建中的 libs 目录,没有 log4j jar。如何告诉gradle向jlink任务添加依赖项?


Problem

这就是你的jlink task:

'--module-path', "libs${File.pathSeparatorChar}${fx_jmods}"

这意味着您要添加来自以下位置的依赖项:

  • libs:基本上是你的模块的jarhelloFX。这是由于jar任务,仅包含项目的类和资源,但不包含其依赖项。

  • fx_jmods:这是 JavaFX jmod 的路径。

但是当你运行时,你会得到这个错误:

错误:找不到模块 org.apache.logging.log4j,应用程序需要

该错误意味着module-path为了jlink命令不完整,并且无法解析所有必需的依赖项。如上所述,我们仅包含 module.jar 和 JavaFX (jmods) jar,但不包含log4j.jar.

所以我们需要找到一种方法将该 jar 添加到模块路径中。

有一些可能的解决方案可以在自定义映像中包含第三方依赖项。

解决方案1

我们必须修改jlink任务,将现有的依赖项包含在我们的运行时配置中。

最直接的解决方案是在本地 .gradle 存储库中查找 gradle 存储 logj4 jar 的位置。

'--module-path', "libs${File.pathSeparatorChar}${fx_jmods}: \
   /Users/<user>/.gradle/caches/modules-2/files-2.1/org.apache.logging.log4j/log4j-api/2.11.1/268..a10/log4j-api-2.11.1.jar: \
   /Users/<user>/.gradle/caches/modules-2/files-2.1/org.apache.logging.log4j/log4j-core/2.11.1/59..e4/log4j-core-2.11.1.jar"

虽然此解决方案有效,但它当然不是最方便的,因为它取决于用户的本地路径。

解决方案2

更好的解决方案是添加一个任务来将运行时依赖项(由 gradle 直接解析)复制到libs文件夹如:

task libs(type: Copy) {
    into 'build/libs/'
    from configurations.runtime
}

然后从调用此任务jlink task:

task jlink(type: Exec) {
    dependsOn 'clean'
    dependsOn 'jar'
    dependsOn 'libs'
    ...
}

如果你跑./gradlew jlink并检查libs文件夹中你应该找到类似这样的内容:

build/libs/hellofx.jar
build/libs/javafx-base-11.0.1.jar
build/libs/javafx-base-11.0.1-$platform.jar
build/libs/javafx-graphics-11.0.1.jar
build/libs/javafx-graphics-11.0.1-$platform.jar
build/libs/javafx-controls-11.0.1.jar
build/libs/javafx-controls-11.0.1-$platform.jar
build/libs/javafx-fxml-11.0.1.jar
build/libs/javafx-fxml-11.0.1-$platform.jar
build/libs/log4j-api-2.11.1.jar
build/libs/log4j-core-2.11.1.jar

where $platform是你的跑步平台。

请注意,libs文件夹现在包含all模块路径所需的依赖项,以及 JavaFX-*-$platform jar 所需的依赖项contain本地库,因此,模块路径选项中不再需要 jmod。这就足够了:

'--module-path', "libs"

所以你的命令行将是:

commandLine "${java_home}/bin/jlink", '--module-path', "libs",
        '--add-modules', "${moduleName}", '--output', "${moduleName}", '--strip-debug', '--compress', '2', '--no-header-files', '--no-man-pages'

你可以运行你的jlink现在任务成功了。

解决方案3

正如 @madhead 评论所建议的,您可以使用另一个插件jlink任务:所谓的 badass-jlink-插件 https://github.com/beryx/badass-jlink-plugin.

将您的构建修改为:

plugins {
    id 'application'
    id 'org.openjfx.javafxplugin' version '0.0.5'
    id 'org.beryx.jlink' version '2.1.8'
}

repositories {
    mavenCentral()
}

dependencies {
    compile group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.11.1'
}

javafx {
    modules = ['javafx.controls', 'javafx.fxml']
}

mainClassName = "${moduleName}/eu.sample.app.Main"

jlink {
    options = ['--strip-debug', '--compress', '2', '--no-header-files', '--no-man-pages']
    launcher {
        name = 'helloFX'
    }
}

另请注意,该插件可以选择为其他平台创建图像(请参阅目标平台 https://badass-jlink-plugin.beryx.org/releases/latest/#_methods).

编辑关于 log4j

正如评论中提到的,log4j依赖关系还不能很好地与模块配合使用。有一个开放问题 https://issues.apache.org/jira/projects/LOG4J2/issues/LOG4J2-2438?filter=allopenissues在 Apache 问题跟踪器上:

目前,根据给定的自动模块,您无法在想要使用 jlink 生成运行时映像的项目中使用 log4j-core。

我已将其添加到我的主类中:

LogManager.getLogger(MainApp.class).info("hellofx!");

我添加了log4j2.xml文件到src/main/resources.

Running ./gradlew run, works:

> Task :run
18:24:26.362 [JavaFX Application Thread] INFO  eu.sample.app.Main - hellofx!

但是,从解决方案 2 运行 jlink 会创建自定义映像,我可以验证是否包含 xml 文件,但是当从映像运行时,我得到:

build/hellofx/bin/java -m hellofx/eu.sample.app.Main
ERROR StatusLogger Log4j2 could not find a logging implementation. \
    Please add log4j-core to the classpath. Using SimpleLogger to log to the console...

And as mentioned, running jlink with the plugin from Solution 3 fails at the createMergedModule task:

 error: package org.apache.logging.log4j.spi is not visible
   provides org.apache.logging.log4j.spi.Provider with org.apache.logging.log4j.core.impl.Log4jProvider;

[参见编辑(2),自版本 2.1.9 以来已修复此问题]

选择

此时,可能的替代方案是使用 Slf4j。有一个sample https://github.com/copper-engine/copper-modular-demo从成功使用它的插件文档中列出。

总而言之,这是必需的:

摇篮文件:

dependencies {
    compile 'org.slf4j:slf4j-api:1.8.0-beta2'
    compile('ch.qos.logback:logback-classic:1.3.0-alpha4') {
        exclude module: "activation"
    }
}

模块信息:

requires org.slf4j;
requires ch.qos.logback.classic;
requires java.naming;

主要类别:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

private static final Logger logger = LoggerFactory.getLogger(MainApp.class);

...
logger.info("hellofx!");

logging.properties from here https://github.com/copper-engine/copper-modular-demo/blob/master/src/main/resources/logging.properties and logback.xml from here https://github.com/copper-engine/copper-modular-demo/blob/master/src/main/resources/logback.xml,和你的主课。

两者都在运行./gradlew run or ./gradlew jlink, and build/image/bin/HelloFX工作,并且消息被记录到控制台。

EDIT (2)

After 报告 https://github.com/beryx/badass-jlink-plugin/issues/14badass-jlink-plugin 的问题跟踪器的问题已经解决,并且从版本 2.1.19 开始,创建自定义图像应该可以正常工作。

Since log4j是多版本jar,需要一件事:

plugins {
    id 'application'
    id 'org.openjfx.javafxplugin' version '0.0.5'
    id 'org.beryx.jlink' version '2.1.9'
}

...

jlink {
    options = ['--strip-debug', '--compress', '2', '--no-header-files', '--no-man-pages']
    launcher {
       name = 'helloFX'
    }
    forceMerge('log4j-api')     // <---- this is required for Log4j
}

查看完整的工作示例here https://github.com/beryx-gist/badass-jlink-example-log4j2-javafx.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 Gradle 将依赖项添加到运行时映像 的相关文章

随机推荐

  • C# - XML - 压缩

    我遇到过这样的情况 我正在生成要提交到 Web 服务的 XML 文件 有时由于数据量超过 30mb 或 50mb 我需要使用 c net Framework 4 0 来压缩文件 而不是使用拥有大部分数据的节点之一 我不知道我要怎么做 如果有
  • Tomcat HTTP Access 日志写入文件有延迟

    在 tomcat 中 http 访问日志 Valve 需要一些时间才能写入文件 请注意 我有 org apache catalina valves AccessLogValve 的默认配置 有什么办法可以改善延迟吗 造成这种延迟的主要原因是
  • 在 Windows 上通过 Jenkins 使用 chromedriver 和 chrome 捕获屏幕截图时,从渲染器接收消息超时:10.000

    操作系统 Windows 10 浏览器 Chrome 浏览器版本 版本73 0 3683 86 官方版本 32位 我正在运行 selenium cucumber BDD 项目 我正在验证一页的标题 我正在使用范围报告版本4 项目在本地运行成
  • afterAjaxUpdate 回调函数 CListView 显示未定义

    我试图调用一个在另一个 js 文件中通过文件名的 afterAjaxUpdate 参数定义的函数 但我在控制台中收到错误 该函数未定义
  • 在同一行显示图标和文本

    我试图让文本 主页 关于我们 奶酪 等显示在社交媒体图标的右侧 以便文本的底部与图标的底部对齐 并且它们显示在同一行 我该怎么做呢 我的代码在这里摆弄http jsfiddle net pnX3d http jsfiddle net pnX
  • /etc/rc.local 和 ~/.bashrc 有什么区别?

    这是一个与linux相关的问题 我四处搜寻但没有得到很好的解释 在我看来 这两个文件在我登录时都会配置设置 但是有什么区别吗 我注意到在决定哪些内容应该放入两个不同的文件中似乎有 一些规则 例如 如果我需要向 PATH 添加特定的搜索路径
  • 如何使用apache poi 3.6获取A1(单元格地址)的单元格值

    我有 Excel 单元格地址 如 A1 A2 那么 如何使用 poi 3 6 以编程方式访问此单元格 另一种方法是 row mySheet getRow cell row getCell 但我的地址格式为 A1 那么 我如何以编程方式访问这
  • 如何使用模板函数从缓冲区(T* 数据数组)创建 cv::Mat?

    我想编写一个模板函数来复制指针引用的数据T image to cv Mat 我很困惑如何概括 T 和 cv type 匹配 template
  • 将 Android studio 更新到 2.2 后出现“无法解决 x”错误

    Google 推出了 Android Studio 2 2 所以我将 Android studio 2 1 更新为 Android Studio 2 2 当我制作项目时 我收到这些错误 和更多 Error Failed to resolve
  • 添加apache用户组文件上传权限

    我有一个 php 脚本将文件上传到某个文件夹 目前它们作为 psacln 组上传 以便我可以通过 FTP 删除文件 当 PHP 作为 FastCGI 运行时 这一切都工作正常 我必须将 PHP 更改为作为 Apache 模块运行 以便让 p
  • 为什么Ul上方有空白

    当我放置无序列表时 then why top bar goes down I cannot find the reason behind this Please tell me how to fix this problem http js
  • Joomla MySQL 性能

    我一直在开发一个版本为2 5 11的Joomla网站 网站的流量将会非常高 我的问题是关于 MySQL 查询性能的 数据库包含大约 60000 行内容表 下面看到的查询 核心 com content 文章模型查询 执行时间约为 6 秒 非常
  • 如何显示尾随“%”符号?

    在 SO Line 的折扣百分比列中 我想在其中显示尾随 我尝试将显示格式设置为 P 但这是一场灾难 由于实际值是数据库中的百分比 因此不必乘以 100 感谢您的回答 您应该设置DisplayFormat to 0 00 结果你会得到以下结
  • Py2Exe 检测为病毒。备择方案?

    所以 我创建了一个python程序 使用 Py2Exe 转换为 exe 并尝试使用 PyInstaller 和 cx freeze 所有这些都会触发程序在virustotal 和我的本地计算机上被avast avg 和其他程序检测为病毒 我
  • Cython:编译独立的静态可执行文件

    我正在尝试编译一个不使用动态加载器的可执行文件 ELF 文件 我用了Cython将Python编译为C cython3 3 test py embed Then gcc test c otest pkg config libs cflags
  • 如何将 Material UI 集成到 Svelte 项目中

    我想整合材质用户界面 https sveltematerialui com 进入我的 Svelte 项目 我尝试遵循官方文档here https github com hperrin svelte material ui 但我不知道为什么在
  • 有没有类似于 Java 的 mini-mvc-profiler 的东西?

    正如标题所说 有没有类似的东西迷你 MVC 分析器 http code google com p mvc mini profiler 对于Java 我正在寻找可以用于Struts http struts apache org or 春季MV
  • 我们可以指定散景导出的 png 文件大小/分辨率吗?

    使用 Python Bokeh export png 函数 我们可以将对象导出到 png 文件 但是 我找不到任何方法来指定生成的 png 文件大小 分辨率 我发现我使用相同的代码在Windows和Linux中得到了不同的png文件 在 W
  • Postgresql 捷克语全文搜索(无默认语言配置)

    我正在尝试设置捷克语全文搜索 我有点困惑 因为我看到了一些cs cz affix and cs cz dict里面的文件tsearch data文件夹 但没有捷克语配置 它可能不随 Postgres 一起提供 那么我应该创建一个吗 我必须创
  • 使用 Gradle 将依赖项添加到运行时映像

    我不知道如何添加依赖项 我的模块需要 Log4j 我向模块信息添加了要求 我还添加了 gradle 依赖项 我可以运行项目 但无法创建自定义运行时映像 plugins id java id application id org openjf