如何防止 Gradle 在每次导入后将我的模块作为 IntelliJ 中的孤立模块移除

huangapple 未分类评论50阅读模式
英文:

How to prevent gradle to remove my Modules as orphan modules in intellij after each import

问题

我们有一个使用Gradle多模块构建的Java项目。该项目规模庞大,包含许多模块。我们的.iml文件也受版本控制。

在更改Gradle文件并导入Gradle之后,环境可以正常工作。然而,在这个过程中,会移除所有的.iml文件。有时,它会直接将这些文件放在"Locally Deleted Files"中,但有时它会询问我们是否要移除这些孤立的模块,以及是否需要还原它们。同时,它会创建新的.iml文件。我们的.iml文件只有一个单词的名称,但Gradle创建的.iml文件的名称包含整个模块路径,格式为projectName.subfolder.moduleName

我们在project.ipr中保存了许多运行配置(runConfiguration),它们需要正确的模块名称才能正常工作。我曾尝试过删除所有的.iml文件,让Gradle重新创建这些文件,并将ipr文件中的运行配置更改为使用Gradle创建的.iml文件名称。然后,在下一次导入Gradle时,它再次移除了由它生成的所有文件,并在.iml文件名之前添加了根目录名称。

所以,如果第一次由Gradle创建的.iml文件名是projectName.subdirectory.moduleName,它会创建一个新的.iml文件,名称为rootDirectory.projectName.subdirectory.moduleName。当我将运行配置中的模块名称更改为新名称后,再次导入Gradle时,它会从.iml文件名中移除rootDirectory

我的问题是,是否有一种方法可以配置Gradle,使其不创建新的模块?如果没有,是否有一种方法可以向Gradle提供.iml文件的名称,或者强制使用特定的名称而不进行更改?

我们使用最新的IntelliJ 2020.1版本,并且使用了带有Gradle 6.0.1的Gradle Wrapper。我们在IntelliJ中的Gradle设置如下图所示:

(图片已省略)

我已经尝试勾选"Generate iml files"选项并取消勾选它,但没有任何区别,它仍然以同样的方式工作。

整个项目的主要build.gradle中的源集(sourceSets)定义如下:

subprojects {
    apply plugin: 'java'
    sourceCompatibility = JavaVersion.VERSION_1_8
    targetCompatibility = JavaVersion.VERSION_1_8

    repositories {
        mavenCentral()
    }

    test {
        useJUnitPlatform()
    }

    dependencies {
        testImplementation 'junit:junit:4.12'
        testImplementation 'org.junit.jupiter:junit-jupiter-api:5.4.2'
        testImplementation 'org.junit.platform:junit-platform-runner:1.4.2'
        testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.4.2'
        testRuntimeOnly 'org.junit.vintage:junit-vintage-engine:5.4.2'

        testImplementation 'org.hamcrest:hamcrest-library:1.3'
    }

    configurations {
        runtimeLibraries
    }

    sourceSets {
        main {
            java {
                srcDirs = ["src"]
            }
            resources {
                srcDirs = ["resources"]
            }
        }
        test {
            java {
                srcDirs = ["test/src"]
            }
            resources {
                srcDirs = ["test/resources"]
            }
        }
    }
}
英文:

We have a java project with Gradle multi build project. The project is big and we have many modules. We have our iml files under version control.

The environment is working until we are changing something in gradle files and importing gradle. Then it will remove all of our iml files. Sometimes it puts them directly under Locally Deleted Files but sometimes it is asking us that it will remove the orphan modules, and we can restore them if we want. At the same time it creates new iml files. Our iml files names have just one word as name but Gradle creates iml files with whole path of modules as this projectName.subfolder.module name.

We have saved many runConfiguration in project.ipr which needs correct module name, so they works. I tried once to remove all of our iml files and let gradle to create iml files and I changed our runConfiguration in ipr file to use the iml file names created by gradle. Then next time while importing gradle removed again all files generated by it and add the root directory name at beginning of iml files.
So if iml files name created by it first time was projectname.subdirectory.modulename it created a new iml files with this name rootDirectory.projectname.subdirectory.modulename. And when I changed the module names in the run configuration name to the new name then when importing gradle again it removed the rootDirectory from the name of iml files.

My question is if there is a way to configure gradle so it doesn't create new modules? If not is there a way to give the iml file names to the gradle or force it to specific names which doesn't changes.

We are using latest Intellij 2020.1 andwe have gradle wrapper with gradle-6.0.1. Our gradle settings in intellij looks like this:

如何防止 Gradle 在每次导入后将我的模块作为 IntelliJ 中的孤立模块移除

I have tested to check the option Generate *iml files and uncheck it. But there is no differences it is working at the same way.
The definition of sourcesets looks like this in main build.gradle for whole project:

subprojects {

    apply plugin: 'java'
    sourceCompatibility = JavaVersion.VERSION_1_8
    targetCompatibility = JavaVersion.VERSION_1_8


    repositories {
        mavenCentral()
    }

    test {
        useJUnitPlatform()
    }

    dependencies {
        testImplementation 'junit:junit:4.12'
        testImplementation 'org.junit.jupiter:junit-jupiter-api:5.4.2'
        testImplementation 'org.junit.platform:junit-platform-runner:1.4.2'
        testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.4.2'
        testRuntimeOnly 'org.junit.vintage:junit-vintage-engine:5.4.2'

        testImplementation 'org.hamcrest:hamcrest-library:1.3'
    }

    configurations {
        runtimeLibraries
    }



    sourceSets {
        main {
            java {
                srcDirs = ["src"]
            }
            resources {
                srcDirs = ["resources"]
            }
        }
        test {
            java {
                srcDirs = ["test/src"]
            }
            resources {
                srcDirs = ["test/resources"]
            }
        }
    }

 
}

huangapple
  • 本文由 发表于 2020年4月10日 00:24:03
  • 转载请务必保留本文链接:https://java.coder-hub.com/61125734.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定