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. CSS实现响应式布局(自动拆分几列)

    1.css代码 <style type="text/css"> .container{ margin-top: 10px; } .outerDiv{ float:lef ...

  2. Codis3.2 安装部署

    转载请注明出处:https://www.cnblogs.com/format-ch/p/9323841.html 一.软件下载 下载 下载 zookeeper (Codis注册中心) http://m ...

  3. Maven+MyBatis 初试

    工作中一直使用的都是Hibernate,总是听见有人拿Mybatis和Hibernate做比较,今天尝试来看看. 一.用Maven建立web项目 此处参见 http://www.cnblogs.com ...

  4. vue-i18n国际化实例

    demo 场景需求分析 需求很简单,左上角 ''网易云音乐''就是一个中英文切换的按钮,点击弹出提示框,确认切换语言后,实现英文版本. 切换成英文版本: 三.实现国际化 1.我们得先有开发环境,先有项 ...

  5. C++中 set(集合容器)的用法

    set集合容器: 实现了红黑树(Red-Black Tree)的平衡二叉检索树的数据结构,在插入元素时, 它会自动调整二叉树的排列,把该元素放到适当的位置,以确保每个子树根节点的键值大于左子树所有节点 ...

  6. 实验一:使用符号执行工具klee对软件进行破解(来自于klee官网)

    原文地址:https://gitlab.com/Manouchehri/Matryoshka-Stage-2/blob/master/stage2.md 实验用代码下载地址:https://gitla ...

  7. 对连接到 Azure 中 Linux VM 时出现的问题进行详细的 SSH 故障排除的步骤

    有许多可能的原因会导致 SSH 客户端无法访问 VM 上的 SSH 服务. 如果已经执行了较常规的 SSH 故障排除步骤,则需要进一步排查连接问题. 本文指导用户完成详细的故障排除步骤,以确定 SSH ...

  8. SSIS ->> 管理和维护SSISDB

    Comming soon!!! 参考文献: Setup and Performance Issues with the Integration Services (SSIS) 2012 Catalog ...

  9. python全栈学习笔记(一)网络基础之网络协议篇

    阅读目录 一.操作系统基础 二.网络通信原理 2.1 互联网的本质就是一系列的网络协议 2.2 osi七层协议 2.3 tcp/ip五层模型讲解 2.3.1 物理层 2.3.2 数据链路层 2.3.3 ...

  10. Python学习---内置函数的学习

    内置函数 [Py3.5官方文档]https://docs.python.org/3.5/library/functions.html#abs Built-in Functions abs() dict ...