【发布时间】:2019-10-18 15:25:27
【问题描述】:
我使用maven-publish插件来部署android库(.aar)。
我的库还有另外一个依赖,也是.aar
如何从build.gradle、dependencies 部分导入所有依赖项:
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'
}
到生成的pom.xmldependencies 部分:
<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>
我找到了一些解释如何做类似的事情:
Optional Gradle dependencies for Maven libraries
How to change artifactory runtime scope to compile scope?
https://issues.gradle.org/browse/GRADLE-1749
所以,我明白,我应该使用pom.withXml,从project.configurations.compile.allDependencies 导入所有依赖项,范围为compile,并将其放入asNode().dependencies
但我不熟悉它,我认为我做错了什么。这是我当前的代码:
publishing {
publications {
maven(MavenPublication) {
artifact "${project.buildDir}/outputs/aar/${project.name}-release.aar"
artifactId = POM_ARTIFACT_ID
groupId = GROUP
version = VERSION_NAME
// Task androidSourcesJar is provided by gradle-mvn-push.gradle
//artifact androidSourcesJar {
// classifier "sources"
//}
pom.withXml {
def depsNode = asNode().dependencies.'*'
project.configurations.compile.allDependencies.each { dep ->
if(dep.name != null && dep.group != null && dep.version != null) {
def depNode = new Node(null, 'dependency')
def groupIdNode = new Node(depNode, 'groupId', dep.getGroup())
def artifactIdNode = new Node(depNode, 'artifactId', dep.getName())
def versionNode = new Node(depNode, 'version', dep.getVersion())
depsNode.add(depNode)
println depsNode
}
}
}
}
}
repositories {
maven {
credentials {
username System.getenv('NEXUS_USER_NAME')
password System.getenv('NEXUS_PASSWORD')
}
url "http://nexus-repo"
}
}
}
【问题讨论】:
-
您使用
pom.withXml来修改生成的 pom.xml,但您不必进行任何自定义配置来包含所有运行时依赖项(默认情况下还包括编译依赖项。)你遇到了什么问题? -
@RaGe,问题是生成的
pom.xml没有任何依赖关系。但我希望它具有compile范围的所有依赖项 -
@RaGe,当我构建
.aar依赖项并将其包含在另一个应用程序中时,我需要包含我的库项目中的所有依赖项,因为我总是得到ClassNotFoundException -
我上面的评论是针对一般的 gradle java 项目的——刚刚尝试过,
compile依赖项已正确写入 pom.xml 文件中。我想您的情况有所不同,因为它是一个 android 项目-环顾四周,我发现了一个专门用于在 android 插件中发布 .aar 文件的插件-在下面发布。
标签: maven gradle android-gradle-plugin pom.xml maven-publish