1

I want to copy from more than one directory int one .Jar file:

So that all the .class files in both directories are bundles into one .jar file, is this possible?

task makeJar(type: Copy)
    from('directory1') && (directory2)
    into('another directory')
    include('classes.jar')

I can copy from one directory like this:

task makeJar(type: Copy)
    from('directory1')
    into('another directory')
    include('classes.jar')
Sbonelo
  • 664
  • 1
  • 9
  • 25
  • Have you seen my answer? Also, did you want a .jar of your sources(.java) as well? – Jared Burrows Apr 29 '15 at 19:27
  • @JaredBurrows, Yes I just did, the problem is I want to copy 2 separate **classes.jar** from different directories and make one **FILE.jar** from both of them, in such a way that all **.classes** that are contained in them are now contained in one .jar – Sbonelo Apr 29 '15 at 21:01
  • @JaredBurrows Your answer copies correctly, but I cannot merge the two classes.jar into one. The directory that I copy into always contains the last classes.jar that I copied. – Sbonelo Apr 29 '15 at 21:10
  • Why do you want the `.class` files? are you trying to make a compiled `.jar` of your project? – Jared Burrows Apr 29 '15 at 21:23
  • Now I understand, see my updated answer. – Jared Burrows Apr 29 '15 at 21:31

1 Answers1

4

For Android:

Command:

gradlew jarDebug / gradlew jarRelease

Code:

android.libraryVariants.all { variant ->
    task("jar${variant.name}", type: Jar) {
        description "Bundles compiled .class files into a JAR file for $variant.name."
        dependsOn variant.javaCompile
        from variant.javaCompile.destinationDir
        exclude '**/R.class', '**/R$*.class', '**/R.html', '**/R.*.html'
    }
}

Source: https://stackoverflow.com/a/19967914/950427

Java or Android Project:

task makeJar(type: Copy) {
    from('directory1/')
    from('directory2/')

    into('another directory/')
    include('classes.jar')
}

Similar: https://stackoverflow.com/a/19037807/950427

Example:

task initConfig(type: Copy) {
    from('src/main/config') {
        include '**/*.properties'
        include '**/*.xml'
        filter(ReplaceTokens, tokens: [version: '2.3.1'])
    }
    from('src/main/config') {
        exclude '**/*.properties', '**/*.xml'
    }
    from('src/main/languages') {
        rename 'EN_US_(.*)', '$1'
    }
    into 'build/target/config'
    exclude '**/*.bak'

    includeEmptyDirs = false

    with dataContent
}

Source: http://gradle.org/docs/current/dsl/org.gradle.api.tasks.Copy.html

Community
  • 1
  • 1
Jared Burrows
  • 54,294
  • 25
  • 151
  • 185