简体   繁体   English

gradle在POM中发布包含源文件和依赖项的Jar

[英]gradle publishing Jar with source files and dependencies in POM

I am using gradle and I want to create a .jar with source files when doing a publishToMavenLocal . 我正在使用gradle,我想在执行publishToMavenLocal时使用源文件创建.jar I have got that to work easily: 我有这个容易工作:

task jarWithSources(type: Jar) {
    from sourceSets.main.output
    if (gradle.startParameter.taskNames.any{it == "publishToMavenLocal"}) {
        from sourceSets.main.allJava
    }
}

publishing {
    publications {
        // publish the data_deposit jar as a standalone artifact
        mavenJar(MavenPublication) {
            artifact jarWithSources
            artifactId "${jar.baseName}_jar"
            version project.version
        }
    }
    repositories {
        maven { ... }
    }
}

However the pom.xml is missing the dependencies. 但是pom.xml缺少依赖项。

If I use this: 如果我用这个:

publishing {
    publications {
        // publish the data_deposit jar as a standalone artifact
        mavenJar(MavenPublication) {
            // artifact jarWithSources    // Stopped using this
            from components.java          // Have added this
            artifactId "${jar.baseName}_jar"
            version project.version
        }
    }
    repositories {
        maven { ... }
    }
}

Where this is only that one change in the MavenPublication , then I get the full pom.xml but no source of course. 这只是MavenPublication中的一个变化,那么我得到了完整的pom.xml但当然没有来源。 I cannot find a way to include both artifact jarWithSources and from components.java . 我找不到包含artifact jarWithSourcesfrom components.java The error is: 错误是:

Invalid publication 'mavenJar': multiple artifacts with the identical extension and classifier ('jar', 'null').

Implying that from components.java is of type: Jar also. 暗示from components.javatype: Jar也是。

Can anyone advise how I can get all this to work? 任何人都可以建议我如何让这一切工作?

Now to end this question, I have to say that Gradle has a very very very steep learning curve. 现在结束这个问题,我不得不说Gradle有一个非常非常陡峭的学习曲线。 Its a computer language (DSL) so must be deterministic though sometimes I wonder. 它是一种计算机语言(DSL)所以必须是确定性的,尽管有时我想知道。 I do not have the knowledge to understand how (for example) from components.java works (#1). 我不知道如何(例如) from components.java工作(#1)。 For as much documentation as there is (such as http://www.gradle.org/docs/current/userguide/publishing_maven.html and the DSL guide), it isn't very clear. 对于尽可能多的文档(例如http://www.gradle.org/docs/current/userguide/publishing_maven.html和DSL指南),它不是很清楚。 I think the documentation could be a lot richer. 我认为文档可能更丰富。 And there needs to be a chapter on "getting it". 并且需要有一章关于“得到它”。 There is some paradigm shift that one seems to need to understand! 人们似乎需要了解一些范式转变!

#1 - as best I understand this gives you what is at http://www.gradle.org/docs/nightly/userguide/java_plugin.html#sec:java_plugin_and_dependency_management but how does that work with a from and in the context of a MavenPublication ? #1 - 据我所知,这可以为您提供http://www.gradle.org/docs/nightly/userguide/java_plugin.html#sec:java_plugin_and_dependency_management的内容,但是如何使用from和来自的上下文MavenPublication

Here you can find working example, it's enough to invoke gradle clean publishToMavenLocal to have artifacts build and published to maven local maven repo (artifact will be taken from folder name version number is unspecified ). 在这里你可以找到工作示例,它足以调用gradle clean publishToMavenLocal来构建工件并发布到maven本地maven repo(工件将取自文件夹名称版本号未指定 )。

It seems that build.gradle file should be slightly altered. 看来build.gradle文件应该略有改动。 Is the project published, may I have a try? 该项目是否已发布,请问我可以尝试一下吗?

At the beginning gradle indeed seems to be difficult, but after configuring first projects you'll quickly catch it. 开始时gradle似乎确实很难,但在配置完第一个项目之后你很快就会抓住它。

EDIT (after discussion in comments) 编辑 (在评论中讨论后)

To include both sources and compiled classes in a single jar add the following piece of code to build.gradle : 要在一个jar中包含源代码和已编译的类,请将以下代码添加到build.gradle

jar { 
   from sourceSets.main.output
   from sourceSets.main.allJava
}

And remove task sourceJar and artifact section from publications . 并从publications删除任务sourceJarartifact部分。 It works however including both sources and compiled classes is quit unusual and not a good idea. 它可以工作但包括源和编译类是退出不寻常,不是一个好主意。

Dependencies are added automatically installed pom.xml - see for guice . 自动安装pom.xml添加依赖关系 - 请参阅guice I've updated project on GitHub. 我在GitHub上更新了项目。

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

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