With Gradle we can execute Java applications using the JavaExec task or the javaexec() method. If we want to run Java code from an external dependency we must first pull in the dependency with the Java application code. The best way to do this is to create a new dependency configuration. When we configure a task with type JavaExec we can set the classpath to the external dependency. Notice we cannot use the buildscript{} script block to set the classpath. A JavaExec task will fork a new Java process so any classpath settings via buildscript{} are ignored.

In the following example build script we want to execute the Java class org.apache.cxf.tools.wsdlto.WSDLToJava from Apache CXF to generate Java classes from a given WSDL. We define a new dependency configuration with the name cxf and use it to assign the CXF dependencies to it. We use the classpath property of the JavaExec task to assign the configuration dependency.

00.// File: build.gradle
01. 
02.// Base plugin for task rule clean<task>
03.apply plugin: 'base'
04. 
05.repositories.mavenCentral()
06. 
07.// New configuration for CXF dependencies.
08.configurations { cxf }
09. 
10.ext {
11.// CXF version.
12.cxfVersion = '2.6.2'
13. 
14.// Artifacts for CXF dependency.
15.cxfArtifacts = [
16.'cxf-tools-wsdlto-frontend-jaxws',
17.'cxf-tools-wsdlto-databinding-jaxb',
18.'cxf-tools-common',
19.'cxf-tools-wsdlto-core'
20.]
21.}
22. 
23.dependencies {
24.// Assign CXF dependencies to configuration.
25.cxfArtifacts.each { artifact ->
26.cxf "org.apache.cxf:$artifact:$cxfVersion"
27.}
28.}
29. 
30.// Custom task to generate Java classes
31.// from WSDL.
32.task wsdl2java(type: JavaExec) {
33.ext {
34.wsdlFile = 'src/wsdl/service-contract.wsdl'
35.outputDir = file("$buildDir/generated/cxf")
36.}
37. 
38.inputs.file file(wsdlFile)
39.outputs.dir outputDir
40. 
41.// Main Java class to invoke.
42.main = 'org.apache.cxf.tools.wsdlto.WSDLToJava'
43. 
44.// Set classpath to dependencies assigned
45.// to the cxf configuration.
46.classpath = configurations.cxf
47. 
48.// Arguments to be passed to WSDLToJava.
49.args '-d', outputDir
50.args '-client'
51.args '-verbose'
52.args '-validate'
53.args wsdlFile
54.}

Code written with Gradle 1.2

Gradle Goodness: Running Java Applications from External Dependency的更多相关文章

  1. Gradle Goodness: Set Java Compiler Encoding--转载

    原文地址:http://java.dzone.com/articles/gradle-goodness-set-java If we want to set an explicit encoding ...

  2. Gradle Goodness: Set Java Compiler Encoding

    If we want to set an explicit encoding for the Java compiler in Gradle we can use the options.encodi ...

  3. Android Studio Gradle Build Running 特别慢的问题探讨

    本文的本本win7 64bit 6G android studio2.1 在运行程序的时候Gradle Build Running 特别慢,一个helloworld都快2min了 1.开启gradle ...

  4. End-to-End Tracing of Ajax/Java Applications Using DTrace

    End-to-End Tracing of Ajax/Java Applications Using DTrace         By Amit Hurvitz, July 2007     Aja ...

  5. android studio 一直卡在Gradle:Build Running的解决办法

    转:android studio 一直卡在Gradle:Build Running的解决办法   在使用AS开发安卓应用程序的时候经常会遇到Gradle build running一直在运行甚至卡死的 ...

  6. 转:android studio 一直卡在Gradle:Build Running的解决办法

    在使用AS开发安卓应用程序的时候经常会遇到Gradle build running一直在运行甚至卡死的情况,解决方法如下: 方法1: 1.在C:\User\<用户名>\.gradle 目录 ...

  7. Gradle Goodness: Init Script for Adding Extra Plugins to Existing Projects

    Gradle Goodness: Init Script for Adding Extra Plugins to Existing Projects Gradle is very flexible. ...

  8. 解决Android Studio Gradle Build Running慢的问题

    Android Studio方便好用,但是Android Studio Gradle Build Running很慢 解决方法: C:\Users\你的用户名\.gradle 目录下新建一个文件名为 ...

  9. Gradle Goodness: Copy Files with Filtering

    Gradle Goodness: Copy Files with Filtering Gradle's copy task is very powerful and includes filterin ...

随机推荐

  1. CSS3之 :nth-child(n)语法讲解

    语法: E:nth-child(n){ sRules } * 匹配父元素索引为n的子元素E :nth-child(n) 让你匹配到父元素的任一子元素: Figure 1:<section id= ...

  2. Java线程面试题 Top 50 (个人总结)(转)

    问答总结: 1. JDK1.5引入了哪些更高阶的并发工具  2. Java中CyclicBarrier 和 CountDownLatch有什么不同?  CountDownLatch和CyclicBar ...

  3. [WC2016]挑战NPC

    Sol 这做法我是想不到\(TAT\) 每个筐子拆成三个相互连边 球向三个筐子连边 然后跑一般图最大匹配 这三个筐子间最多有一个匹配 那么显然每个球一定会放在一个筐子里,一定有一个匹配 如果筐子间有匹 ...

  4. 把连接中传的参数截取出来变成一个json对象

    获取url function test() { var url=window.location.search; if(url.indexOf("?")!=-1) { var str ...

  5. sauvola二值化算法研究

    sauvola二值化算法研究   sauvola是一种考虑局部均值亮度的图像二值化方法, 以局部均值为基准在根据标准差做些微调.算法实现上一般用积分图方法 来实现.这个方法能很好的解决全局阈值方法的短 ...

  6. hiho一下 第一周 最长回文子串

    时间限制:1000ms 单点时限:1000ms 内存限制:64MB 描述 小Hi和小Ho是一对好朋友,出生在信息化社会的他们对编程产生了莫大的兴趣,他们约定好互相帮助,在编程的学习道路上一同前进. 这 ...

  7. 批量删除微博的js代码

    清空微博,网上找了一段js代码,试了下,还行. var fileref=document.createElement('script') fileref.setAttribute("type ...

  8. Spring Boot Async异步执行

    异步调用就是不用等待结果的返回就执行后面的逻辑,同步调用则需要等带结果再执行后面的逻辑. 通常我们使用异步操作都会去创建一个线程执行一段逻辑,然后把这个线程丢到线程池中去执行,代码如下: Execut ...

  9. win10 x64 python3.6 pycharm 安装statsmodels

    在pycharm下,安装statsmodels,会出现需要vc++14.0的错误提示. 这时可以到网站 https://www.lfd.uci.edu/~gohlke/pythonlibs/#word ...

  10. ES6入门——函数的扩展

    1.函数参数的默认值 在ES6之前,不能直接为函数的参数指定默认值,只能采用变通的方法.现在ES6可以为函数的参数添加默认值,简洁了许多. ES5 function show(a,b){ b = b ...