Gradle构建-添加模块路径 [英] Gradle build - add module path

查看:588
本文介绍了Gradle构建-添加模块路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题:如何为gradle build设置模块路径?

My question: How do I set a module path for gradle build?

我已经很习惯从命令行使用Java模块.我经常在Powershell中进行练习,生成这些源文件.

I've become comfortable working with Java modules from the command line. I do a frequent exercise in Powershell which results in these source files.

└───src
    ├───appMod
    │   │   module-info.java
    │   │
    │   └───appPack
    │           Entry.java
    │
    └───greetMod
        │   module-info.java
        │
        └───greetPack
                Hello.java

appMod/module-info

module appMod {
    requires greetMod;
}

appMod/appPack.Entry

package appPack;

import greetPack.Hello;

public class Entry {
    public static void main(String[] args) {
        System.out.println(new Hello().sayHello());
    }
}

greetMod/module-info

module greetMod {
    exports greetPack;
}

greetMod/greetPack.Hello

package greetPack;

public class Hello {
    public String sayHello() {
        return "Greetings from Hello class!";
    }
}

由于appMod模块需要greetMod,所以我先编译并打包greetMod.

Since the appMod module requires greetMod, I compile and jar greetMod first.

javac -d out/greetMod src/greetMod/module-info.java src/greetMod/greetPack/Hello.java;
jar cf lib/greetJar.jar -C out/greetMod .;

然后我编译并打包appMod,但是我这样做时指定了模块路径(-p),可在其中找到新的greetMod jar(greetJar)(在lib中).

Then I compile and jar appMod, but as I do so I specify the module path (-p) where the new greetMod jar (greetJar) can be found (in lib).

javac -d out/appMod -p lib src/appMod/module-info.java src/appMod/appPack/Entry.java;
jar cfe lib/appJar.jar appPack.Entry -C out/appMod .;

然后我可以通过添加模块路径来部分运行或链接它.

I can then run this or jlink it in part by adding a module path.

java -p lib -m appMod;
jlink -p lib --add-modules appMod --launcher launch=appMod --output dist;
dist/bin/launch

我现在想在Gradle中做同样的练习,但是我不知道如何做等同于设置模块路径(如-p lib)的方法.我已经看到了 sourceSets 的代码,以及依赖性的无数变种,但到目前为止,我还无法将它们放在一起有用的东西.我还阅读了有冲突的陈述,它们都说 Gradle不完全支持Java模块,并且 Gradle确实支持它们.. >

I now want to do this same exercise in Gradle, but I can't figure out how to do the equivalent of setting a module path such as -p lib. I've seen code for sourceSets, and countless variations of dependencies, but so far I haven't been able to put together something that works. I've also read conflicting statements that both say that Gradle doesn't fully support Java modules, and that Gradle does support them.

推荐答案

我知道这可能会造成混淆,但是绝对可以通过gradle来完成.您将需要使用多项目构建来完成这项工作.在最顶部的build.gradle中,执行以下操作:

I know it can be confusing, but it can definitely be done by gradle. You will need to use a multiproject build to have this work. In your top-most build.gradle, do this:

subprojects {
    apply plugin: 'java'

    sourceCompatibility = 1.9

    compileJava {
        doFirst {
            options.compilerArgs += [
                    '--module-path', classpath.asPath
            ]
            classpath = files()
        }
    }
}

在您的settings.gradle中:

rootProject.name = 'module-testing'

include 'src:greetMod'
include 'src:appMod'

appMod中的所有内容都应移动到名为appModSrc的文件夹中.对greetMod执行相同操作,因此请使用greetModSrc.

Everything inside appMod should be moved into a folder called appModSrc. Do the same for greetMod so use greetModSrc.

目录结构:

├── build.gradle
└── greetModSrc
    ├── greetPack
    │   └── Hello.java
    └── module-info.java

build.gradle

sourceSets {
    main {
        java {
            srcDirs 'greetModSrc'
        }
    }
}


appMod

目录结构:


appMod

Directory structure:

├── appModSrc
│   ├── appPack
│   │   └── Entry.java
│   └── module-info.java
└── build.gradle

build.gradle

plugins {
    id 'application'
}

sourceSets {
    main {
        java {
            srcDirs 'appModSrc'
        }
    }
}

application {
    mainClassName 'appPack.Entry'
}

jar {
    doFirst {
        manifest {
            attributes('ModuleMainClass': mainClassName)
        }
    }
}

dependencies {
    implementation project(':src:greetMod')
}


使用此设置,您只需运行 ./gradlew :src:appMod:run:


With this setup, you can simply run ./gradlew :src:appMod:run:

> Task :src:appMod:run
Greetings from Hello class!

您可以在此处下载想法项目: https://github.com/smac89/multi -java9-gradle

You can download the idea project here: https://github.com/smac89/multi-java9-gradle

这篇关于Gradle构建-添加模块路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆