使用 Gradle 将 Java 工件发布到 Maven Local

Publish Java artifact to Maven Local with Gradle

我在尝试将生成的 jar 安装到本地 Maven 存储库时遇到问题。消息错误只是告诉我“找不到任务 'publish'”

我正在使用这个 Gradle 脚本:

buildscript {
    ext {
        springBootVersion = '1.3.2.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") 
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'
apply plugin: 'maven-publish'

jar {
    baseName = 'mongofoundry'
    version = '1.0.0'
}
sourceCompatibility = 1.7
targetCompatibility = 1.7


repositories {
    mavenCentral()
}


dependencies {
    compile('org.springframework.boot:spring-boot-starter-web')
    compile('org.springframework.boot:spring-boot-starter-data-mongodb')
    testCompile('org.springframework.boot:spring-boot-starter-test') 
}

publishing {
    publications {
        mavenJava(MavenPublication) {
            from components.java
        }
    }
}


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

task wrapper(type: Wrapper) {
    gradleVersion = '2.9'
}

你知道我为什么会读到那条错误消息吗? 谢谢

已更新

运行 @RaGe 提到的命令,解决了问题:

gradle publishToMavenLocal.

将工件发布到本地 maven 的正确任务是

gradle publishToMavenLocal

maven plugin 添加到您的项目,然后: gradle clean install

本地检查Maven

对于开发和测试,在本地检查库很有用

gradle apply plugin: 'com.android.library' 而非 apply plugin: 'java-library' 的设置(默认情况下您可以使用它)

apply plugin: 'maven-publish'

//simple settings
project.afterEvaluate {
    publishing {
        publications {
            library(MavenPublication) {
                //setGroupId groupId
                setGroupId "com.company"
                //setArtifactId artifactId
                setArtifactId "HelloWorld"
                version "1.1"

                artifact bundleDebugAar

/* add a dependency into generated .pom file
                pom.withXml {
                    def dependenciesNode = asNode().appendNode('dependencies')
                    def dependencyNode = dependenciesNode.appendNode('dependency')
                    dependencyNode.appendNode('groupId', 'com.company')
                    dependencyNode.appendNode('artifactId', 'HelloWorld-core')
                    dependencyNode.appendNode('version', '1.1')

                }
*/
            }
        }
    }
}

到 运行 使用命令行或在 Gradle 选项卡中找到此命令

./gradlew publishToMavenLocal

位置

artefact 将添加到 .m2 文件夹中

//Unix
~/.m2

//Windows
C:\Users\<username>\.m2

//For example
/Users/alex/.m2/repository/<library_path>/<version>/<name>.<extension>

build 文件夹

<project_path>/build/outputs/<extension>

其他存储库位置

~/.gradle/caches/modules-2/files-2.1/<group_id>/<artifact_id>/<version>/<id>

//For example
/Users/alex/.gradle/caches/modules-2/files-2.1/com.company/HelloWorld/1.1/c84ac8bc425dcae087c8abbc9ecdc27fafbb664a

要使用它,请添加 mavenLocal()。重要的是将它放在第一项以优先考虑它,这对于内部依赖性很有用

buildscript {
    repositories {
        mavenLocal()
    }

allprojects {
    repositories {
        mavenLocal()
    }
}

dependencies {
    implementation 'com.company:HelloWorld:+'
}

*还请记住,如果您使用一种 shared.gradle 文件(通过 apply from),您应该设置与 [=26= 相关的 path ] (不是 shared.gradle)

[iOS CocoaPod local]

这是我用 Kotlin DSL (build.gradle.kts) for my Android library 做的:

plugins {
    id("maven-publish")
    // OR simply
    // `maven-publish`
    // ...
}

publishing {
    repositories {
        // Local repository which we can first publish in it to check artifacts
        maven {
            name = "LocalTestRepo"
            url = uri("file://${buildDir}/local-repository")
        }
    }
    publications {
        // ...
    }
}

您可以使用以下命令创建所有发布:

./gradlew publishAllPublicationsToLocalTestRepoRepository

或者仅使用此命令发布:

./gradlew publishReleasePublicationToLocalTestRepoRepository

有关详细信息,请参阅 Gradle documentations: Maven Publish Plugin