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. react native项目直接全局定义横竖屏幕

    这里指的是项目本身为横屏项目,直接全局设置,当然还有其他方法,使用react-native-orientation 全局定义如图: iOS android:

  3. deb文件怎么安装

    deb 是 ubuntu .debian 的格式.rpm 是 redhat .fedora .suse 的格式.deb是debian发行版的软件包ubuntu是基于debian 发行的 所有可以用.d ...

  4. python schedule 任务调度

    python schedule可以简单处理一些日常提醒的工作,下面简要说一下用法: import schedule import time def job(): print("I'm wor ...

  5. Maximum Subarray解题报告zz

    http://fisherlei.blogspot.com/2012/12/leetcode-maximum-subarray.html Find the contiguous subarray wi ...

  6. 关于map容器的元素被无参初始化

    使用C++中的map容器定义一个mp,当你执行if语句判断mp[3]是否为1时,那么如果mp[3]以前不存在,此时mp[3]就会被无参初始化,second赋值为0. 以下的程序可以证明这一点.执行了第 ...

  7. 爬虫入门之Scrapy 框架基础功能(九)

    Scrapy是用纯Python实现一个为了爬取网站数据.提取结构性数据而编写的应用框架,用途非常广泛. 框架的力量,用户只需要定制开发几个模块就可以轻松的实现一个爬虫,用来抓取网页内容以及各种图片,非 ...

  8. IntelliJ IDEA下"Cannot resolve symbol 'log'"的解决方法

    转自:https://my.oschina.net/greatqing/blog/703989 最近接手了一个Maven项目,IDE使用的是IntelliJ IDEA,导入后可以编译运行.但是输出日志 ...

  9. MapReduce编程中常用的字符操作

    本文主要用于记录自己在编写mapreduce程序时常用的一些方法,后期会不断更新,用于自己复习和给新手一些帮助. 字符串操作 String str = " 12345"; // 字 ...

  10. Fcoin和Coinex,谁更值得投资?

    2018年是数字货币市场很熊的一年,大部分币种在这一年下跌超过了90%.但是这一年对于数字货币的发展也是不平凡的,可以说是至关重要的一年.通证经济商业模型在这一年萌芽,并茁壮成长,2018是通证经济元 ...