Java Gradle:自定义源设置为主和测试的依赖项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21161845/
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-13 07:02:06  来源:igfitidea点击:

Gradle: custom source set as dependency for the main and test ones

javagradleclasspathsource-sets

提问by Anton Moiseev

I've created custom source set in Gradle project to keep all generated code:

我在 Gradle 项目中创建了自定义源集以保留所有生成的代码:

sourceSets {
  generated {
    java {
      srcDir 'src/generated/java'
    }
    resources {
      srcDir 'src/generated/resources'
    }
  }
}

I want to make the result of this source set's code compilation available at compile and run time for mainand testsource sets.

我想要在编译可用此源集合的代码编译的结果,运行时间maintest源集。

What's the right semantic way to do it in Gradle?

在 Gradle 中正确的语义方式是什么?

UPDATE:

更新:

As suggested here: How do I add a new sourceset to Gradle?doesn't work for me, I still get java.lang.ClassNotFoundExceptionwhen I launch my app (though compilation and unit tests run fine). Here is what I tried:

正如这里所建议的:How do I add a new sourceset to Gradle? 对我不起作用,java.lang.ClassNotFoundException当我启动我的应用程序时我仍然得到(尽管编译和单元测试运行良好)。这是我尝试过的:

sourceSets {
  main {
    compileClasspath += sourceSets.generated.output
    runtimeClasspath += sourceSets.generated.output
  }

  test {
    compileClasspath += sourceSets.generated.output
    runtimeClasspath += sourceSets.generated.output
  }
}

采纳答案by Peter Niederwieser

sourceSets {
    main {
        compileClasspath += generated.output
        runtimeClasspath += generated.output
    }
}

Same for the testsource set.

test源集相同。