15

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?

Tunaki
  • 132,869
  • 46
  • 340
  • 423
Dims
  • 47,675
  • 117
  • 331
  • 600
  • I would use the solution that Frank Neblung linked and recommend using the -t (--coninious) option that will install it right away on every change. "gradle install -t" and as the file is changing it is not needed to reload gradle every time. – Hillkorn Mar 13 '17 at 21:23

5 Answers5

31

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

Strelok
  • 50,229
  • 9
  • 102
  • 115
  • 5
    BTW, you can use abbreviated task name to reduce typing on command line, e.g. the abbreviated form of "publishToMavenLocal" is "pTML". Hence you can also run `gradle pTML` – N. Ngo Oct 02 '18 at 13:59
  • 1
    To get this to work with Groovy/Spring project, I need to change `from sourceSets.main.allJava` to `from sourceSets.main.allSource` – SGT Grumpy Pants Sep 30 '22 at 15:41
4

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.

N. Ngo
  • 646
  • 5
  • 6
2

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

Community
  • 1
  • 1
Frank Neblung
  • 3,047
  • 17
  • 34
1

I managed to do this by simply adding this to the build file:

java {    
    withSourcesJar()
}
Tom
  • 1,414
  • 12
  • 21
1

Here is an alternative skeleton for Gradle 7.5.1 with Java 17 which would generate javadoc and sources jars along with the source code.

build.gradle

plugins {
    id 'org.gradle.java'
    id 'org.gradle.maven-publish'
}

repositories {
    mavenLocal()
    mavenCentral()
}

dependencies {
}

java {
    toolchain {
        languageVersion = JavaLanguageVersion.of(17)
    }
    withJavadocJar()
    withSourcesJar()
}

publishing {
    publications {
        mavenJava(MavenPublication) {
            groupId = 'your-group'
            artifactId = 'your-artifact'
            version = "0.0.1"
            from components.java
        }
    }
    repositories {
        mavenLocal()
    }
}

Publishing

Add --info for more details on the publishing.

./gradlew --info publishToMavenLocal

Output

~/.m2/repository $ tree your-group/
your-group/
└── your-artifact
    ├── 0.0.1
    │   ├── your-artifact-0.0.1-javadoc.jar
    │   ├── your-artifact-0.0.1-sources.jar
    │   ├── your-artifact-0.0.1.jar
    │   ├── your-artifact-0.0.1.module
    │   └── your-artifact-0.0.1.pom
    └── maven-metadata-local.xml
rbento
  • 9,919
  • 3
  • 61
  • 61