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. poj 1947 树形背包 (删边)

    http://blog.csdn.net/woshi250hua/article/details/7632785 这道题我一开始想的dp[i][j],i是节点,j是删除的点数,dp是最少删边的个数,然 ...

  2. 《第一行代码》Android特色开发,基于位置服务,出现的问题

    手机GPS定位较慢.精度高.耗电量多,网络定位较快.精度低.耗电量少 当位置精度要求非常高的时候,使用GPS定位:一般情况下,使用网络定位. 按<第一行代码>写了一个定位程序,真机一直没有 ...

  3. PAT 1071. Speech Patterns

    又是考输入输出 #include <cstdio> #include <cstdlib> #include <string> #include <vector ...

  4. H5,API的pushState(),replaceState()和popstate()用法

    pushState和replaceState是H5的API中新添加的两个方法.通过window.history方法来对浏览器历史记录的读写. pushState和replaceState 在 HTML ...

  5. 洛谷P1024 一元三次方程求解(数学)

    题意 题目链接 Sol 本来是一道好的公式题. 然后输出只要保留两位小数?? 直接上不就赢了嘛.. #include<bits/stdc++.h> #define LL long long ...

  6. Eclipse常用操作

    一 Eclipse常用快捷键 ctrl+Q:跳到最后一次编辑处. ctrl+F:在当前文件中查找并替换. ctrl+T:查看类的继承关系. alt+左箭头:返回到光标的上一个位置. alt+右箭头:前 ...

  7. CentOS 7运维管理笔记(2)----修改命令提示符颜色

    使用 su  命令切换到root用户: 使用 vi /etc/bashrc 命令插入如下代码: PS1="[\e[1;32m\u\e[m\e[1;33m@\e[m\e[1;35m\h\e[m ...

  8. Android中获取TextView行数

    项目中发现,如果直接通过TextView.getLineCount()方法获取行数时,总是0,研究发现,setText()后立即调用getLineCount(), 这时TextView还未完成meas ...

  9. Goclipse的Eclipse插件包安装升级地址

    http://goclipse.github.io/releases/ Eclipse Software Site for Goclipse This URL is an Eclipse softwa ...

  10. string 替换字符串

    C++的string提供了replace方法来实现字符串的替换,但是对于将字符串中某个字符串全部替换这个功能,string并没有实现,我们今天来做的就是这件事.首先明白一个概念,即string替换所有 ...