Intellij和Eclipse无法找到库工件

时间:2018-07-16 07:00:22

标签: java android eclipse maven intellij-idea

我在Android Studio中有一个图书馆项目,该项目将在Android和Java项目中使用。我正在使用Android Studio随附的maven-publish插件将此库以.aar和.jar格式发布到我自己的远程工件存储库中。我可以在Android应用程序中正确获取和使用该库,但是当我尝试在Java项目中使用它时,即使该库存在, intellij和eclipse也无法检测到 / 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"
                }
            }
        }
    }
}

我在做什么错?

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。事实证明gradle中的com.android.library插件以某种方式弄乱了生成的工件,因此只有Android项目可以使用库工件,而不能使用普通的Java项目。由于我的库没有任何布局资源,因此我可以跳过制作.aar的操作,而只生成一个jar文件。为了生成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'
    }
}