我的问题:如何为 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
module appMod {
requires greetMod;
}
package appPack;
import greetPack.Hello;
public class Entry {
public static void main(String[] args) {
System.out.println(new Hello().sayHello());
}
}
module greetMod {
exports greetPack;
}
package greetPack;
public class Hello {
public String sayHello() {
return "Greetings from Hello class!";
}
}
由于appMod模块需要greetMod,所以我先编译jargreetMod.
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 .;
然后我编译并 jar appMod,但在此过程中,我指定了新的 greetMod jar (greetJar) 所在的模块路径 (-p)(在 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 的代码,并且dependencies 的无数变体,但到目前为止我还无法将它们放在一起有用的东西.我还阅读了相互矛盾的声明,它们都说 Gradle 不完全支持 Java 模块,并且 Gradle 确实支持它们.p>
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
sourceSets {
main {
java {
srcDirs 'greetModSrc'
}
}
}
目录结构:
├── appModSrc
│ ├── appPack
│ │ └── Entry.java
│ └── module-info.java
└── 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
:
> Task :src:appMod:run
Greetings from Hello class!
你可以在这里下载idea项目:https://github.com/MVCE-Superstars/multi-java9-gradle
You can download the idea project here: https://github.com/MVCE-Superstars/multi-java9-gradle
这篇关于Gradle build - 添加模块路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!