Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/maven/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Maven 如何将依赖项从build.gradle导入pom.xml_Maven_Gradle_Android Gradle Plugin_Pom.xml_Maven Publish - Fatal编程技术网

Maven 如何将依赖项从build.gradle导入pom.xml

Maven 如何将依赖项从build.gradle导入pom.xml,maven,gradle,android-gradle-plugin,pom.xml,maven-publish,Maven,Gradle,Android Gradle Plugin,Pom.xml,Maven Publish,我使用maven publish插件部署android库(.aar) 我的库还有另一个依赖项,它们也是.aar 如何从build.gradle,依赖项部分导入所有依赖项: dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) compile 'com.android.support:appcompat-v7:23.1.1' compile 'com.android.support:design:2

我使用
maven publish
插件部署android库(
.aar

我的库还有另一个依赖项,它们也是
.aar

如何从
build.gradle
依赖项
部分导入所有依赖项:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:23.1.1'
    compile 'com.android.support:design:23.1.1'
    compile 'com.android.support:support-v4:23.1.1'
    compile ('com.android.support:recyclerview-v7:22.2.1'){
        exclude group: 'com.android.support', module: 'support-v4'
    }
    compile 'com.inthecheesefactory.thecheeselibrary:stated-fragment-support-v4:0.10.0'

    //Http communication, websockets, etc.
    compile 'com.squareup.okhttp:okhttp:2.4.0'
    compile 'com.squareup.retrofit:retrofit:1.9.0'

    //Fonts
    compile 'uk.co.chrisjenx:calligraphy:2.1.0'

    //Unit tests
    testCompile 'junit:junit:4.12'
    testCompile 'org.mockito:mockito-core:1.9.5'

    //Other
    compile ('org.apache.commons:commons-lang3:3.4'){
        exclude group: 'org.apache.httpcomponents'
    }

    //Reactive programmnig
    compile 'io.reactivex:rxjava:1.0.13'
    compile 'io.reactivex:rxandroid:0.25.0'

    compile 'com.github.bumptech.glide:glide:3.6.1'
}
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.my.sdk</groupId>
<artifactId>SDK</artifactId>
<version>0.0.1</version>
<packaging>aar</packaging>

<dependencies>
   ...
</dependencies>

</project>
要生成
pom.xml
依赖项
部分:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:23.1.1'
    compile 'com.android.support:design:23.1.1'
    compile 'com.android.support:support-v4:23.1.1'
    compile ('com.android.support:recyclerview-v7:22.2.1'){
        exclude group: 'com.android.support', module: 'support-v4'
    }
    compile 'com.inthecheesefactory.thecheeselibrary:stated-fragment-support-v4:0.10.0'

    //Http communication, websockets, etc.
    compile 'com.squareup.okhttp:okhttp:2.4.0'
    compile 'com.squareup.retrofit:retrofit:1.9.0'

    //Fonts
    compile 'uk.co.chrisjenx:calligraphy:2.1.0'

    //Unit tests
    testCompile 'junit:junit:4.12'
    testCompile 'org.mockito:mockito-core:1.9.5'

    //Other
    compile ('org.apache.commons:commons-lang3:3.4'){
        exclude group: 'org.apache.httpcomponents'
    }

    //Reactive programmnig
    compile 'io.reactivex:rxjava:1.0.13'
    compile 'io.reactivex:rxandroid:0.25.0'

    compile 'com.github.bumptech.glide:glide:3.6.1'
}
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.my.sdk</groupId>
<artifactId>SDK</artifactId>
<version>0.0.1</version>
<packaging>aar</packaging>

<dependencies>
   ...
</dependencies>

</project>

要发布包含pom.xml中正确列出的依赖项的
.aar
库,使用插件可能比使用maven发布插件组装依赖项部分更容易

要应用插件,请执行以下操作:

plugins {
  id "com.github.dcendents.android-maven" version "1.3"
}
然后运行task
install
将库推送到本地.m2 repo

您可以覆盖正在发布的回购协议,如下所示:

install {
    repositories {
        mavenDeployer {
            repository(...)
        }
    }
}

当然,如果您愿意,可以继续使用
maven publish
插件。在代码中,您正在查找尚未存在的
depsNode
。您可能需要为depsNode创建一个新节点,并首先将其添加到pom中。或者只使用append:

pom.withXml {
    def depsNode  = asNode().appendNode('dependencies')

    configurations.compile.allDependencies.each {  dep ->
        def depNode  = depsNode.appendNode('dependency')
        depNode.appendNode('groupId', dep.group)
        depNode.appendNode('artifactId', dep.name)
        depNode.appendNode('version', dep.version)
        //optional add scope
        //optional add transitive exclusions
    }
}

这里需要注意的是,您仍然需要正确处理排除。此外,如果您的项目中有变体,并且具有不同的编译配置,例如
androidCompile
somethingelsecile
,则也需要正确处理这些变体。

您可以在maven发布任务中使用另一个项目或库的依赖项更新依赖项。这必须在jar生成任务之前完成。这样,更改将反映在pom生成中,而不需要pom xml操作。getDependencies是提取这些依赖项的方法,您应该实现它;)

这是发布任务中的代码段

        artifactId = POM_ARTIFACT_ID
        groupId = GROUP
        version = VERSION_NAME

        project.configurations.implementation.withDependencies { dependencies ->            
             dependencies.addAll(getDependencies(project)) 
        }

        from components.java

您可以使用
pom.withXml
修改生成的pom.xml,但不必进行任何自定义配置以包含所有运行时依赖项(默认情况下还包括编译依赖项)。您遇到了什么问题?@RaGe,问题是生成的
pom.xml
没有任何依赖项。但我希望它与
compile
scope@RaGe,当我构建我的
.aar
依赖项并将其包含在另一个应用程序中时,我需要包含我的库项目中的所有依赖项,因为我总是得到
ClassNotFoundException
我上面的评论通常是针对一个gradle java项目的-刚刚尝试过,并且
compile
依赖项正确地写入了pom.xml文件中。我想你的情况是不同的,因为这是一个android项目-环顾四周,我发现了一个专门用于发布android插件中的.aar文件的插件-发布在下面。谢谢你的回答。我今天稍后会尝试它,如果它对我有帮助的话,我会非常感激。我没有测试第一部分,因为第二部分适用于我。对于gradle maven插件版本1.4.1,请使用此configurations.releaseCilassPath.allDependencies.each在pom.xml中编写所有依赖项