简体   繁体   English

Maven 库不会从 POM 内部的私有存储库中获得依赖

[英]Maven library don’t get dependency from private repository inside POM

I've been banging my head on this problem for a few days.几天来我一直在解决这个问题。

My situation is this:我的情况是这样的:

  • I have a sdk written by me that uses two dependencies hosted on a private maven repository which can only be accessed via login我有一个由我编写的 sdk,它使用托管在私有 Maven 存储库上的两个依赖项,只能通过登录访问

    • repo-1 hosted on S3 maven托管在 S3 maven 上的repo-1
    • repo-2 hosted on Nexus repo-2托管在 Nexus 上
  • This sdk was always used within my Android app (but treated as a sub-module) within the project.该 sdk 始终在项目中的我的 Android 应用程序中使用(但被视为子模块)。

  • Inside the project's Gradle there are the 2 maven repositories from which to look for the 2 private dependencies mentioned above.在项目的 Gradle 中有 2 个 maven 存储库,可以从中查找上述 2 个私有依赖项。

maven {
    name “repo-1”
    url "s3://repo-1/maven"
    credentials(AwsCredentials) {
        accessKey awsUsername
        secretKey awsPassword
    }
}

maven {
    name “repo-2”
    url "http://repo-2/repository/maven-releases"
    credentials {
        username ‘repo-2-name’
        password ‘repo-2-password’
    }
}
  • The 2 dependency mentioned above is used ONLY by the sdk, the app is completely unaware of it上面提到的 2 依赖项仅由 sdk 使用,应用程序完全不知道它

Now I want to completely divide the two things and host the SDK on my private maven repository, in order to hide all the code and the dependencies concerning the SDK from the app.现在我想将这两件事完全分开,并将 SDK 托管在我的私有 Maven 存储库中,以便从应用程序中隐藏与 SDK 相关的所有代码和依赖项。

First I want to do everything locally, so I could be faster to do different tests.首先,我想在本地完成所有操作,这样我可以更快地进行不同的测试。 I therefore "uploaded" my SDK in my local maven.因此,我在本地 Maven 中“上传”了我的 SDK。

In the POM that I generated, I have all the necessary dependencies to the SDK (including the 2 dependencies of the private repositories)like this :在我生成的 POM 中,我拥有对 SDK 的所有必要依赖项(包括私有存储库的 2 个依赖项),如下所示:

<packaging>aar</packaging>
    <dependencies>
            <dependency>
                 <artifactId>repo-1</artifactId>
                    <groupId>com.repo-1.android</groupId>
                    <version>0.0.1</version>
            </dependency>
        <dependency>
                <artifactId>repo-2</artifactId>
                <groupId>com.repo-2.android</groupId>
                <version>1.0.0</version>
        </dependency>

and in addition i have also added the 2 repositories from which the 2 private dependencies must be downloaded :此外,我还添加了 2 个存储库,必须从中下载 2 个私有依赖项:

<repositories>
        <repository>
            <id>repo-1</id>
            <name>repository-1</name>
            <url>http://repo-1/repository/maven-releases</url>
        </repository>
        <repository>
            <id>repo-2</id>
            <name>repository-2</name>
            <url>s3://repo-1/maven</url>
        </repository>
</repositories>

I then set the settings.xml file , in the root of my local maven (../.m2) , with the credentials to access the 2 repositories.然后,我在本地 maven (../.m2) 的根目录中设置 settings.xml文件,并使用凭据来访问 2 个存储库。

<settings 
    xmlns="http://maven.apache.org/SETTINGS/1.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
        https://maven.apache.org/xsd/settings-1.0.0.xsd">
     <servers>
            <server>
                <id>repo-1</id>
                <username>awsPassword</username>
                <password>awsPassword</password>
            </server>
            <server>
                <id>repo-2</id>
                <username>repo-2-name</username>
                <password>repo-2-password</password>
            </server>
    </servers>
</settings>

I then added my sdk as a dependency within the app, but does not want to download the 2 dependencies from the private repositories.然后我将我的 sdk 添加为应用程序中的依赖项,但不想从私有存储库下载 2 个依赖项。

BUT

If I add the two maven repository in the gradle of the app project (like the first example), however, it works, so I'm pretty sure it's a problem of pom and setting.xml configuration.但是,如果我在应用程序项目的 gradle 中添加两个 maven 存储库(如第一个示例),则它可以工作,所以我很确定这是pomsetting.xml配置的问题。

Thank you very much to everyone for any answers非常感谢大家的任何答案

What you are facing is a difference between the way Maven and Gradle handle repositories.您面临的是 Maven 和 Gradle 处理存储库的方式之间的差异。

In short Gradle is more strict and will not transparently download dependencies from repositories that are not declared in a build script.简而言之,Gradle 更严格,不会从未在构建脚本中声明的存储库透明地下载依赖项。

For more details on why this is the case, have a look at the discussion in the Gradle issue tracker .有关为什么会出现这种情况的更多详细信息,请查看Gradle 问题跟踪器的讨论

So the solution, as you found out, is to declare these repositories in the consuming build script.因此,正如您所发现的,解决方案是在使用构建脚本中声明这些存储库。 If you believe that's too verbose, consider writing a minimal plugin for your SDK that would add the dependencies and the repositories.如果您认为这太冗长,请考虑为您的 SDK 编写一个最小的插件来添加依赖项和存储库。

It would then become:然后它会变成:

plugins {
    id 'my-sdk-plugin'
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 来自gitlab本地服务器的私有存储库的Android库Maven依赖项 - Android library maven dependency from gitlab local server's private repository 为什么我无法在Maven存储库中的Android项目中使用库? 如何为此编写正确的pom.xml? - Why I can't use a library in my Android project from Maven repository? How to write a correct pom.xml for this? Gradle构建无法从自定义maven存储库获取依赖关系 - Gradle build failing to get dependency from custom maven repository 在私有服务器上设置 Maven Repository 以共享 Android Studio 的 android 库 - Setup Maven Repository on private server to share the android library for Android Studio 私有 Maven 存储库中的多模块 android 项目 - Multi module android project inside a private maven repository Gradle/Maven/Android:通过 maven 使用库时找不到库私有依赖项 - Gradle/Maven/Android: Library-private dependency not found when library is used via maven IDE无法打开库依赖项对话框(Maven依赖项) - The IDE can't open library dependency dialog (Maven dependency) 如果Maven存储库发布者删除其存储库,那么在Android Studio中具有远程库依赖项的项目会发生什么情况 - What happens to project with remote library dependency in android studio if maven repository publisher removes his repository pom.xml中的Maven依赖错误 - Maven Dependency Error in pom.xml Maven拒绝从远程存储库下载aar打包的依赖项 - Maven refuse to download aar packaged dependency from remote repository
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM