Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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.

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. I cannot find a way to include both artifact jarWithSources and from 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.

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. Its a computer language (DSL) so must be deterministic though sometimes I wonder. I do not have the knowledge to understand how (for example) from components.java works (#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. 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?

like image 981
HankCa Avatar asked Jan 12 '15 03:01

HankCa


People also ask

Where are JAR files published in Gradle?

As of Gradle 6.0, the Gradle Module Metadata will always be published alongside the Ivy XML or Maven POM metadata file.

Does Gradle use pom files?

Gradle ships with a Maven plugin, which adds support to convert a Gradle file to a Maven POM file. It can also deploy artifacts to Maven repositories. The plugin uses the group and the version present in the Gradle file and adds them to the POM file. Also, it automatically takes the artifactId from the directory name.


1 Answers

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).

It seems that build.gradle file should be slightly altered. 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.

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 { 
   from sourceSets.main.output
   from sourceSets.main.allJava
}

And remove task sourceJar and artifact section from publications. 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. I've updated project on GitHub.

like image 57
Opal Avatar answered Oct 05 '22 23:10

Opal