mrt181
mrt181

Reputation: 5316

How to copy mutliple jars with gradle?

I want to execute a custom task, which should build some jars and copy these to a different directory.

This is my build.gradle file:

allprojects {
  apply plugin: 'java'
  apply plugin: 'eclipse'
  task hello << { task -> println "I'm project $task.project.name" }
}

subprojects {
  repositories {
    maven {
      url "http://repo1.maven.org/maven2"
    }
  }

  task allDependencies(type: DependencyReportTask) {}
  task allDependenciesInsight(type: DependencyInsightReportTask) << {}

  dependencies {
    compile 'log4j:log4j:1.2.14'
    compile 'org.eclipse.jdt:org.eclipse.jdt.annotation:2.0.+'

    testCompile "junit:junit:4.+"
    testCompile 'org.mockito:mockito-all:1.10.+'
  }

  task copyJars(type: Copy) << {
    println "Copies artifacts from " + project.libsDir + " to " + project.rootDir + "."
    from('$project.libsDir')
    into('$project.rootDir')
  }
}

task bamm(type: Copy) << {
  description "Creates BAMM plugin."
  println "Creates BAMM plugin."
  println "Copies artifacts from " + project.libsDir + " to " + gradle.bammDir
  from(project.libsDir)
  into(gradle.bammDir)
}

bamm.dependsOn ':camm-server:jar', ':camm-server:copyJars'

project(':camm-shared') {
  dependencies {
    compile 'joda-time:joda-time:1.5.2'
    compile 'org.springframework:spring:2.5.6.+'
    compile files ('../lib/MarketInterface.jar')
  }
}

project(':camm-client') {
  dependencies {
    compile 'org.jfree:jfreechart:1.0.+'
    compile project(':camm-shared')
  }
}

project(':camm-server') {
  dependencies {
    compile 'org.jibx:jibx-run:1.2.+'
    compile project(':camm-client')
    compile project(':camm-shared')

    testCompile files ('../lib/MarketInterface.jar')
    testCompile project(':camm-shared').sourceSets.test.output
  }
}

task wrapper(type: Wrapper) {
  gradleVersion = '2.8'
}

I can run the bamm task but it never copies anything.

My goal is to build all necessary jars (which works) and to copy all of the jars created from my projects to the gradle.bammDir.

Upvotes: 0

Views: 696

Answers (1)

Opal
Opal

Reputation: 84874

from and into does not work when added in an action (<<). It should be:

task bamm(type: Copy) {
  description "Creates BAMM plugin."
  println "Creates BAMM plugin."
  println "Copies artifacts from " + project.libsDir + " to " + gradle.bammDir
  from(project.libsDir)
  into(gradle.bammDir)
}

or:

task bamm << {
  description "Creates BAMM plugin."
  println "Creates BAMM plugin."
  println "Copies artifacts from " + project.libsDir + " to " + gradle.bammDir
  copy {
     from(project.libsDir)
     into(gradle.bammDir)
  }
}

Please read about configuration and execution phase - it's crucial to understand how gradle works.

Upvotes: 2

Related Questions