简体   繁体   中英

How to create a Jar file with only project dependencies, without project classes?

I have a Java project that uses some dependencies, ie Commons, HttpClient, SQLite.

I would like to create two build tasks in Gradle:

  1. First one would allow me to build a Jar file with only Commons, HttpClient, Sqlite, without my project,

  2. Second one would allow me to build a Jar file with my project alone. This is I guess a default build action behavior, so I already know how to do it :)

I also know how to create a Jar file with my project and dependencies together (a "fat jar"), but the resulting Jar file is big with all my dependencies grouped together (~ 14mb).

The reason for my question is that I'm testing the application on a remote server. My thought was that I will upload the 'jar with dependencies' only occasionally, once every few days, when dependencies will change, while uploading the 'application jar' every few builds. Right now I'm waiting few minutes on each upload and I would want to wait few seconds.

Is this possible in Gradle?

This is my current gradle config:

apply plugin: 'java'

buildscript {
    repositories {
        mavenCentral()
    }
}

repositories {
    mavenCentral()
}

task fatjar(type: Jar) {
    from {
        configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
    }

    baseName = project.name + "-all"

    with jar
}

compileJava {
    options.encoding = 'UTF-8'
    options.compilerArgs << "-Xlint:unchecked" << "-Xlint:deprecation"
}

dependencies {
    compile group: 'commons-collections', name: 'commons-collections', version: '3.2.2'
    compile 'org.hjson:hjson:1.0.0'
    compile group: 'commons-cli', name: 'commons-cli', version: '1.3.1'
    compile group: 'commons-io', name: 'commons-io', version: '2.4'
    compile group: 'org.java-websocket', name: 'Java-WebSocket', version: '1.3.0'
    compile group: 'org.apache.httpcomponents', name: 'httpclient', version: '4.5.3'
    compile group: 'org.apache.httpcomponents', name: 'httpcore', version: '4.4.6'
    compile group: 'org.json', name: 'json', version: '20160810'
    compile group: 'org.xerial', name: 'sqlite-jdbc', version: '3.16.1'
    compile group: 'org.zeromq', name: 'jeromq', version: '0.4.0'
    compile group: 'com.nimbusds', name: 'nimbus-jose-jwt', version: '4.23'
    compile group: 'ws.wamp.jawampa', name: 'jawampa-core', version: '0.5.0'
    compile group: 'ws.wamp.jawampa', name: 'jawampa-netty', version: '0.5.0'
}

edit : added more context to the question.

You can just remove

with jar

from your task, which is what includes the content of your project jar file into the fat jar. See the documentation .

I know your question is about Gradle, but for people who find this page while wanting to do this with Maven, it can be done with the following config for the maven-shade-plugin:

<artifactSet>
    <excludes>
        <exclude>${project.groupId}:${project.artifactId}</exclude>
    </excludes>
</artifactSet>

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