简体   繁体   English

Intellij和Eclipse无法找到库工件

[英]Intellij and Eclipse unable to find library artifact

I've a library project in Android studio that will be used in both Android and Java projects. 我在Android Studio中有一个图书馆项目,该项目将在Android和Java项目中使用。 I'm publishing this library to my own remote artifact repository in both .aar and .jar formats using the maven-publish plugin that's shipped with Android Studio. 我正在使用Android Studio随附的maven-publish插件将此库以.aar和.jar格式发布到我自己的远程工件存储库中。 I can fetch and use the library properly in an Android app however when I try to use it in a Java project, intellij and eclipse are unable to detect even if the library is there even though gradle/maven is fetching the library from the remote repo . 我可以在Android应用程序中正确获取和使用该库,但是,当我尝试在Java项目中使用它时, 即使gradle / maven从远程仓库获取了该库intellij和eclipse也无法检测到该库是否存在 I've tried pushing only aar to the remote repo and fetching that in Java projects but that didn't workout either. 我试过只将aar推送到远程仓库,并在Java项目中获取它,但也没有锻炼。 Please note that I've no Android dependency/resources in this library. 请注意,该库中没有Android依赖项/资源。 Following is the script being used to generate and push the artifacts to remote server. 以下是用于生成工件并将其推送到远程服务器的脚本。

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"
                }
            }
        }
    }
}

What am I doing wrong? 我究竟做错了什么?

I found the solution. 我找到了解决方案。 Turns out the com.android.library plugin in gradle was somehow messing with the generated artifact such that only Android projects could use the library artifact and not the plain java projects. 事实证明gradle中的com.android.library插件以某种方式弄乱了生成的工件,因此只有Android项目可以使用库工件,而不能使用普通的Java项目。 Since my library doesn't have any layout resources, I can skip making .aar and just generate a jar file. 由于我的库没有任何布局资源,因此我可以跳过制作.aar的操作,而只生成一个jar文件。 For generating the jar file and publishing it to the remote repo following snippet is being utilized: 为了生成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'
    }
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM