Wudong
Wudong

Reputation: 2360

Modify gradle's runtime dependencies according to operation system?

Is it possible to change gradle's runtime dependencies according to the what the operation system it is on?

I'm using the SWT in my application which has jars that are platform dependent. I want to only distribute the right SWT jar for each platform it is running. Something like:

dependencies {
    runtime fileTree(dir: 'swt', include: 'swt_win_32.jar') if windows.
    runtime fileTree(dir: 'swt', include: 'swt_linux_x86.jar') if linux.
}

Hope this question make sense. Thanks.

Upvotes: 7

Views: 3465

Answers (4)

Ned Twigg
Ned Twigg

Reputation: 2224

The goomph plugin has a mavencentral plugin specifically for this purpose.

apply plugin: 'com.diffplug.gradle.eclipse.mavencentral'
eclipseMavenCentral {
  release '4.7.0', {
    compile 'org.eclipse.swt'
    useNativesForRunningPlatform()
  }
}

Upvotes: 0

Alpar
Alpar

Reputation: 2897

If you want to go with a more Gradle-ish way to do it you can use the Gradle OS Plugin. https://bitbucket.org/tinhtruong/gradle-os-plugin

Upvotes: 0

Jerome
Jerome

Reputation: 8447

String jarName;
switch(System.getProperty('os.name').toLowerCase().split()[0]) {
  case 'windows':
    jarName = 'swt_win_32.jar' 
    break
  case 'linux':
    jarName = 'swt_linux_x86.jar' 
    break
  default:
    throw new Exception('Unknown OS')
}

dependencies {
  runtime fileTree(dir: 'swt', include: jarName)
}

Upvotes: 6

Peter Niederwieser
Peter Niederwieser

Reputation: 123960

Gradle has no public API to check for OS, but you can use something like System.getProperty("os.name") and act accordingly (e.g with a simple if-statement).

Upvotes: 3

Related Questions