Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to publish source into local maven repository with Gradle?

Tags:

java

gradle

I am developing two related project. One of which is "helper" one and another is "main" one. I am constantly updating "main", but from time to time I am also updating "helper" one. After then I am running install goal in gradle's maven plugin and get jars in local maven repo. After that I do gradle update in "main" project and have updated jars linked.

There are two questions.

1) If staying with maven plugin, then how to publish source code into local maven repo too?

2) Can I do similar without maven plugin, staying only with Gradle?

like image 395
Dims Avatar asked Mar 13 '17 13:03

Dims


3 Answers

I believe a cleaner and more concise solution for this is to use the maven-plugin, it provides the install task which matches Maven's install task. Update your build.gradle to include the following:

apply plugin: 'maven'

Then to publish (e.g. install) to Maven local repository, run: gradle install or ./gradlew install. See also this solution: Gradle alternate to mvn install

Note: the current Gradle docs (as of v4.9) says the maven-plugin is the "Old Maven Plugin" (but it is not deprecated). https://docs.gradle.org/current/userguide/maven_plugin.html

I think this is the simplest solution to this question at this time.

like image 123
N. Ngo Avatar answered Sep 20 '22 17:09

N. Ngo


It's easy to publish sources with the "maven-publish" plugin:

apply plugin: "maven-publish"

task sourceJar(type: Jar) {
  from sourceSets.main.allJava
}

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

          artifact sourceJar {
            classifier "sources"
          }
        }
     }
}

Then go publish to local maven run: gradle publishToMavenLocal

More info in the docs: https://docs.gradle.org/current/userguide/publishing_maven.html#gsc.tab=0

like image 21
Strelok Avatar answered Sep 21 '22 17:09

Strelok


Perhaps you are better of with a multi-module project. Then the necessity to install doesn't arise at all.

The topic of -sources.jar is discussed here

like image 30
Frank Neblung Avatar answered Sep 17 '22 17:09

Frank Neblung