如何将maven清单添加到指定依赖关系的AAR [英] How to add a maven manifest to an AAR which specifies dependencies

查看:172
本文介绍了如何将maven清单添加到指定依赖关系的AAR的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我(终于)能够使用这个指南。它作为AAR而不是JAR文件发布,这意味着即使生成的POM文件列出了其所有内部依赖项,在将库添加到另一个项目时也会忽略它们。似乎是一个非常常见的问题。我理解这个,我只需列出maven清单中的依赖关系,但这是怎么做到的?

I was (finally) able to publish my Android library to an AWS S3 maven repository using this guide. It's published as an AAR instead of JAR file, which means that even though the generated POM file lists all its internal dependencies, they are ignored when adding the library to another project. Seems like a pretty common problem. I understand from this that I just need to list the dependencies in a maven manifest, but how is this done?

我真的是新手,所以越简单越好......谢谢!

I'm really a newbie at this, so the simpler the better... Thanks!

注意:我还发现这个问题,所以我在依赖项实现行的末尾添加了{transitive = true}并且它工作正常。

Note: I also found this question, so I added "{transitive=true}" at the end of the dependency implementation line and it worked.

所以现在要成功包含我的库,依赖项必须编码为:

So now to include my library successfully, the dependency must be coded as:

implementation (group: 'com.mygroup', name: 'my_library', version: '1.0', ext: 'aar', classifier: 'release') { transitive=true }

如何避免要求transitive = true,更一般地说,将线条简化为更接近:

How can I avoid requiring "transitive=true" and, more generally, simplify the line to be closer to:

implementation 'com.mygroup:my_library:1.0'

我试图简化它as ...

I tried to simplify it as...

implementation ('com.humanvideoboard:hvb_library:1.0@aar,release') { transitive=true }

...并且gradle同步运行正常,但是当我尝试编译时,我得到以下内容: / p>

...and the gradle sync ran fine, but when I tried to compile I got the following:

error: package com.mygroup.library does not exist

这是我用来发布库的脚本(注意它只发布了版本构建,但依赖行仍然需要release分类器):

Here is the script I used to publish the library (note that it only publishes the release build, but the dependency line still requires the "release" classifier):

apply plugin: 'maven-publish'

// update these next lines to fit your submodule
group = 'com.mygroup'
version = '1.0'

// Add sources as an artifact
task sourceJar(type: Jar) {
    from android.sourceSets.main.java.srcDirs
    classifier "source"
}

// Loop over all variants
android.libraryVariants.all { variant ->
    if (variant.buildType.name == "release") { // only release build
        variant.outputs.all { output ->
            // This creates a publication for each variant
            publishing.publications.create(variant.name, MavenPublication) {
                // The sources artifact from earlier
                artifact sourceJar

                // Variant dependent artifact, e.g. release, debug
                artifact source: output.outputFile, classifier: output.name

                // Go through all the dependencies for each variant and add them to the POM
                // file as dependencies
                pom.withXml {
                    def dependencies = asNode().appendNode('dependencies')

                    // Filter out anything that's not an external dependency.
                    configurations.getByName(variant.name + "CompileClasspath").allDependencies
                            .findAll { it instanceof ExternalDependency }
                            .each {
                        def dependency = dependencies.appendNode('dependency')

                        dependency.appendNode('groupId', it.group)
                        dependency.appendNode('artifactId', it.name)
                        dependency.appendNode('version', it.version)
                    }
                }
            }
        }
    }
}

// Ensure that the publish task depends on assembly
tasks.all { task ->
    if (task instanceof AbstractPublishToMaven) {
        task.dependsOn assemble
    }
}

// Configure the destination repository with
// S3 URL and access credentials
publishing {
    // Properties properties = new Properties()
    // properties.load(file('maven.properties').newDataInputStream())

    repositories {
        maven {
            url "s3://androidsdk.mygroup.com.s3.amazonaws.com"
            credentials(AwsCredentials) {
                accessKey "myaccesskey"
                secretKey "mysecretkey"
            }
        }
    }
}

myLibrary-1.0.pom文件包含以下内容。 (请注意,还有myLibrary-1.0.pom.md5和myLibrary-1.0.pom.sha1文件,但我不知道为什么会创建这些文件。)

The myLibrary-1.0.pom file contains the following. (Note that there are also myLibrary-1.0.pom.md5 and myLibrary-1.0.pom.sha1 files but I have no idea why those are created.)

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.mygroup</groupId>
  <artifactId>my_library</artifactId>
  <version>1.0</version>
  <packaging>pom</packaging>
  <dependencies>
    <dependency>
      <groupId>com.android.support.constraint</groupId>
      <artifactId>constraint-layout</artifactId>
      <version>1.1.2</version>
    </dependency>
    <dependency>
      <groupId>com.android.support</groupId>
      <artifactId>design</artifactId>
      <version>27.1.1</version>
    </dependency>
    <dependency>
      <groupId>com.google.android.gms</groupId>
      <artifactId>play-services-gcm</artifactId>
      <version>15.0.1</version>
    </dependency>
    <dependency>
      <groupId>io.reactivex.rxjava2</groupId>
      <artifactId>rxandroid</artifactId>
      <version>2.1.0</version>
    </dependency>
    <dependency>
      <groupId>io.reactivex.rxjava2</groupId>
      <artifactId>rxjava</artifactId>
      <version>2.2.4</version>
    </dependency>
    <dependency>
      <groupId>com.github.instacart.truetime-android</groupId>
      <artifactId>library-extension-rx</artifactId>
      <version>3.3</version>
    </dependency>
    <dependency>
      <groupId>com.google.dagger</groupId>
      <artifactId>dagger-android</artifactId>
      <version>2.15</version>
    </dependency>
    <dependency>
      <groupId>com.nostra13.universalimageloader</groupId>
      <artifactId>universal-image-loader</artifactId>
      <version>1.9.5</version>
    </dependency>
    <dependency>
      <groupId>com.android.support</groupId>
      <artifactId>appcompat-v7</artifactId>
      <version>27.1.1</version>
    </dependency>
  </dependencies>
</project>


推荐答案

您应该可以通过更改POM来解决此问题XML生成,以便< packaging> 部分设置为 aar 而不是 pom

You should be able to resolve this by changing your POM XML generation so that the <packaging> section is setup as aar rather than pom:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.humanvideoboard</groupId>
  <artifactId>hvb_library</artifactId>
  <version>1.0</version>
  <packaging>aar</packaging>

但是,现在我正在重新检查这个,原因是为什么它适用于 transitive 依赖关系是通过使用实现 vs api 来构建库或构建使用库的东西。如果您的库从其依赖项中公开类型作为其公共API的一部分,则需要在库构建中正确处理这些构建依赖项。有关详细信息,请参见此页

However, now that I'm re-examining this, the cause for why it works with transitive dependencies is by the use of implementation vs api when building the library or when building the thing which is using the library. If your library is exposing types from its dependencies as part of its public API, then those build dependencies need to be handled correctly in your library build. See this page for more information.

这篇关于如何将maven清单添加到指定依赖关系的AAR的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆