build multiple jars from a single project source

后端 未结 1 1689
我在风中等你
我在风中等你 2021-01-13 10:34

I\'m fairly new to gradle.

I\'d like to find out if it is possible to build multiple jars from the same project source. I\'ve browsed previous similar questions, but

相关标签:
1条回答
  • 2021-01-13 10:43

    One clean way of achieving what you want is to create a multi-project build, with two subprojects (foobase & foo2), where the source sets for foo2 is configured to contain the source sets of foobase in addition to its own sources.

    To get different dependencies for the artifacts, you will just need to declare the dependencies section differently in the subprojects.

    To test this, I created a multiproject build with one java file in each subproject. To simplify the output here, the root build.gradle file contains everything, including subproject specific customizations. In "real life" though, I always put subproject specific configurations in a build.gradle file at the correct subproject level.

    The gradle build file contains

    • Adding source sets from foobase to foo2
    • Different dependencies in the two projects
    • Deployment to a local maven repository (to verify that pom's are created correctly)
    • Optional IDE plugins for IntelliJ IDEA and Eclipse

    All in all, I ended up with the following:

    Project structure

    build.gradle                      => root project build file
    settings.gradle                   => specification of included subprojects
    foo2\                             => foo2 subproject folder
      src\
        main\
          java\
            Foo2.java                 => Empty class
    foobase\                          => foobase subproject folder
      src\
        main\
          java\
            FooBase.java              => Empty class
    

    settings.gradle

    include ':foobase', ':foo2'
    

    build.gradle

    allprojects {
        apply plugin: 'idea'
        apply plugin: 'eclipse'
        group = 'org.foo'
        version = '1.0'
    }
    
    subprojects {
        apply plugin: 'java'
        apply plugin: 'maven'
    
        repositories {
            mavenCentral()
        }
    
        uploadArchives {
            it.repositories.mavenDeployer {
                repository(url: "file:///tmp/maven-repo/")
            }
        }
    }
    
    project(':foobase') {
        dependencies {
            compile 'log4j:log4j:1.2.13'
        }
    }
    
    project(':foo2') {
        dependencies {
            compile 'log4j:log4j:1.2.16'
        }
    
        sourceSets.main.java.srcDirs        project(':foobase').sourceSets.main.java
        sourceSets.main.resources.srcDirs   project(':foobase').sourceSets.main.resources
        sourceSets.test.java.srcDirs        project(':foobase').sourceSets.test.java
        sourceSets.test.resources.srcDirs   project(':foobase').sourceSets.test.resources
    }
    

    Note that I added the source directories for resources and tests as well. You may omit the last three lines if that is not required.

    To verify the build:

    • Make sure that the two jar files contains all the wanted classes.
    • Verify that the two deployed maven pom's has the correct dependencies.

    In my case:

    • foobase-1.0.jar contains only FooBase.class
    • foo2-1.0.jar contains both FooBase.class and Foo2.class
    • the uploaded foobase-1.0.pom contains a dependency to log4j-1.2.13
    • the uploaded foo2-1.0.pom contains a dependency to log4j-1.2.16
    0 讨论(0)
提交回复
热议问题