简体   繁体   中英

Add to subproject to gradle jar

How can I add a subproject referenced using project(':api') to the jar gradle builds?

This is the build.gradle of my main project. The subproject is includes as git submodule and has a similar buildscript.

apply plugin: 'java'

sourceCompatibility = 1.5
version = '1.0'

jar {
    manifest {
        attributes('Main-Class': '..........')
    }
}

repositories {
    mavenCentral()
}

dependencies {
    compile files('libs/jfxrt.jar')
    compile project(':api')
    testCompile group: 'junit', name: 'junit', version: '4.11'
}

I figured it out on my own.

Include the source of a subproject in the main jar:

sourceSets {
    main {
        java {
            srcDir project(':api').file('src/main/java')
        }
    }
}

Including the classes of a jar in the main jar:

jar {
    from zipTree('libs/abc.jar')
}

Try to add classpath to your manifest file. You need to have directory (example below uses "lib") to keep jar files on which your project depends.

Try modifying your "jar" block in gradle build to something like this. I have some addition properties just for demonstration. But the important one is Class-Path

jar {
    manifest.attributes(
        'Class-Path': lib/api.jar
        'Built-By': System.getProperty('user.name'),
        'Built-JDK': System.getProperty('java.version'),
        'Built-OS': System.getProperty('os.name'),
        'Built-DATE': buildDate,
    )
}

I hope it helps to fix your issue.

In the simplest case, a fat Jar can be created as follows:

main/build.gradle:

jar {
    from configurations.runtime
}

There are other, more robust solutions, such as the gradle-one-jar plugin for "main" method style applications.

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