Gradle Goodness: Running Java Applications from External Dependency

來源:互聯網
上載者:User

標籤:

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.

view sourceprint? 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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.