java - Gradle SourceSet IntelliJ 与 Eclipse

标签 java eclipse gradle intellij-idea source-sets

当我将最初使用 Eclipse 构建的 gradle 项目导入到 IntelliJ 中时,我指出了一个问题。 事实上,该项目是一个 gradle-project,但是当我将其导入 IntelliJ 时,IDE 告诉我它删除了(一些)重复的内容根。

我得到一个气球 - 告诉我:

Duplicate content roots deleted

事件日志中的消息是:

hh:mm[=22:58] Duplicate content roots detected

Path [/Users/.../someProject/someModule] of module [someModuleName] was removed from modules [someModuleName.someSourceSetName]

Also 1 more path was deduplicatetd. See idea log for details

一般来说,它是一个gradle-project,但我无法在 IntelliJ 中运行它。 它在命令行(./gradlew build)上使用gradle-Wrapper进行构建,但我希望使用 IntelliJ 进行构建,以提高使用率(重新编译+重新加载)以及 IDE 的其他功能

我有以下模块:

  • A(A 是我尝试构建的模块)

  • B(B 是模块,我想在其中重用模块 A 的 sourceSets 'main' 和 'testutils')

模块 A - 由 3 个源集组成(=main、test、testutils):

src/main/java
src/test/java
src/testutils/java

模块 A 具有 testutils(使用“main”)、test(使用“main”和“testutils”)、main(模块的生产 java-src)的依赖项

模块B,其中sourceSet testutils main 内需要模块A src 和 tests ; 模块 B - 使用 sourceSet 'A/src/main/java' 和 'A/src/testutils/java':

所以我的 gradle 文件是(稍微缩短):

A/build.gradle:

apply plugin: 'maven'

sourceSets {
    test.java.srcDirs +='src/testutils/java'
    testutils
}

dependencies {
    api project('someOtherProjectA')
    ...

    testImplementation project('someOtherProjectZZ')
    testutilsImplementation sourceSets.test.compileClasspath
}

task testutilsJar(type: Jar) {
    from sourceSets.testutils.output
      appendix 'testutils'
}

configurations {
    testutils
}

artifacts {
    testutils testutilsJar
}

uploadTestutils {
    repositories {
        mavenDeployer {
                configuration = configurations.testutils
                repository(url: mavenPublicRepository) {
                      authentication(userName: repoUsername, password: repoPassword)
                }
        }           
    }
}

publish.dependsOn uploadTestutils

B/build.gradlew:

dependencies {
  api project('someOtherProjectB')

  api "someOtherDependency"
  ...

  testImplementation project('someOtherProjectC')       
  testImplementation project(path: ':A:theNameOfTheModule', configuration: 'testutils')
  ...
  
}

我的 DEV 系统设置:

JDK: 11.0.4
Gradle-Wrapper with Version: gradle-5.5.1
IntelliJ IDEA 2020.1 (Ultimate Edition) - Build #IU-201.6668.121, built on April 8, 2020
    Runtime version: 11.0.6+8-b765.25 x86_64
    VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
    macOS 10.15.4
    Memory: 3987M
    Cores: 16

我已经尝试使用来自的答案

如果您建议我如何管理我的“testutils”-SourceSet,我将非常感激,它还使用嵌入式 IntelliJ-Build 构建封闭项目:

  • 设置(=构建、执行、部署> 构建工具> Gradle)
    • 使用 >“IntelliJ IDEA”(而不是“Gradle”)构建并运行
    • 使用 >“IntelliJ IDEA”(而不是“Gradle”)运行测试

最佳答案

我可以找到解决我的问题的方法。

sourceSet 在模块 A 中定义并在模块 B 中引用。

模块A的sourceSets:

  • 主要
  • testutils(可以使用 main 并查看 main 和 test 的所有依赖项)
  • 测试(测试主 + testutils)

模块 A (build.gradle) 中:

apply plugin: 'maven'

sourceSets {
    testutils
    testutils.compileClasspath += sourceSets.main.output
    testutils.runtimeClasspath += sourceSets.main.output
    test.compileClasspath += sourceSets.testutils.output
    test.runtimeClasspath += sourceSets.testutils.output
}

dependencies {
    api project('someOtherProjectA')
    ...

    testImplementation project('someOtherProjectZZ')
    testutilsImplementation sourceSets.main.compileClasspath
}
...

模块 B (build.gradle) 中:

dependencies {
  testImplementation project(path: ':A:theNameOfTheModule', configuration: 'testutils')
  ...
}

模块 B 只能访问模块 A 的 sourceSet testutils

关于java - Gradle SourceSet IntelliJ 与 Eclipse,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61240379/

相关文章:

启动时出现Eclipse错误

gradle - Unresolved reference : launch

gradle - 如何使用 Gradle 和 Kotlin DSL 创建额外的 Kotlin SourceSet

android - Gradle 同步失败 : invalid type code: 6C

java - 通过以太网将Arduino的模拟传感器值发送到android应用程序

java - Eclipse 桌面应用程序

java - GEF 和 GMF 之间的关系?

java - 使用eclipse使用ssh连接到远程机器[linux服务器]

java - Java 字节码中的平台独立性

java - 在 Sahi 中记录 Java 代码