<bdo id='WljlS'></bdo><ul id='WljlS'></ul>

<tfoot id='WljlS'></tfoot>
  • <small id='WljlS'></small><noframes id='WljlS'>

    1. <i id='WljlS'><tr id='WljlS'><dt id='WljlS'><q id='WljlS'><span id='WljlS'><b id='WljlS'><form id='WljlS'><ins id='WljlS'></ins><ul id='WljlS'></ul><sub id='WljlS'></sub></form><legend id='WljlS'></legend><bdo id='WljlS'><pre id='WljlS'><center id='WljlS'></center></pre></bdo></b><th id='WljlS'></th></span></q></dt></tr></i><div id='WljlS'><tfoot id='WljlS'></tfoot><dl id='WljlS'><fieldset id='WljlS'></fieldset></dl></div>
      1. <legend id='WljlS'><style id='WljlS'><dir id='WljlS'><q id='WljlS'></q></dir></style></legend>

        Gradle build - 添加模块路径

        时间:2023-07-13

        <legend id='hS5pW'><style id='hS5pW'><dir id='hS5pW'><q id='hS5pW'></q></dir></style></legend>

        <small id='hS5pW'></small><noframes id='hS5pW'>

        <tfoot id='hS5pW'></tfoot>

            • <bdo id='hS5pW'></bdo><ul id='hS5pW'></ul>
                  <tbody id='hS5pW'></tbody>
                <i id='hS5pW'><tr id='hS5pW'><dt id='hS5pW'><q id='hS5pW'><span id='hS5pW'><b id='hS5pW'><form id='hS5pW'><ins id='hS5pW'></ins><ul id='hS5pW'></ul><sub id='hS5pW'></sub></form><legend id='hS5pW'></legend><bdo id='hS5pW'><pre id='hS5pW'><center id='hS5pW'></center></pre></bdo></b><th id='hS5pW'></th></span></q></dt></tr></i><div id='hS5pW'><tfoot id='hS5pW'></tfoot><dl id='hS5pW'><fieldset id='hS5pW'></fieldset></dl></div>

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

                  问题描述

                  我的问题:如何为 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 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 greetMod {
                      exports greetPack;
                  }
                  

                  greetMod/greetPack.Hello

                  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
                  

                  build.gradle

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


                  appMod

                  目录结构:

                  ├── 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:

                  > 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 - 添加模块路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Java - 如何减小第三方 jar 的大小以减小应用程序的大小 下一篇:Maven 私有依赖

                  相关文章

                  • <bdo id='HC15l'></bdo><ul id='HC15l'></ul>
                  <i id='HC15l'><tr id='HC15l'><dt id='HC15l'><q id='HC15l'><span id='HC15l'><b id='HC15l'><form id='HC15l'><ins id='HC15l'></ins><ul id='HC15l'></ul><sub id='HC15l'></sub></form><legend id='HC15l'></legend><bdo id='HC15l'><pre id='HC15l'><center id='HC15l'></center></pre></bdo></b><th id='HC15l'></th></span></q></dt></tr></i><div id='HC15l'><tfoot id='HC15l'></tfoot><dl id='HC15l'><fieldset id='HC15l'></fieldset></dl></div>

                  <legend id='HC15l'><style id='HC15l'><dir id='HC15l'><q id='HC15l'></q></dir></style></legend>

                  <tfoot id='HC15l'></tfoot>

                    1. <small id='HC15l'></small><noframes id='HC15l'>