简体   繁体   中英

How to upload Archives for several modules by using gradle?

I want to publish my SDK in maven with $ ./gradlew uploadArchives

I had this project structure:

MyProject

  |_ SDK
     |_ src
         |_ main
              |_ java
                   |_ java files
     |_ build.gradle
  |_ gradle
        |_ gradle-publish.gradle

I used following code ( gradle-publish.gradle ) that works as expected:

task androidJavadocsJar(type: Jar, dependsOn: androidJavadocs) {
    classifier = 'javadoc'
    from androidJavadocs.destinationDir
}

task sourcesJar(type: Jar) {
    classifier = 'sources'
    from android.sourceSets.main.java.sourceFiles
}

uploadArchives {
    repositories {
        mavenDeployer {
         /* ... */
        }
    }
}

artifacts {
    archives sourcesJar
    archives androidJavadocsJar
}

Now I splitted main module to 2: SDK and new_module so my project structure looks like:

MyProject

  |_ SDK
     |_ src
         |_ main
              |_ java
                   |_ java files  "com.app"
     |_ build.gradle

 |_ new_module
     |_ src
         |_ main
              |_ java
                   |_ java files "com.app.mod"
      |_ build.gradle
  |_ gradle
        |_ gradle-publish.gradle

However no matter what I do, I always get only SDK sources and not new_module ae com.app should be merged with com.app.mod

I tried to play with sourceSets but it looks like it works inside main module SDK and not under root project:

sourceSets {
    main{
      java {
        srcDirs(
                "src/main/java",
                 "../new_module/src/main/java"
        )
    }
}

and:

task sourcesJar(type: Jar) {
   classifier = 'sources'   
   from android.sourceSets.main.java.srcDirs    
}

Any ideas, tips?

Edit

I added

sourceSets {
    main{
      java {

        srcDirs("src/main/java",
                "${root_dir}/new_module/src/main/java"
        )
      }
    }
  }

However on maven I see all have sources but classes.jar contains only app .class files

Add custom sourceSet under android section of your SDK build.gradle :

android{
    //...
    sourceSets {
        main{
            java {
                srcDirs "$rootDir/new_module/src/main/java/"

            }
        }
    }
}

You don't need include "src/main/java" - its already included

Also you don't need bind new_module into your dependancies, new_module will be a part of your SDK module

task androidSourcesJar(type: Jar) {
    classifier = 'sources'
    from android.sourceSets.main.java.sourceFiles
  }

Did you try to include your gradle-publish.gradle in each build.gradle? Or alternatively in your main build.gradle?

Eg in ${rootDir}/new_module/build.gradle

apply from "${rootDir}/gradle/gradle-publish.gradle"

Or in ${rootDir}/build.gradle :

subprojects {
  apply from "${rootDir}/gradle/gradle-publish.gradle"
}

Or maybe you didn't define the artifacts to publish in your build.gradle:

artifacts {
    archives jar, javadocJar, sourcesJar
    testArchives testJar, testSourcesJar
}

task javadocJar(type: Jar) {
    description "Assembles a jar archive containing the javadoc files"

    classifier = "javadoc"
    from javadoc
}

task sourcesJar(type: Jar) {
    description "Assembles a jar archive containing the source files"

    classifier = "sources"
    from sourceSets.main.allSource
}

task testJar(type: Jar, dependsOn: project.testClasses) {
    description "Assembles a jar archive containing the test files"

    classifier = "test"
    from project.sourceSets.test.output
}

task testSourcesJar(type: Jar) {
    description "Assembles a jar archive containing the test source files"

    classifier = "test-sources"
    from sourceSets.test.allSource
}

Check this link it's intended for distribution through Jcenter, follow the steps mentioned in the link for each of your submodules.

And make sure all your submodules are published to the same remote with a common GROUP_ID.

uploadArchives isn't meant to be used to install to the local Maven repository. Instead, use the install task.

replace this: from

uploadArchives {
    repositories {
        mavenDeployer {
         /* ... */
        }
    }
}

to :

install{
        repositories {
            mavenDeployer {
             /* ... */
            }
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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