我如何决定在 .gitignore 中添加 .idea/jarRepositories.xml

How can I decide to add .idea/jarRepositories.xml in .gitignore

我在 Mac 上将 Android Studio 3.5.3 更新为 4.0.1 时发现了一个不熟悉的文件 .idea/jarRepositories.xml。内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
  <component name="RemoteRepositoriesConfiguration">
    <remote-repository>
      <option name="id" value="central" />
      <option name="name" value="Maven Central repository" />
      <option name="url" value="https://repo1.maven.org/maven2" />
    </remote-repository>
    <remote-repository>
      <option name="id" value="jboss.community" />
      <option name="name" value="JBoss Community repository" />
      <option name="url" value="https://repository.jboss.org/nexus/content/repositories/public/" />
    </remote-repository>
    <remote-repository>
      <option name="id" value="BintrayJCenter" />
      <option name="name" value="BintrayJCenter" />
      <option name="url" value="https://jcenter.bintray.com/" />
    </remote-repository>
    <remote-repository>
      <option name="id" value="Google" />
      <option name="name" value="Google" />
      <option name="url" value="https://dl.google.com/dl/android/maven2/" />
    </remote-repository>
  </component>
</project>

已经有一个 on it, and 可以添加到.gitignore

我想知道我是否应该将它添加到 .gitignore 中而不应该提交到 git。我如何决定将其添加到 .gitignore

JetBrains.gitignore

流行的存储库 gitignore of github has Android.gitignore,但在撰写此问题时没有 .idea/jarRepositories.xml

另一方面,Global/JetBrains.gitignore 有以下评论。

# Gradle and Maven with auto-import
# When using Gradle or Maven with auto-import, you should exclude module files,
# since they will be recreated, and may cause churn.  Uncomment if using
# auto-import.
# .idea/artifacts
# .idea/compiler.xml
# .idea/jarRepositories.xml
# .idea/modules.xml
# .idea/*.iml
# .idea/modules
# *.iml
# *.ipr

如您所见,它有 .idea/jarRepositories.xml 并且该行被注释掉了。它还具有以下注释:

Gradle and Maven with auto-import

When using Gradle or Maven with auto-import, you should exclude module files, since they will be recreated, and may cause churn. Uncomment if using auto-import.

在 Android Studio 4.0.x 中,是否可以在不自动导入的情况下使用 Gradle?

远程 Jar 存储库

在上面的Global/JetBrains.gitignore中,# .idea/jarRepositories.xml行被davidkron的pull request, and he commented添加如下:

Since IntelliJ 2019.3 this file appeared automatically in our git changes. It seems these are just cached information about remote repositories that are defined in Maven/Gradle.

Reasons for making this change:

In a Maven/Gradle build, automatically generated files should should not be commited.

Links to documentation supporting these rule changes:

I haven't found a documentation or article about this file, but I'm basing my assumption on the following steps:

  • in IntelliJ the "Remote Jar Repositories" can be managed in the Settings menu
  • I deleted every entry in the list -> file disappeared
  • I re-imported the Maven/Gradle project again
  • the file appeared again with the same entries present as before

正如他的评论,Android Studio 4.0.1Remote Jar Repositories 设置,您可以在下面的屏幕截图中看到。

查看了AndroidStudio4.0.0的release notes,发现a section如下:

IntelliJ IDEA 2019.3.3

The core Android Studio IDE has been updated with improvements from IntelliJ IDEA through the 2019.3.3 release.

To learn more about the improvements from other IntelliJ versions that are included cumulatively with version 2019.3.3, see the following pages:

  • IntelliJ IDEA 2019.3
  • IntelliJ IDEA 2019.3.3

我不确定,但想知道我是否应该在 .gitignore 中添加 .idea/jarRepositories.xml 而不应将其提交到 git。我如何决定添加或不添加它?如果有人能澄清标准,我将不胜感激。

更新:

在 Android Studio 4.0.0 的源代码中,我找到了源文件,其中定义了文件名 jarRepositories.xml:

JpsRemoteRepositoriesConfigurationSerializer.java

public JpsRemoteRepositoriesConfigurationSerializer() {
  super("jarRepositories.xml", "RemoteRepositoriesConfiguration");
}

RemoteRepositoriesConfiguration.java

@State(name = "RemoteRepositoriesConfiguration", storages = @Storage("jarRepositories.xml"))
public class RemoteRepositoriesConfiguration implements PersistentStateComponent<RemoteRepositoriesConfiguration.State> {
  // ...
}

我还在 JetBrain 的 IntelliJ IDEA 社区版的存储库中找到了它们的来源。

Jps代表JetBrains Project System,根据a document

The project model in External Build process is provided by JPS (JetBrains Project System).

JPS's previous repository 将自己描述为:

Gant based build framework + dsl, with declarative project structure definition and automatic IntelliJ IDEA projects build

How can I decide to add it or not?

决定是否应忽略文件的明确通用标准是:

你能否在没有那个文件的情况下克隆存储库并且仍然有一个有效的项目(一个你可以编译和执行的项目)

例如,如果该文件是自动重新生成的,或者 re-created 具有自定义本地路径(这对于不同 PC 上的不同开发人员来说没有意义),则可以忽略该文件。

所以试试吧,特别是考虑到你可以“”。

文件 .idea/jarRepositories.xml 应添加到 .gitignore。它是 auto-generated,只有关于 remote jar repositories 的冗余信息。

我们的 Android 项目在 project-level build.gradle 中定义了那些存储库,例如:

allprojects {
    repositories {
        google()
        jcenter()
    }
}

第二个参考在其网页顶部写了以下介绍:

Add build dependencies

The Gradle build system in Android Studio makes it easy to include external binaries or other library modules to your build as dependencies. ... This page describes how to use dependencies with your Android project, ... but remember that your Android project must use only the dependency configurations defined on this page. [emphasis added]

此外,Add Library Dependencies 对话框在 Android Studio 4.0.1 中有这样的说明:

Step 1.

Use the form below to find the library to add. This form uses the repositories specified in the project's build files (Google, JCenter, Android Repository, Google Repository)

您可以通过以下步骤找到它:

  1. Select 文件 > 项目结构... 在 Android Studio 的菜单中。
  2. Select DependenciesProject Structure 对话框的 left-side 菜单中。
  3. Select appModules 列中。
  4. Select +Declared Dependencies 列中。
  5. Select 1 Library Dependency 在弹出菜单中。