【问题标题】:Intellij and Eclipse unable to find library artifactIntellij 和 Eclipse 无法找到库工件
【发布时间】:2018-12-23 16:06:21
【问题描述】:

我在 Android Studio 中有一个库项目,将在 Android 和 Java 项目中使用。我正在使用 Android Studio 附带的 maven-publish 插件以 .aar 和 .jar 格式将此库发布到我自己的远程工件存储库。我可以在 Android 应用程序中正确获取和使用该库,但是当我尝试在 Java 项目中使用它时,intellij 和 eclipse 即使库在那里也无法检测到 即使 gradle /maven 正在从远程仓库获取库。我尝试只将 aar 推送到远程仓库并在 ​​Java 项目中获取它,但这也没有成功。请注意,我在这个库中没有 Android 依赖项/资源。以下是用于生成工件并将其推送到远程服务器的脚本。

apply plugin: 'com.android.library'
apply plugin: 'maven-publish'

android.libraryVariants.all { variant ->
    variant.outputs.all {
        outputFileName = "${_groupId}-${_artifactId}-${android.defaultConfig.versionName}.aar"
    }
}

task sourceJar(type: Jar) {
    from android.sourceSets.main.java.srcDirs
    classifier "sources"
}

task deleteOldJar(type:Delete){
    delete 'release/MyLibrary.jar'
}

task exportJar(type:Copy){
    from('build/intermediates/bundles/release/')
    into('release/')
    include('classes.jar')
    rename('classes.jar','MyLibrary.jar')
}
publishing {
    repositories {
        maven {
            url _remoteRepoUrl
        }
    }
    publications {
        library(MavenPublication) {
            groupId _groupId
            artifactId _artifactId
            version android.defaultConfig.versionName
            artifact(sourceJar)
            artifact(exportJar)
            artifact ("$buildDir/outputs/aar/${_groupId}-${_artifactId}-${android.defaultConfig.versionName}.aar") { //aar artifact you want to publish
                builtBy assembleDebug
            }
            //generate pom nodes for dependencies
            pom.withXml {
                def dependenciesNode = asNode().appendNode('dependencies')
                configurations.compile.allDependencies.each { dependency ->
                    println "dependency name is: "+dependency.name
                    if(!dependency.name.equals('unspecified')) {
                        def dependencyNode = dependenciesNode.appendNode('dependency')
                        dependencyNode.appendNode('groupId', dependency.group)
                        dependencyNode.appendNode('artifactId', dependency.name)
                        dependencyNode.appendNode('version', dependency.version)
                        dependencyNode.appendNode('type', "aar")
                    }
                    else
                        println "dependency name is unspecified"
                }
            }
        }
    }
}

我做错了什么?

【问题讨论】:

    标签: java android eclipse maven intellij-idea


    【解决方案1】:

    我找到了解决方案。事实证明,gradle 中的 com.android.library 插件不知何故弄乱了生成的工件,以至于只有 Android 项目可以使用库工件,而不是普通的 java 项目。由于我的库没有任何布局资源,我可以跳过制作 .aar 并只生成一个 jar 文件。使用 sn-p 生成 jar 文件并将其发布到远程仓库:

    apply plugin: 'java'
    apply plugin: 'eclipse'
    apply plugin: 'idea'
    apply plugin: 'maven-publish'
    
    sourceCompatibility = 1.8
    
    def _remoteRepoUrl = "http://myserver.com/repo"
    def _groupId = group
    def _versionName = version
    
    repositories {
        mavenCentral()
        jcenter()
        maven {
            url _remoteRepoUrl
        }
        flatDir {
            dirs 'libs'
        }
    }
    
    jar {
        from sourceSets.main.output
    }
    
    task jarWithSources(type: Jar) {
        from sourceSets.main.output
        if (gradle.startParameter.taskNames.any{it == "publishToMavenLocal"}) {
            from sourceSets.main.allJava
        }
    }
    
    // Define how maven-publish publishes to the nexus repository
    publishing {
        repositories {
            maven {
                url _remoteRepoUrl
            }
        }
        publications {
            mavenJava(MavenPublication) {
                from components.java
            }
            // publish the <project> jar as a standalone artifact
            mavenJar(MavenPublication) {
                artifact jarWithSources
                //from components.java -- can't have both the above line and this
                //artifactId "${jar.baseName}_jar"
                version project.version
            }
        }
    }
    
    eclipse {
        classpath {
            containers.remove('org.eclipse.jdt.launching.JRE_CONTAINER')
            containers 'org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7'
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-11
      相关资源
      最近更新 更多