将另一个 java 源目录添加到 gradle 脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31077844/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 10:36:46  来源:igfitidea点击:

Add another java source directory to gradle script

javagradle

提问by RedCollarPanda

I hava an example java project package

我有一个示例java项目包

package com.example.testing;

包 com.example.testing;

with such file tree

使用这样的文件树

app
|
  src->com->example->testing->Main.java

and a gradle script:

和一个gradle脚本:

apply plugin: 'java'
apply plugin: 'application'

sourceSets {
    main {
        java {
            srcDirs 'src'
        }
    }


}

sourceSets.main.output.classesDir = file("classes")
mainClassName = 'com.example.testing.Main'

defaultTasks 'compileJava', 'run'

Now I want to add some module to this project and my folders will be something like this

现在我想向这个项目添加一些模块,我的文件夹将是这样的

app
|
  src1->com->example->testing->Main.java

  src2->com->another_example->another_testing->Library.java

How do I add new source code to gradle script?

如何将新的源代码添加到 gradle 脚本?

采纳答案by RaGe

I agree with @JB Nizet about respecting standard conventions. If you still insist on being an Anarchist though:

我同意@JB Nizet 关于尊重标准约定的看法。如果你仍然坚持做一个无政府主义者:

You already have srcdeclared in your sourceset, why not add src1and src2as well? You can add them to the same sourceset, or define a sourceset per module if you want.

您已经src在源集中声明了,为什么不添加src1src2呢?如果需要,您可以将它们添加到同一个源集,或者为每个模块定义一个源集。

sourceSets {
    main {
        java {
            srcDirs 'src'
            srcDirs 'src1'
            srcDirs 'src2'
        }
    }
 }

回答by gabowsky

I have a slightly different approach with a Gradle 4.6:

我对 Gradle 4.6 的方法略有不同:

sourceSets {
    main {
        java {
            srcDir 'src/main/java'
            srcDir 'build/swagger-code-dummy/src/main/java'
        }
    }
}

as you can see, I had to specify the directories with the "/main/java" subdirectories as well, otherwise gradle/intellij was not setting the right path.

如您所见,我还必须指定带有“/main/java”子目录的目录,否则 gradle/intellij 没有设置正确的路径。

Maybe this helps someone else too :)

也许这对其他人也有帮助:)

回答by Fresh Codemonger

A slightly different solution:

一个稍微不同的解决方案:

sourceSets.main.java.srcDirs = ['build/jasper', 'src/main/java']

回答by koppor

The question is about "Adding"; the question of the text is describing a more concrete scenario. If one just wants to add an existing directory, this is the way to add:

问题是关于“添加”;案文的问题是描述一个更具体的场景。如果只想添加一个现有目录,这是添加的方法:

sourceSets.main.java.srcDirs += ['src/gen/java']

The full build.gradle is as follows:

完整的 build.gradle 如下:

apply plugin: 'java'

repositories {
    mavenCentral()
}

dependencies {
    implementation 'com.squareup:javapoet:1.12.1'
}

sourceSets.main.java.srcDirs += ['src/gen/java']