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

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

本文介绍了使用Gradle向运行时映像添加依赖项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I don't know how to add dependency. My module needs Log4j. I added requirement to module info. I added also to gradle dependencies. I can run project, but I can't create custom runtime image.

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

当我执行任务链接时,我得到:

When I fire task jlink I get:

我检查了build中的libs目录,但没有log4j jar.如何告诉gradle为jlink任务添加依赖项?

I checked libs directory in build and there is not log4j jar. How to tell gradle to add dependencies to jlink task?

推荐答案

中,使用解决方案3的插件运行jlink失败:

And as mentioned, running jlink with the plugin from Solution 3 :

 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;

[请参阅EDIT(2),此问题自版本2.1.9起已得到修复]

[See EDIT(2), this has been fixed since version 2.1.9]

替代

在这一点上,可能的替代方法是改用Slf4j.成功使用插件的文档中列出了一个示例.

At this point, a possible alternative could be use Slf4j instead. There is a sample listed from the plugin documentation that uses it successfully.

总之,这是必需的:

成绩文件:

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"rel =" nofollow noreferrer">这里和logback.xml来自这里,与您的主要班级一起.

logging.properties from here and logback.xml from here, with your main class.

同时运行./gradlew run或./gradlew jlink,并且build/image/bin/HelloFX正常运行,并且消息记录到控制台.

Both running ./gradlew run or ./gradlew jlink, and build/image/bin/HelloFX work, and the message is logged to the console.

编辑(2)

报告问题后,将问题提交给badass-jlink-plugin问题追踪器,此问题已解决,并且从2.1.19版本开始,它应该可以很好地创建自定义图像.

After reporting the issue to the badass-jlink-plugin's issue tracker, this was solved, and since version 2.1.19 it should work fine creating a custom image.

由于log4j是多版本jar,因此需要做一件事:

Since log4j is multi-release jar, there is one thing required:

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
}

此处中查看完整的工作示例..

See a full working sample here.

这篇关于使用Gradle向运行时映像添加依赖项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 15:22